summaryrefslogtreecommitdiffstats
path: root/2022/day2/part1/src/main.rs
diff options
context:
space:
mode:
Diffstat (limited to '2022/day2/part1/src/main.rs')
-rw-r--r--2022/day2/part1/src/main.rs31
1 files changed, 23 insertions, 8 deletions
diff --git a/2022/day2/part1/src/main.rs b/2022/day2/part1/src/main.rs
index 44ebe3a..81b20df 100644
--- a/2022/day2/part1/src/main.rs
+++ b/2022/day2/part1/src/main.rs
@@ -1,8 +1,16 @@
1let rps_result = vec![4, 1, 7, 8, 5, 2, 3, 9 ,6]; 1/// opponent
2 2/// R P S
3/// me A B C
4/// 1 R X 4 1 7
5/// 2 P Y 8 5 2
6/// 3 S Z 3 9 6
7///
8/// idx = 3 * op_idx + my_idx
9const RPS_RESULTS: [usize; 9] = [4, 1, 7, 8, 5, 2, 3, 9, 6];
3 10
4fn main() { 11fn main() {
5 let score = include_str!("../../example").lines().fold(0, parse_line); 12 let score = include_str!("../../input").lines().fold(0, parse_line);
13 println!("{}", score);
6} 14}
7 15
8fn parse_line(score: usize, line: &str) -> usize { 16fn parse_line(score: usize, line: &str) -> usize {
@@ -10,12 +18,19 @@ fn parse_line(score: usize, line: &str) -> usize {
10 let opponent_move = moves.next().unwrap(); 18 let opponent_move = moves.next().unwrap();
11 let my_move = moves.next().unwrap(); 19 let my_move = moves.next().unwrap();
12 20
13 match opponent_move { 21 let my_idx = match opponent_move {
14 "A" => 1, 22 "A" => 0,
15 "B" => 2, 23 "B" => 1,
16 "C" => 3, 24 "C" => 2,
25 _ => unreachable!(),
26 };
27
28 let op_idx = match my_move {
29 "X" => 0,
30 "Y" => 1,
31 "Z" => 2,
17 _ => unreachable!(), 32 _ => unreachable!(),
18 }; 33 };
19 34
20 0 + score 35 RPS_RESULTS[3 * op_idx + my_idx] + score
21} 36}