summaryrefslogtreecommitdiffstats
path: root/src/day_02.rs
blob: a27fbc8a7457cbd9602ad2f707894d4b84113854 (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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
use std::str::FromStr;

use anyhow::{anyhow, Result};

use crate::{Problem, Solution};

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
struct Game {
    shapes: (Shape, Shape),
    outcome: Outcome,
    score: usize,
}

impl Game {
    fn new(a: Shape, b: Shape, outcome: Outcome) -> Self {
        Self {
            shapes: (a, b),
            outcome,
            score: b as usize + outcome as usize,
        }
    }
}

impl From<(Shape, Shape)> for Game {
    fn from((a, b): (Shape, Shape)) -> Self {
        Self::new(a, b, (a, b).into())
    }
}

impl From<(Shape, Outcome)> for Game {
    fn from((a, outcome): (Shape, Outcome)) -> Self {
        Self::new(a, (a, outcome).into(), outcome)
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u8)]
enum Outcome {
    Loss = 0,
    Draw = 3,
    Win = 6,
}

impl From<(Shape, Shape)> for Outcome {
    fn from((a, b): (Shape, Shape)) -> Self {
        match (a, b) {
            (Shape::Rock, Shape::Rock) => Outcome::Draw,
            (Shape::Rock, Shape::Paper) => Outcome::Win,
            (Shape::Rock, Shape::Scissors) => Outcome::Loss,
            (Shape::Paper, Shape::Rock) => Outcome::Loss,
            (Shape::Paper, Shape::Paper) => Outcome::Draw,
            (Shape::Paper, Shape::Scissors) => Outcome::Win,
            (Shape::Scissors, Shape::Rock) => Outcome::Win,
            (Shape::Scissors, Shape::Paper) => Outcome::Loss,
            (Shape::Scissors, Shape::Scissors) => Outcome::Draw,
        }
    }
}

impl FromStr for Outcome {
    type Err = anyhow::Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "X" => Ok(Outcome::Loss),
            "Y" => Ok(Outcome::Draw),
            "Z" => Ok(Outcome::Win),
            s => Err(anyhow!("Unknown symbol: {}", s)),
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Shape {
    Rock = 1,
    Paper = 2,
    Scissors = 3,
}

impl From<(Shape, Outcome)> for Shape {
    fn from((shape, outcome): (Shape, Outcome)) -> Self {
        match (shape, outcome) {
            (Shape::Rock, Outcome::Draw) => Shape::Rock,
            (Shape::Rock, Outcome::Win) => Shape::Paper,
            (Shape::Rock, Outcome::Loss) => Shape::Scissors,
            (Shape::Paper, Outcome::Loss) => Shape::Rock,
            (Shape::Paper, Outcome::Draw) => Shape::Paper,
            (Shape::Paper, Outcome::Win) => Shape::Scissors,
            (Shape::Scissors, Outcome::Win) => Shape::Rock,
            (Shape::Scissors, Outcome::Loss) => Shape::Paper,
            (Shape::Scissors, Outcome::Draw) => Shape::Scissors,
        }
    }
}

impl FromStr for Shape {
    type Err = anyhow::Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "A" | "X" => Ok(Shape::Rock),
            "B" | "Y" => Ok(Shape::Paper),
            "C" | "Z" => Ok(Shape::Scissors),
            s => Err(anyhow!("Unknown symbol: {}", s)),
        }
    }
}

pub struct Day02;

impl Problem for Day02 {
    const DAY: u8 = 3;

    const INPUT: &'static str = include_str!("../input/day_2.txt");
}

impl Solution for Day02 {
    type Answer1 = usize;

    type Answer2 = usize;

    fn part_1(input: &str) -> Result<Self::Answer1, anyhow::Error> {
        input.lines().try_fold(0, |acc, l| {
            let (a, b) = l
                .split_once(' ')
                .ok_or_else(|| anyhow!("Missing deliminator"))?;

            let a: Shape = a.parse()?;
            let b: Shape = b.parse()?;
            let game: Game = (a, b).into();

            Ok(acc + game.score)
        })
    }

    fn part_2(input: &str) -> Result<Self::Answer2, anyhow::Error> {
        input.lines().try_fold(0, |acc, l| {
            let (a, outcome) = l
                .split_once(' ')
                .ok_or_else(|| anyhow!("Missing deliminator"))?;

            let a: Shape = a.parse()?;
            let outcome: Outcome = outcome.parse()?;
            let game: Game = (a, outcome).into();

            Ok(acc + game.score)
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    const INPUT: &str = indoc::indoc! {"
        A Y
        B X
        C Z
    "};

    #[test]
    fn test_part_1_example() -> Result<()> {
        Ok(assert_eq!(15, Day02::part_1(INPUT)?))
    }

    #[test]
    fn test_part_2_example() -> Result<()> {
        Ok(assert_eq!(12, Day02::part_2(INPUT)?))
    }
}