aoc/year2022/day02.rs
1//! # Rock Paper Scissors
2//!
3//! With so few combinations it's possible to precompute the values for each scenario by hand
4//! then quickly look them up for each game.
5
6/// Map each line from one of the 9 possible combinations ("A", "B" or "C" followed by "X", "Y" or "Z")
7/// to between 0 and 8 inclusive.
8pub fn parse(input: &str) -> Vec<usize> {
9 input.as_bytes().chunks_exact(4).map(|c| (3 * (c[0] - b'A') + c[2] - b'X') as usize).collect()
10}
11
12/// Map each index to a score using a small precomputed lookup table.
13pub fn part1(input: &[usize]) -> u32 {
14 let score = [4, 8, 3, 1, 5, 9, 7, 2, 6];
15 input.iter().map(|&i| score[i]).sum()
16}
17
18/// Map each index to a (different) score using a second small precomputed lookup table.
19pub fn part2(input: &[usize]) -> u32 {
20 let score = [3, 4, 8, 1, 5, 9, 2, 6, 7];
21 input.iter().map(|&i| score[i]).sum()
22}