From 9725ad032ce7d42f01119d8334d420a398d2c855 Mon Sep 17 00:00:00 2001 From: Yigit Sever Date: Fri, 2 Dec 2022 22:08:38 +0300 Subject: 2022, day2: part 2 done --- 2022/day2/part2/Cargo.toml | 9 +++++++++ 2022/day2/part2/src/main.rs | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 2022/day2/part2/Cargo.toml create mode 100644 2022/day2/part2/src/main.rs (limited to '2022/day2/part2') 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 @@ +[package] +name = "part2" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +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 @@ +/// Rock : 1 +/// Paper : 2 +/// Scissors : 3 +/// opponent +/// R P S +/// me A B C +/// _ L X 3 1 2 +/// _ D Y 4 5 6 +/// _ W Z 8 9 7 +/// +/// idx = 3 * op_idx + my_idx +const RPS_RESULTS: [usize; 9] = [3, 1, 2, 4, 5, 6, 8, 9, 7]; + +fn main() { + let score = include_str!("../../input").lines().fold(0, parse_line); + println!("{}", score); +} + +fn parse_line(score: usize, line: &str) -> usize { + let mut moves = line.split(' '); + let opponent_move = moves.next().unwrap(); + let my_move = moves.next().unwrap(); + + let my_idx = match opponent_move { + "A" => 0, + "B" => 1, + "C" => 2, + _ => unreachable!(), + }; + + let op_idx = match my_move { + "X" => 0, + "Y" => 1, + "Z" => 2, + _ => unreachable!(), + }; + + RPS_RESULTS[3 * op_idx + my_idx] + score +} -- cgit v1.2.3-70-g09d2