summaryrefslogtreecommitdiffstats
path: root/2022
diff options
context:
space:
mode:
Diffstat (limited to '2022')
-rw-r--r--2022/day2/part2/Cargo.toml9
-rw-r--r--2022/day2/part2/src/main.rs39
2 files changed, 48 insertions, 0 deletions
diff --git a/2022/day2/part2/Cargo.toml b/2022/day2/part2/Cargo.toml
new file mode 100644
index 0000000..69fa504
--- /dev/null
+++ b/2022/day2/part2/Cargo.toml
@@ -0,0 +1,9 @@
1[package]
2name = "part2"
3version = "0.1.0"
4edition = "2021"
5
6# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7
8[dependencies]
9itertools = "0.10.5"
diff --git a/2022/day2/part2/src/main.rs b/2022/day2/part2/src/main.rs
new file mode 100644
index 0000000..86c893b
--- /dev/null
+++ b/2022/day2/part2/src/main.rs
@@ -0,0 +1,39 @@
1/// Rock : 1
2/// Paper : 2
3/// Scissors : 3
4/// opponent
5/// R P S
6/// me A B C
7/// _ L X 3 1 2
8/// _ D Y 4 5 6
9/// _ W Z 8 9 7
10///
11/// idx = 3 * op_idx + my_idx
12const RPS_RESULTS: [usize; 9] = [3, 1, 2, 4, 5, 6, 8, 9, 7];
13
14fn main() {
15 let score = include_str!("../../input").lines().fold(0, parse_line);
16 println!("{}", score);
17}
18
19fn parse_line(score: usize, line: &str) -> usize {
20 let mut moves = line.split(' ');
21 let opponent_move = moves.next().unwrap();
22 let my_move = moves.next().unwrap();
23
24 let my_idx = match opponent_move {
25 "A" => 0,
26 "B" => 1,
27 "C" => 2,
28 _ => unreachable!(),
29 };
30
31 let op_idx = match my_move {
32 "X" => 0,
33 "Y" => 1,
34 "Z" => 2,
35 _ => unreachable!(),
36 };
37
38 RPS_RESULTS[3 * op_idx + my_idx] + score
39}