-
Notifications
You must be signed in to change notification settings - Fork 0
/
02.rs
133 lines (116 loc) · 3.42 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
static INPUT_TXT: &str = include_str!("../../input/02.txt");
fn main() {
println!("Part 1: {}", part_1(INPUT_TXT));
println!("Part 2: {}", part_2(INPUT_TXT));
}
enum Colour {
Blue,
Green,
Red,
}
impl From<&str> for Colour {
fn from(s: &str) -> Self {
match s {
"blue" => Self::Blue,
"green" => Self::Green,
"red" => Self::Red,
_ => panic!("Unknown colour {s}"),
}
}
}
struct Block {
num: i32,
colour: Colour,
}
impl From<&str> for Block {
fn from(s: &str) -> Self {
let (num, c) = s.trim().split_once(' ').unwrap();
let num = num.parse::<i32>().unwrap();
let colour = Colour::from(c);
Self { num, colour }
}
}
struct Game {
id: i32,
blocks: Vec<Vec<Block>>,
}
impl From<&str> for Game {
fn from(s: &str) -> Self {
let (game, blocks_str) = s.split_once(':').unwrap();
let id = game.strip_prefix("Game ").unwrap().parse::<i32>().unwrap();
let blocks = blocks_str
.split(';')
.map(|selection| selection.trim().split(',').map(Block::from).collect())
.collect();
Self { id, blocks }
}
}
impl Game {
fn valid(&self) -> bool {
self.blocks.iter().all(|selection| {
let (blue, green, red) =
selection
.iter()
.fold((0, 0, 0), |(blue, green, red), block| match block.colour {
Colour::Blue => (blue + block.num, green, red),
Colour::Green => (blue, green + block.num, red),
Colour::Red => (blue, green, red + block.num),
});
blue <= 14 && green <= 13 && red <= 12
})
}
fn max(&self) -> i32 {
let (blue, green, red) =
self.blocks
.iter()
.flatten()
.fold((0, 0, 0), |(blue, green, red), block| match block.colour {
Colour::Blue => (blue.max(block.num), green, red),
Colour::Green => (blue, green.max(block.num), red),
Colour::Red => (blue, green, red.max(block.num)),
});
blue * green * red
}
}
struct Games(Vec<Game>);
impl From<&str> for Games {
fn from(s: &str) -> Self {
Self(s.trim().split('\n').map(Game::from).collect())
}
}
impl Games {
fn valid(&self) -> impl Iterator<Item = i32> + '_ {
self.0
.iter()
.filter(|game| game.valid())
.map(|game| game.id)
}
fn max(&self) -> impl Iterator<Item = i32> + '_ {
self.0.iter().map(Game::max)
}
}
fn part_1(input: &str) -> i32 {
Games::from(input).valid().sum()
}
fn part_2(input: &str) -> i32 {
Games::from(input).max().sum()
}
#[cfg(test)]
mod tests {
use super::*;
static INPUT: &str = "Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green
Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue
Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red
Game 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red
Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green";
#[test]
fn test_part_1() {
assert_eq!(part_1(INPUT), 8);
assert_eq!(part_1(INPUT_TXT), 2600);
}
#[test]
fn test_part_2() {
assert_eq!(part_2(INPUT), 2286);
assert_eq!(part_2(INPUT_TXT), 86036);
}
}