diff options
| author | Yigit Sever | 2022-12-02 21:58:46 +0300 |
|---|---|---|
| committer | Yigit Sever | 2022-12-02 21:58:46 +0300 |
| commit | 59826b861c1dabd7cac8558c668172187b45ae9c (patch) | |
| tree | 7ce82e970947a9c3575d8e52edc21a535a8ba973 /2022/day2/part1/src | |
| parent | b5715d6b6e7d60e8b662ad4b46f0e1f1e7e445c1 (diff) | |
| download | aoc-59826b861c1dabd7cac8558c668172187b45ae9c.tar.gz aoc-59826b861c1dabd7cac8558c668172187b45ae9c.tar.bz2 aoc-59826b861c1dabd7cac8558c668172187b45ae9c.zip | |
2022, day2: part 1 done
Diffstat (limited to '2022/day2/part1/src')
| -rw-r--r-- | 2022/day2/part1/src/main.rs | 31 |
1 files 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 @@ | |||
| 1 | let rps_result = vec![4, 1, 7, 8, 5, 2, 3, 9 ,6]; | 1 | /// opponent |
| 2 | 2 | /// R P S | |
| 3 | /// me A B C | ||
| 4 | /// 1 R X 4 1 7 | ||
| 5 | /// 2 P Y 8 5 2 | ||
| 6 | /// 3 S Z 3 9 6 | ||
| 7 | /// | ||
| 8 | /// idx = 3 * op_idx + my_idx | ||
| 9 | const RPS_RESULTS: [usize; 9] = [4, 1, 7, 8, 5, 2, 3, 9, 6]; | ||
| 3 | 10 | ||
| 4 | fn main() { | 11 | fn main() { |
| 5 | let score = include_str!("../../example").lines().fold(0, parse_line); | 12 | let score = include_str!("../../input").lines().fold(0, parse_line); |
| 13 | println!("{}", score); | ||
| 6 | } | 14 | } |
| 7 | 15 | ||
| 8 | fn parse_line(score: usize, line: &str) -> usize { | 16 | fn parse_line(score: usize, line: &str) -> usize { |
| @@ -10,12 +18,19 @@ fn parse_line(score: usize, line: &str) -> usize { | |||
| 10 | let opponent_move = moves.next().unwrap(); | 18 | let opponent_move = moves.next().unwrap(); |
| 11 | let my_move = moves.next().unwrap(); | 19 | let my_move = moves.next().unwrap(); |
| 12 | 20 | ||
| 13 | match opponent_move { | 21 | let my_idx = match opponent_move { |
| 14 | "A" => 1, | 22 | "A" => 0, |
| 15 | "B" => 2, | 23 | "B" => 1, |
| 16 | "C" => 3, | 24 | "C" => 2, |
| 25 | _ => unreachable!(), | ||
| 26 | }; | ||
| 27 | |||
| 28 | let op_idx = match my_move { | ||
| 29 | "X" => 0, | ||
| 30 | "Y" => 1, | ||
| 31 | "Z" => 2, | ||
| 17 | _ => unreachable!(), | 32 | _ => unreachable!(), |
| 18 | }; | 33 | }; |
| 19 | 34 | ||
| 20 | 0 + score | 35 | RPS_RESULTS[3 * op_idx + my_idx] + score |
| 21 | } | 36 | } |
