File tree 3 files changed +82
-0
lines changed 3 files changed +82
-0
lines changed Original file line number Diff line number Diff line change
1
+ // eslint-disable-next-line no-unused-vars
2
+ const console = require ( '../helpers' )
3
+ require ( './solution' )
Original file line number Diff line number Diff line change
1
+
2
+ // Lookup tables for possible rock / paper / scissor values
3
+ const selfCodes = [ 'X' , 'Y' , 'Z' ]
4
+ const opponentCodes = [ 'A' , 'B' , 'C' ]
5
+
6
+ const scoreRound = ( opponent , self ) => {
7
+ const scoreShape = ( self ) => {
8
+ return selfCodes . indexOf ( self ) + 1
9
+ }
10
+
11
+ const scoreOutcome = ( opponent , self ) => {
12
+ const selfScore = selfCodes . indexOf ( self )
13
+ const oppScore = opponentCodes . indexOf ( opponent )
14
+ // Win
15
+ if (
16
+ ( selfScore - 1 === oppScore ) ||
17
+ ( selfScore === - 1 && oppScore === 2 )
18
+ ) {
19
+ return 6
20
+ }
21
+ // Lose
22
+ if (
23
+ ( oppScore - 1 === selfScore ) ||
24
+ ( oppScore === - 1 && selfScore === 2 )
25
+ ) {
26
+ return 0
27
+ }
28
+ // Draw
29
+ if ( selfCodes . indexOf ( self ) === opponentCodes . indexOf ( opponent ) ) {
30
+ return 3
31
+ }
32
+
33
+ throw new Error ( `Could not calculate the results of the match: ${ opponent } , ${ self } ` )
34
+ }
35
+
36
+ return scoreShape ( self ) + scoreOutcome ( opponent , self )
37
+ }
38
+
39
+ /**
40
+ * Tallies the results of all rounds in a match
41
+ * @param {* } guide
42
+ * @returns
43
+ */
44
+ const scoreMatch = ( guide ) => {
45
+ return guide . map ( ( match ) => {
46
+ return scoreRound ( ...match )
47
+ } ) . reduce ( ( sum , value ) => sum + value , 0 )
48
+ }
49
+
50
+ module . exports = {
51
+ scoreMatch,
52
+ scoreRound
53
+ }
Original file line number Diff line number Diff line change
1
+ /* eslint-env mocha */
2
+ const { expect } = require ( 'chai' )
3
+ const { scoreMatch, scoreRound } = require ( './rochambeau' )
4
+
5
+ describe . only ( '--- Day 2: Rock Paper Scissors ---' , ( ) => {
6
+ describe ( 'Part 1' , ( ) => {
7
+ describe ( 'scoreRound' , ( ) => {
8
+ it ( 'calculates the score of a round based on what the opponent played and what you played' , ( ) => {
9
+ expect ( scoreRound ( 'A' , 'Y' ) ) . to . equal ( 8 )
10
+ expect ( scoreRound ( 'B' , 'X' ) ) . to . equal ( 1 )
11
+ expect ( scoreRound ( 'C' , 'Z' ) ) . to . equal ( 6 )
12
+ } )
13
+ } )
14
+ describe ( 'scoreMatch' , ( ) => {
15
+ it ( 'calculates the total score of a match' , ( ) => {
16
+ expect (
17
+ scoreMatch ( [
18
+ [ 'A' , 'Y' ] ,
19
+ [ 'B' , 'X' ] ,
20
+ [ 'C' , 'Z' ]
21
+ ] )
22
+ ) . to . equal ( 15 )
23
+ } )
24
+ } )
25
+ } )
26
+ } )
You can’t perform that action at this time.
0 commit comments