-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
73 lines (63 loc) · 1.97 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
use std::cmp::max;
struct Pick {
r: u32,
g: u32,
b: u32,
}
struct Game {
id: u32,
picks: Vec<Pick>,
}
fn main() {
let data: Vec<Game> = include_str!("input.txt")
.lines()
.map(|l| {
// Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green
let (game, picks) = l.split_once(':').unwrap();
let game_id: u32 = game.split_once(' ').unwrap().1.parse().unwrap();
return Game {
id: game_id,
picks: picks.split(';')
.map(|pick|
pick.split(',')
.map(|pick| pick.trim().split_once(' ').unwrap())
.fold(Pick { r: 0, g: 0, b: 0 }, |mut acc, (val, color)| {
let v: u32 = val.parse().unwrap();
match color {
"red" => acc.r += v,
"green" => acc.g += v,
"blue" => acc.b += v,
_ => panic!()
}
acc
}))
.collect(),
};
})
.collect();
let start = Pick {
r: 12,
g: 13,
b: 14,
};
let part1: u32 = data.iter()
.filter(|game| {
game.picks.iter().all(|pick| pick.r <= start.r && pick.b <= start.b && pick.g <= start.g)
})
.map(|g| g.id)
.sum();
println!("{part1}");
let part2: u32 = data.iter()
.map(|game| {
game.picks.iter()
.fold(Pick { r: 0, g: 0, b: 0 }, |mut acc, p| {
acc.r = max(acc.r, p.r);
acc.g = max(acc.g, p.g);
acc.b = max(acc.b, p.b);
acc
})
})
.map(|p| p.b * p.r * p.g)
.sum();
println!("{part2}");
}