-
Notifications
You must be signed in to change notification settings - Fork 0
/
02.rs
73 lines (66 loc) · 1.9 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
#![feature(test)]
type Input = Vec<Game>;
type Game = Vec<CubeCounts>;
#[derive(Debug, Clone, Copy, Default)]
struct CubeCounts {
red: u32,
green: u32,
blue: u32,
}
fn setup(input: &str) -> Input {
input
.lines()
.map(|line| {
line.split(": ")
.nth(1)
.unwrap()
.split("; ")
.map(|round| {
round
.split(", ")
.fold(CubeCounts::default(), |mut acc, round| {
let mut round = round.split(' ');
let count = round.next().unwrap().parse().unwrap();
let color = round.next().unwrap();
match color {
"red" => acc.red = count,
"green" => acc.green = count,
"blue" => acc.blue = count,
_ => panic!(),
}
acc
})
})
.collect()
})
.collect()
}
fn min_config(game: &Game) -> CubeCounts {
game.iter()
.fold(CubeCounts::default(), |acc, &round| CubeCounts {
red: acc.red.max(round.red),
green: acc.green.max(round.green),
blue: acc.blue.max(round.blue),
})
}
fn part1(input: &Input) -> usize {
input
.iter()
.enumerate()
.filter(|(_, game)| {
let min = min_config(game);
min.red <= 12 && min.green <= 13 && min.blue <= 14
})
.map(|(i, _)| i + 1)
.sum()
}
fn part2(input: &Input) -> u32 {
input
.iter()
.map(|game| {
let min = min_config(game);
min.red * min.green * min.blue
})
.sum()
}
aoc::main!(2023, 2, ex: 1);