generated from fspoettel/advent-of-code-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
02.rs
138 lines (120 loc) · 3.29 KB
/
02.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
use std::str::FromStr;
use tinyvec::TinyVec;
advent_of_code::solution!(2);
pub fn part_one(input: &str) -> Option<u32> {
Some(
input
.lines()
.filter_map(|line| line.parse::<Game>().ok())
.filter(|game| {
game.possible(Colors {
red: 12,
green: 13,
blue: 14,
})
})
.map(|game| game.id)
.sum(),
)
}
pub fn part_two(input: &str) -> Option<u32> {
Some(
input
.lines()
.filter_map(|line| line.parse::<Game>().ok())
.filter_map(|game| {
game.reveals
.iter()
.copied()
.reduce(|acc, reveal| acc.maximum(reveal))
})
.map(Colors::power)
.sum(),
)
}
#[derive(Copy, Clone, Default, Debug, Eq, PartialEq, PartialOrd)]
struct Colors {
red: u32,
green: u32,
blue: u32,
}
#[derive(Clone, Debug, Eq, PartialEq)]
struct Game {
id: u32,
reveals: TinyVec<[Colors; 10]>,
}
impl Game {
pub fn possible(&self, bag: Colors) -> bool {
self.reveals
.iter()
.all(|reveal| reveal.possible_reveal(bag))
}
}
impl Colors {
pub fn possible_reveal(self, bag: Colors) -> bool {
self.red <= bag.red && self.green <= bag.green && self.blue <= bag.blue
}
pub fn maximum(self, other: Self) -> Self {
Self {
red: self.red.max(other.red),
green: self.green.max(other.green),
blue: self.blue.max(other.blue),
}
}
pub fn power(self) -> u32 {
self.red * self.green * self.blue
}
}
struct ParseGameErr;
impl FromStr for Game {
type Err = ParseGameErr;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let (game_id, reveal_str) = s.split_once(':').ok_or(ParseGameErr)?;
if game_id.len() < 6 {
return Err(ParseGameErr);
}
Ok(Self {
id: game_id[5..].parse::<u32>().map_err(|_| ParseGameErr)?,
reveals: reveal_str
.trim()
.split(';')
.map(|reveal| reveal.parse::<Colors>())
.collect::<Result<_, _>>()?,
})
}
}
impl FromStr for Colors {
type Err = ParseGameErr;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let mut ret = Colors {
red: 0,
green: 0,
blue: 0,
};
for part in s.split(',') {
let (num_str, color) = part.trim().split_once(' ').ok_or(ParseGameErr)?;
let num = num_str.parse::<u32>().map_err(|_| ParseGameErr)?;
match color {
"red" => ret.red += num,
"green" => ret.green += num,
"blue" => ret.blue += num,
_ => return Err(ParseGameErr),
};
}
Ok(ret)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_part_one() {
let result = part_one(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, Some(8));
}
#[test]
fn test_part_two() {
let result = part_two(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, Some(2286));
}
}