aboutsummaryrefslogtreecommitdiffstats
path: root/AoC2022/02/solver.rs
blob: 1f9a394866a0cccbd55129f8928cd793e2f3a1e4 (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
40
41
42
43
44
45
46
47
48
49
50
51
52
use std::fs;
use std::io::{self, BufRead};

#[derive(Debug, PartialEq)]
enum Hand {
    Rock,
    Paper,
    Scissors,
}

fn translate(play: &str) -> Hand {
    match play {
        "A" | "X" => Hand::Rock,
        "B" | "Y" => Hand::Paper,
        "C" | "Z" => Hand::Scissors,
        &_ => panic!(),
    }
}

fn weight(play: Hand) -> u32 {
    match play {
        Hand::Rock => 1,
        Hand::Paper => 2,
        Hand::Scissors => 3,
    }
}

fn fight(my: Hand, other: Hand) -> u32 {
    match (my, other) {
        (a, b) if a == b => 3,
        (Hand::Rock, Hand::Scissors)
        | (Hand::Scissors, Hand::Paper)
        | (Hand::Paper, Hand::Rock) => 6,
        _ => 0,
    }
}

fn main() -> std::io::Result<()> {
    let file = fs::File::open("input").unwrap();
    let lines = io::BufReader::new(file).lines();
    let mut score = 0;
    for line in lines {
        if let [ot, my] = line?.split_whitespace().collect::<Vec<_>>()[..] {
            score += fight(translate(my), translate(ot)) + weight(translate(my));
        } else {
            todo!()
        };
    }

    println!("{}", score);
    Ok(())
}