-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
172 lines (147 loc) · 4.06 KB
/
main.rs
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
171
172
pub fn main() {
let data = include_str!("input.txt");
println!("Part 1: {}", part_one(data).unwrap());
println!("Part 2: {}", part_two(data).unwrap());
}
fn part_one(data: &str) -> color_eyre::Result<u64> {
let mut total = 0;
for round in data.lines().map(Round::from_moves_str) {
total += round?.points();
}
Ok(total)
}
fn part_two(data: &str) -> color_eyre::Result<u64> {
let mut total = 0;
for round in data.lines().map(Round::from_outcome_str) {
total += round?.points();
}
Ok(total)
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Move {
Rock,
Paper,
Scissors,
}
#[derive(Debug)]
struct Round {
opp: Move,
ours: Move,
}
#[derive(Debug, Clone, Copy)]
enum Outcome {
Win,
Draw,
Loss,
}
impl Move {
fn points(&self) -> u64 {
match self {
Move::Rock => 1,
Move::Paper => 2,
Move::Scissors => 3,
}
}
fn beats(&self) -> Self {
match self {
Move::Rock => Move::Scissors,
Move::Paper => Move::Rock,
Move::Scissors => Move::Paper,
}
}
fn beaten_by(&self) -> Self {
match self {
Move::Rock => Move::Paper,
Move::Paper => Move::Scissors,
Move::Scissors => Move::Rock,
}
}
}
impl Round {
fn from_moves_str(line: &str) -> color_eyre::Result<Self> {
let mut parts = line.chars();
let (Some(opp), Some(' '), Some(ours), None) = (parts.next(), parts.next(), parts.next(), parts.next()) else {
return Err(color_eyre::eyre::eyre!("was expecting opp<SP>ours<EOL>, got {line}"));
};
let opp = opp.try_into()?;
let ours = ours.try_into()?;
Ok(Self::from_moves(opp, ours))
}
fn from_moves(opp: Move, ours: Move) -> Self {
Self { opp, ours }
}
fn from_outcome_str(line: &str) -> color_eyre::Result<Self> {
let mut parts = line.chars();
let (Some(opp), Some(' '), Some(outcome), None) = (parts.next(), parts.next(), parts.next(), parts.next()) else {
return Err(color_eyre::eyre::eyre!("was expecting opp<SP>outcome<EOL>, got {line}"));
};
let opp = opp.try_into()?;
let outcome = outcome.try_into()?;
Ok(Self::from_outcome(opp, outcome))
}
fn from_outcome(opp: Move, outcome: Outcome) -> Self {
let ours = match outcome {
Outcome::Win => opp.beaten_by(),
Outcome::Draw => opp,
Outcome::Loss => opp.beats(),
};
Self { opp, ours }
}
fn outcome(&self) -> Outcome {
if self.ours.beats() == self.opp {
Outcome::Win
} else if self.opp.beats() == self.ours {
Outcome::Loss
} else {
Outcome::Draw
}
}
fn points(&self) -> u64 {
self.ours.points() + self.outcome().points()
}
}
impl Outcome {
fn points(&self) -> u64 {
match self {
Outcome::Win => 6,
Outcome::Draw => 3,
Outcome::Loss => 0,
}
}
}
impl TryFrom<char> for Move {
type Error = color_eyre::Report;
fn try_from(c: char) -> Result<Self, Self::Error> {
match c {
'A' | 'X' => Ok(Move::Rock),
'B' | 'Y' => Ok(Move::Paper),
'C' | 'Z' => Ok(Move::Scissors),
_ => Err(color_eyre::eyre::eyre!("not a valid move: {c:?}")),
}
}
}
impl TryFrom<char> for Outcome {
type Error = color_eyre::Report;
fn try_from(c: char) -> Result<Self, Self::Error> {
match c {
'X' => Ok(Outcome::Loss),
'Y' => Ok(Outcome::Draw),
'Z' => Ok(Outcome::Win),
_ => Err(color_eyre::eyre::eyre!("not a valid outcome: {c:?}")),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn one() {
let data = include_str!("test.txt");
assert_eq!(15, part_one(data).unwrap());
}
#[test]
fn two() {
let data = include_str!("test.txt");
assert_eq!(12, part_two(data).unwrap());
}
}