generated from fspoettel/advent-of-code-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path15.rs
247 lines (206 loc) · 7.4 KB
/
15.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
use std::collections::{HashMap, HashSet};
advent_of_code::solution!(15);
#[derive(Debug, Eq, Hash, PartialEq, Clone, Copy)]
struct Pos(i32, i32);
fn sum_pos(pos1: Pos, pos2: Pos) -> Pos {
Pos(pos1.0 + pos2.0, pos1.1 + pos2.1)
}
fn gps_coord(pos: Pos) -> i32 {
(100 * pos.1) + pos.0
}
fn gps_coord2(pos: Pos) -> i32 {
(100 * pos.1) + pos.0
}
pub fn part_one(input: &str) -> Option<i32> {
let (map, directions) = input.split_once("\n\n").unwrap();
let mut walls = HashSet::new();
let mut rocks = HashSet::new();
let mut guard = Pos(0, 0);
for (y, row) in map.lines().enumerate() {
for (x, el) in row.chars().enumerate() {
match el {
'#' => {
walls.insert(Pos(x as i32, y as i32));
}
'O' => {
rocks.insert(Pos(x as i32, y as i32));
}
'@' => guard = Pos(x as i32, y as i32),
_ => {}
}
}
}
for dir in directions.trim().replace("\n", "").chars() {
let delta_pos = match dir {
'<' => Pos(-1, 0),
'^' => Pos(0, -1),
'>' => Pos(1, 0),
'v' => Pos(0, 1),
_ => panic!("Woah that's not a direction {}", dir),
};
let mut should_move = false;
let mut rocks_to_move: HashSet<Pos> = HashSet::new();
// Find what to move
let mut next_candidate = sum_pos(guard, delta_pos);
'l: loop {
if walls.contains(&next_candidate) {
break 'l;
} else if rocks.contains(&next_candidate) {
rocks_to_move.insert(next_candidate);
} else {
should_move = true;
break 'l;
}
next_candidate = sum_pos(next_candidate, delta_pos)
}
// Move them
if should_move {
guard = sum_pos(guard, delta_pos);
let mut new_rocks = HashSet::new();
for rock in rocks_to_move {
rocks.remove(&rock);
new_rocks.insert(sum_pos(rock, delta_pos));
}
rocks.extend(new_rocks);
}
}
let mut result = 0;
for rock in rocks {
result += gps_coord(rock)
}
Some(result)
}
pub fn part_two(input: &str) -> Option<i32> {
let (map, directions) = input.split_once("\n\n").unwrap();
let mut walls = HashSet::new();
let mut rocks = HashSet::new();
let mut rock_connections = HashMap::new();
let mut guard = Pos(0, 0);
for (y, row) in map.lines().enumerate() {
for (x, el) in row.chars().enumerate() {
match el {
'#' => {
walls.insert(Pos(x as i32 * 2, y as i32));
walls.insert(Pos((x as i32 * 2) + 1, y as i32));
}
'O' => {
rocks.insert(Pos(x as i32 * 2, y as i32));
rocks.insert(Pos((x as i32 * 2) + 1, y as i32));
rock_connections.insert(
Pos(x as i32 * 2, y as i32),
Pos((x as i32 * 2) + 1, y as i32),
);
rock_connections.insert(
Pos((x as i32 * 2) + 1, y as i32),
Pos(x as i32 * 2, y as i32),
);
}
'@' => guard = Pos(x as i32 * 2, y as i32),
_ => {}
}
}
}
for dir in directions.trim().replace("\n", "").chars() {
let (delta_pos, is_vert) = match dir {
'<' => (Pos(-1, 0), false),
'^' => (Pos(0, -1), true),
'>' => (Pos(1, 0), false),
'v' => (Pos(0, 1), true),
_ => panic!("Woah that's not a direction {}", dir),
};
let mut should_move = false;
let mut rocks_to_move: HashSet<Pos> = HashSet::new();
// Find what to move
let mut move_frontier = HashSet::from_iter(vec![sum_pos(guard, delta_pos)]);
'l: loop {
let mut new_move_frontier = HashSet::new();
let mut rocks_added = false;
for movement in move_frontier {
if walls.contains(&movement) {
break 'l;
} else if rocks.contains(&movement) {
rocks_added = true;
if is_vert {
let connected_rock = rock_connections.get(&movement).unwrap().to_owned();
new_move_frontier.insert(sum_pos(movement, delta_pos));
new_move_frontier.insert(sum_pos(connected_rock, delta_pos));
rocks_to_move.insert(movement);
rocks_to_move.insert(connected_rock);
} else {
new_move_frontier.insert(sum_pos(movement, delta_pos));
rocks_to_move.insert(movement);
}
} else {
new_move_frontier.insert(movement);
};
}
if !rocks_added {
should_move = true;
break 'l;
}
move_frontier = new_move_frontier;
}
// Move them
if should_move {
guard = sum_pos(guard, delta_pos);
let mut already_moved = HashSet::new();
let mut new_rocks = HashSet::new();
let mut new_connections = HashMap::new();
for rock in rocks_to_move {
if already_moved.contains(&rock) {
continue;
}
let connected_rock = rock_connections.get(&rock).unwrap().to_owned();
rocks.remove(&rock);
rocks.remove(&connected_rock);
rock_connections.remove(&rock);
rock_connections.remove(&connected_rock);
already_moved.insert(rock);
already_moved.insert(connected_rock);
let new_connected_rock = sum_pos(connected_rock, delta_pos);
let new_rock = sum_pos(rock, delta_pos);
new_rocks.insert(new_rock);
new_rocks.insert(new_connected_rock);
new_connections.insert(new_rock, new_connected_rock);
new_connections.insert(new_connected_rock, new_rock);
}
rock_connections.extend(new_connections);
rocks.extend(new_rocks);
}
}
let mut sig_rocks = HashSet::new();
let mut processed_rocks = HashSet::new();
for rock in rocks {
if processed_rocks.contains(&rock) {
continue;
}
let connected_rock = rock_connections.get(&rock).unwrap().to_owned();
let l_rock = if rock.0 < connected_rock.0 {
rock
} else {
connected_rock
};
sig_rocks.insert(l_rock);
processed_rocks.insert(rock);
processed_rocks.insert(connected_rock);
}
let mut result = 0;
for rock in sig_rocks {
result += gps_coord2(rock)
}
Some(result)
}
#[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(10092));
}
#[test]
fn test_part_two() {
let result = part_two(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, Some(9021));
}
}