aboutsummaryrefslogtreecommitdiff
path: root/2022/rust/day02/src/main.rs
blob: fe1494f591e49a815479af7d62c00c527eb1e9bc (plain) (blame)
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::env;
use std::fs;

fn main() {
    let args = env::args().nth(1).expect("missing input");
    let input = fs::read_to_string(args).unwrap();
    let moves = input.trim().split("\n").map(|x| {
        let mut vec = x.split(" ");
        return (vec.next().unwrap(), vec.next().unwrap());
    }).collect::<Vec<(&str, &str)>>();

    let mut sum = 0;
    // note: without the iter "moves" is implicitly moved into the loop
    for i in moves.iter() {
        match i {
            // (x, "X") => sum += 1,
            ("A", "X") => sum += 3 + 1,
            ("B", "X") => sum += 0 + 1,
            ("C", "X") => sum += 6 + 1,
            // (x, "Y") => sum += 2,
            ("A", "Y") => sum += 6 + 2,
            ("B", "Y") => sum += 3 + 2,
            ("C", "Y") => sum += 0 + 2,
            // (x, "Z") => sum += 3,
            ("A", "Z") => sum += 0 + 3,
            ("B", "Z") => sum += 6 + 3,
            ("C", "Z") => sum += 3 + 3,
            _ => ()
        }
    }
    println!("{}", sum);

    sum = 0;
    for i in moves.iter() {
        match i {
            // (x, "X") => sum += 1,
            ("A", "X") => sum += 3 + 0,
            ("B", "X") => sum += 1 + 0,
            ("C", "X") => sum += 2 + 0,
            // (x, "Y") => sum += 2,
            ("A", "Y") => sum += 1 + 3,
            ("B", "Y") => sum += 2 + 3,
            ("C", "Y") => sum += 3 + 3,
            // (x, "Z") => sum += 3,
            ("A", "Z") => sum += 2 + 6,
            ("B", "Z") => sum += 3 + 6,
            ("C", "Z") => sum += 1 + 6,
            _ => ()
        }
    }
    println!("{}", sum);
}