@@ -6,11 +6,15 @@ namespace AdventOfCode.Y2022.Day02;
6
6
[ ProblemName ( "Rock Paper Scissors" ) ]
7
7
class Solution : Solver {
8
8
9
- // There are many obscure ways of solving this challenge, this is a
10
- // rather explicit one. We parse the input lines into a pair of
11
- // Rock/Paper/Scissors signs represented as 1,2,3 (the values from the
12
- // problem description). We calculate the score for each pair and sum up
13
- // the result.
9
+ // There are many obscure ways of solving this challenge. You can use
10
+ // mod 3 arithmetic or play with ASCII encoding. This approach is a more
11
+ // explicit one. I think it is as simple as it gets.
12
+
13
+ // We parse the input lines into a pair of Rock/Paper/Scissors signs
14
+ // represented by 1,2,3 (the values from the problem description),
15
+ // calculate the score for each pair and sum it up.
16
+
17
+ // Part one and two differs only in the decoding of the X, Y and Z signs.
14
18
15
19
enum Sign {
16
20
Rock = 1 ,
@@ -19,44 +23,44 @@ enum Sign {
19
23
}
20
24
21
25
public object PartOne ( string input ) => (
22
- from match in input . Split ( '\n ' )
23
- let elf =
24
- match [ 0 ] == 'A' ? Sign . Rock :
25
- match [ 0 ] == 'B' ? Sign . Paper :
26
- match [ 0 ] == 'C' ? Sign . Scissors :
27
- throw new ArgumentException ( match )
28
- let you =
29
- match [ 2 ] == 'X' ? Sign . Rock :
30
- match [ 2 ] == 'Y' ? Sign . Paper :
31
- match [ 2 ] == 'Z' ? Sign . Scissors :
32
- throw new ArgumentException ( match )
33
- select Score ( elf , you )
34
- ) . Sum ( ) ;
26
+ from line in input . Split ( '\n ' )
27
+ let elf =
28
+ line [ 0 ] == 'A' ? Sign . Rock :
29
+ line [ 0 ] == 'B' ? Sign . Paper :
30
+ line [ 0 ] == 'C' ? Sign . Scissors :
31
+ throw new ArgumentException ( line )
32
+ let you =
33
+ line [ 2 ] == 'X' ? Sign . Rock :
34
+ line [ 2 ] == 'Y' ? Sign . Paper :
35
+ line [ 2 ] == 'Z' ? Sign . Scissors :
36
+ throw new ArgumentException ( line )
37
+ select Score ( elf , you )
38
+ ) . Sum ( ) ;
35
39
36
40
public object PartTwo ( string input ) => (
37
- from match in input . Split ( '\n ' )
38
- let elf =
39
- match [ 0 ] == 'A' ? Sign . Rock :
40
- match [ 0 ] == 'B' ? Sign . Paper :
41
- match [ 0 ] == 'C' ? Sign . Scissors :
42
- throw new ArgumentException ( match )
43
- let you =
44
- match [ 2 ] == 'X' ? Next ( Next ( elf ) ) : // elf wins
45
- match [ 2 ] == 'Y' ? elf : // draw
46
- match [ 2 ] == 'Z' ? Next ( elf ) : // you win
47
- throw new ArgumentException ( match )
48
- select Score ( elf , you )
49
- ) . Sum ( ) ;
41
+ from line in input . Split ( '\n ' )
42
+ let elf =
43
+ line [ 0 ] == 'A' ? Sign . Rock :
44
+ line [ 0 ] == 'B' ? Sign . Paper :
45
+ line [ 0 ] == 'C' ? Sign . Scissors :
46
+ throw new ArgumentException ( line )
47
+ let you =
48
+ line [ 2 ] == 'X' ? Next ( Next ( elf ) ) : // elf wins
49
+ line [ 2 ] == 'Y' ? elf : // draw
50
+ line [ 2 ] == 'Z' ? Next ( elf ) : // you win
51
+ throw new ArgumentException ( line )
52
+ select Score ( elf , you )
53
+ ) . Sum ( ) ;
50
54
51
55
int Score ( Sign elfSign , Sign yourSign ) =>
52
56
yourSign == Next ( elfSign ) ? 6 + ( int ) yourSign : // you win
53
57
yourSign == elfSign ? 3 + ( int ) yourSign : // draw
54
58
yourSign == Next ( Next ( elfSign ) ) ? 0 + ( int ) yourSign : // elf wins
55
- throw new ArgumentException ( ) ;
59
+ throw new ArgumentException ( ) ;
56
60
57
61
Sign Next ( Sign sign ) =>
58
62
sign == Sign . Rock ? Sign . Paper :
59
63
sign == Sign . Paper ? Sign . Scissors :
60
64
sign == Sign . Scissors ? Sign . Rock :
61
- throw new ArgumentException ( ) ;
65
+ throw new ArgumentException ( ) ;
62
66
}
0 commit comments