summaryrefslogtreecommitdiffstats
path: root/2022/day2/part1/src/main.rs
blob: 81b20dfdee3d40dbe680ce99bc5df18f9012f2dc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
///         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!("../../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
}