From 59826b861c1dabd7cac8558c668172187b45ae9c Mon Sep 17 00:00:00 2001 From: Yigit Sever Date: Fri, 2 Dec 2022 21:58:46 +0300 Subject: 2022, day2: part 1 done --- 2022/day2/part1/src/main.rs | 31 +++++++++++++++++++++++-------- 1 file 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 @@ -let rps_result = vec![4, 1, 7, 8, 5, 2, 3, 9 ,6]; - +/// opponent +/// R P S +/// me A B C +/// 1 R X 4 1 7 +/// 2 P Y 8 5 2 +/// 3 S Z 3 9 6 +/// +/// idx = 3 * op_idx + my_idx +const RPS_RESULTS: [usize; 9] = [4, 1, 7, 8, 5, 2, 3, 9, 6]; fn main() { - let score = include_str!("../../example").lines().fold(0, parse_line); + let score = include_str!("../../input").lines().fold(0, parse_line); + println!("{}", score); } fn parse_line(score: usize, line: &str) -> usize { @@ -10,12 +18,19 @@ fn parse_line(score: usize, line: &str) -> usize { let opponent_move = moves.next().unwrap(); let my_move = moves.next().unwrap(); - match opponent_move { - "A" => 1, - "B" => 2, - "C" => 3, + 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!(), }; - 0 + score + RPS_RESULTS[3 * op_idx + my_idx] + score } -- cgit v1.2.3-70-g09d2