-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday02.rs
157 lines (141 loc) · 3.78 KB
/
day02.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
use std::{cmp::Ordering, str::FromStr};
#[derive(PartialEq, Clone, Copy, Debug)]
enum Choice {
Rock,
Paper,
Scissor,
}
impl PartialOrd for Choice {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
if self == other {
return Some(Ordering::Equal);
}
match self {
Choice::Rock => {
if *other == Choice::Paper {
Some(Ordering::Less)
} else {
Some(Ordering::Greater)
}
}
Choice::Paper => {
if *other == Choice::Scissor {
Some(Ordering::Less)
} else {
Some(Ordering::Greater)
}
}
Choice::Scissor => {
if *other == Choice::Rock {
Some(Ordering::Less)
} else {
Some(Ordering::Greater)
}
}
}
}
}
impl FromStr for Choice {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"A" | "X" => Ok(Self::Rock),
"B" | "Y" => Ok(Self::Paper),
"C" | "Z" => Ok(Self::Scissor),
_ => Err(format!("Invalid choice {}", s)),
}
}
}
impl Choice {
fn play(&self, other: &Self) -> u32 {
(match self {
Choice::Rock => 1,
Choice::Paper => 2,
Choice::Scissor => 3,
}) + (match self.partial_cmp(other).unwrap() {
Ordering::Less => 0,
Ordering::Equal => 3,
Ordering::Greater => 6,
})
}
}
#[derive(Debug)]
enum Strategy {
Win,
Lose,
Draw,
}
impl FromStr for Strategy {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"X" => Ok(Self::Lose),
"Y" => Ok(Self::Draw),
"Z" => Ok(Self::Win),
_ => Err(format!("Invalid strategy {}", s)),
}
}
}
impl Strategy {
fn choose(&self, other: &Choice) -> Choice {
match self {
Strategy::Win => match other {
Choice::Rock => Choice::Paper,
Choice::Paper => Choice::Scissor,
Choice::Scissor => Choice::Rock,
},
Strategy::Lose => match other {
Choice::Rock => Choice::Scissor,
Choice::Paper => Choice::Rock,
Choice::Scissor => Choice::Paper,
},
Strategy::Draw => *other,
}
}
}
pub fn part_one(input: &str) -> u32 {
input
.lines()
.map(|l| {
let mut split = l.split(' ');
let elf = split.next().unwrap().parse::<Choice>().unwrap();
let me = split.next().unwrap().parse::<Choice>().unwrap();
me.play(&elf)
})
.sum()
}
pub fn part_two(input: &str) -> u32 {
input
.lines()
.map(|l| {
let mut split = l.split(' ');
let elf = split.next().unwrap().parse::<Choice>().unwrap();
let me = split
.next()
.unwrap()
.parse::<Strategy>()
.unwrap()
.choose(&elf);
me.play(&elf)
})
.sum()
}
#[cfg(test)]
mod tests {
#[test]
fn part_one_example() {
assert_eq!(super::part_one(include_str!("input/day02_example.txt")), 15);
}
#[test]
fn part_one() {
assert_eq!(super::part_one(include_str!("input/day02.txt")), 14827);
}
#[test]
fn part_two_example() {
assert_eq!(super::part_two(include_str!("input/day02_example.txt")), 12);
}
#[test]
fn part_two() {
assert_eq!(super::part_two(include_str!("input/day02.txt")), 13889);
}
}