generated from fspoettel/advent-of-code-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
02.rs
128 lines (100 loc) · 2.69 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
advent_of_code::solution!(2);
pub fn part_one(input: &str) -> Option<u32> {
let mut sum = 0;
for line in input.lines() {
let game = parse_game(line);
if is_game_possible(&game) {
sum += game.id;
}
}
Some(sum)
}
pub fn part_two(input: &str) -> Option<u32> {
let mut sum = 0;
for line in input.lines() {
let game = parse_game(line);
let minimum_set = get_minimum_set(&game.sets);
let minimum_set_power = minimum_set.red * minimum_set.green * minimum_set.blue;
sum += minimum_set_power;
}
Some(sum)
}
fn parse_game(line: &str) -> Game {
let mut line_iterator = line.split(": ");
let game_string = line_iterator.next().unwrap();
let sets_string = line_iterator.next().unwrap();
let id = game_string
.split(' ')
.last()
.unwrap()
.parse::<u32>()
.unwrap();
let sets = sets_string.split("; ").map(parse_set).collect();
Game { id, sets }
}
fn parse_set(set_string: &str) -> GameSet {
let mut set = GameSet {
red: 0,
green: 0,
blue: 0,
};
for color_string in set_string.split(", ") {
let mut color_iterator = color_string.split(' ');
let color_value = color_iterator.next().unwrap().parse::<u32>().unwrap();
match color_iterator.next().unwrap() {
"red" => set.red = color_value,
"green" => set.green = color_value,
"blue" => set.blue = color_value,
_ => panic!("Unknown color"),
}
}
set
}
fn is_game_possible(game: &Game) -> bool {
game.sets.iter().all(is_set_possible)
}
fn is_set_possible(set: &GameSet) -> bool {
set.red <= 12 && set.green <= 13 && set.blue <= 14
}
fn get_minimum_set(sets: &Vec<GameSet>) -> GameSet {
let mut minimum_set = GameSet {
red: 0,
green: 0,
blue: 0,
};
for set in sets {
if set.red > minimum_set.red {
minimum_set.red = set.red;
}
if set.green > minimum_set.green {
minimum_set.green = set.green;
}
if set.blue > minimum_set.blue {
minimum_set.blue = set.blue;
}
}
minimum_set
}
struct Game {
id: u32,
sets: Vec<GameSet>,
}
struct GameSet {
red: u32,
green: u32,
blue: u32,
}
#[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));
}
}