Skip to content

Commit 48fc3bd

Browse files
committed
2022/02 comments
1 parent 1a6285f commit 48fc3bd

File tree

1 file changed

+37
-33
lines changed

1 file changed

+37
-33
lines changed

2022/Day02/Solution.cs

Lines changed: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,15 @@ namespace AdventOfCode.Y2022.Day02;
66
[ProblemName("Rock Paper Scissors")]
77
class Solution : Solver {
88

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.
1418

1519
enum Sign {
1620
Rock = 1,
@@ -19,44 +23,44 @@ enum Sign {
1923
}
2024

2125
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();
3539

3640
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();
5054

5155
int Score(Sign elfSign, Sign yourSign) =>
5256
yourSign == Next(elfSign) ? 6 + (int)yourSign : // you win
5357
yourSign == elfSign ? 3 + (int)yourSign : // draw
5458
yourSign == Next(Next(elfSign)) ? 0 + (int)yourSign : // elf wins
55-
throw new ArgumentException();
59+
throw new ArgumentException();
5660

5761
Sign Next(Sign sign) =>
5862
sign == Sign.Rock ? Sign.Paper :
5963
sign == Sign.Paper ? Sign.Scissors :
6064
sign == Sign.Scissors ? Sign.Rock :
61-
throw new ArgumentException();
65+
throw new ArgumentException();
6266
}

0 commit comments

Comments
 (0)