diff options
Diffstat (limited to '2022/day2/part2/src')
| -rw-r--r-- | 2022/day2/part2/src/main.rs | 39 |
1 files changed, 39 insertions, 0 deletions
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 | } | ||
