summaryrefslogtreecommitdiff
path: root/2022/Rust/src/day2.rs
blob: adfcb96f60a4bf9221ae7f13f8b0621156b11c28 (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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// A -> Rock
// B -> Paper
// C -> Scissors
// D -> Rock

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

impl From<&str> for Hand {
    fn from(s: &str) -> Self {
        match s {
            "A" | "X" => Hand::Rock(1),
            "B" | "Y" => Hand::Paper(2),
            "C" | "Z" => Hand::Scissors(3),
            _ => panic!("Should've been a valid hand"),
        }
    }
}

fn solve_part_one(data: &str) -> u32 {
    let mut score = 0;
    for line in data.lines() {
        let (h1, h2) = line.split_once(" ").unwrap();
        match Hand::from(h1) {
            Hand::Rock(_) => match Hand::from(h2) {
                Hand::Rock(x) => {
                    score += x + 3;
                }
                Hand::Paper(x) => {
                    score += x + 6;
                }
                Hand::Scissors(x) => {
                    score += x;
                }
            },
            Hand::Paper(_) => match Hand::from(h2) {
                Hand::Rock(x) => {
                    score += x;
                }
                Hand::Paper(x) => {
                    score += x + 3;
                }
                Hand::Scissors(x) => {
                    score += x + 6;
                }
            },
            Hand::Scissors(_) => match Hand::from(h2) {
                Hand::Rock(x) => {
                    score += x + 6;
                }
                Hand::Paper(x) => {
                    score += x;
                }
                Hand::Scissors(x) => {
                    score += x + 3;
                }
            },
        }
    }
    score
}

fn solve_part_two(data: &str) -> u32 {
    let mut score = 0;
    for line in data.lines() {
        let (h1, h2) = line.split_once(" ").unwrap();
        match Hand::from(h2) {
            Hand::Rock(_) => match Hand::from(h1) {
                Hand::Rock(_) => {
                    score += 3;
                }
                Hand::Paper(_) => {
                    score += 1;
                }
                Hand::Scissors(_) => {
                    score += 2;
                }
            },
            Hand::Paper(_) => match Hand::from(h1) {
                Hand::Rock(x) => {
                    score += x + 3;
                }
                Hand::Paper(x) => {
                    score += x + 3;
                }
                Hand::Scissors(x) => {
                    score += x + 3;
                }
            },
            Hand::Scissors(_) => match Hand::from(h1) {
                Hand::Rock(_) => {
                    score += 2 + 6;
                }
                Hand::Paper(_) => {
                    score += 3 + 6;
                }
                Hand::Scissors(_) => {
                    score += 1 + 6;
                }
            },
        }
    }
    score
}

fn main() {
    let data_test = include_str!("../input/day2.test");
    let data_prod = include_str!("../input/day2.prod");
    println!("{}", solve_part_one(data_test));
    println!("{}", solve_part_one(data_prod));
    println!("{}", solve_part_two(data_test));
    println!("{}", solve_part_two(data_prod));
}