diff options
author | Yigit Sever | 2022-12-02 22:08:38 +0300 |
---|---|---|
committer | Yigit Sever | 2022-12-02 22:08:38 +0300 |
commit | 9725ad032ce7d42f01119d8334d420a398d2c855 (patch) | |
tree | 0b4a19c6637215d89b3645502a469a1a77e46a31 | |
parent | 59826b861c1dabd7cac8558c668172187b45ae9c (diff) | |
download | aoc-9725ad032ce7d42f01119d8334d420a398d2c855.tar.gz aoc-9725ad032ce7d42f01119d8334d420a398d2c855.tar.bz2 aoc-9725ad032ce7d42f01119d8334d420a398d2c855.zip |
2022, day2: part 2 done
-rw-r--r-- | 2022/day2/part2/Cargo.toml | 9 | ||||
-rw-r--r-- | 2022/day2/part2/src/main.rs | 39 |
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] | ||
2 | name = "part2" | ||
3 | version = "0.1.0" | ||
4 | edition = "2021" | ||
5 | |||
6 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
7 | |||
8 | [dependencies] | ||
9 | itertools = "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 | ||
12 | const RPS_RESULTS: [usize; 9] = [3, 1, 2, 4, 5, 6, 8, 9, 7]; | ||
13 | |||
14 | fn main() { | ||
15 | let score = include_str!("../../input").lines().fold(0, parse_line); | ||
16 | println!("{}", score); | ||
17 | } | ||
18 | |||
19 | fn 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 | } | ||