summaryrefslogtreecommitdiffstats
path: root/2022/day2/part2/src/main.rs
blob: 86c893b824c3df7e615ac59302b3d2be9af3d1ac (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
37
38
39
///         Rock     : 1
///         Paper    : 2
///         Scissors : 3
///         opponent
///         R   P   S
/// me      A   B   C
/// _ L X   3   1   2
/// _ D Y   4   5   6
/// _ W Z   8   9   7
///
/// idx = 3 * op_idx + my_idx
const RPS_RESULTS: [usize; 9] = [3, 1, 2, 4, 5, 6, 8, 9, 7];

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
}