generated from fspoettel/advent-of-code-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path15.rs
228 lines (218 loc) · 6.67 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
advent_of_code::solution!(15);
fn find_empty(map: &[Vec<char>], robot: (i32, i32), dir: (i32, i32)) -> Option<(i32, i32)> {
let mut robot = robot;
loop {
robot.0 += dir.0;
robot.1 += dir.1;
if map[robot.0 as usize][robot.1 as usize] == '#' {
return None;
}
if map[robot.0 as usize][robot.1 as usize] == '.' {
return Some(robot);
}
}
}
// If can push, return all the positions that can be pushed
// If can't push, return None
fn push_recursive(
map: &mut [Vec<char>],
pos: (i32, i32),
dir: (i32, i32),
) -> Option<Vec<(i32, i32)>> {
match map[pos.0 as usize][pos.1 as usize] {
'#' => return None,
'.' => return Some(Vec::new()),
_ => (),
}
let mut positions: Vec<(i32, i32)> = Vec::new();
if dir.0 != 0 {
// Up or down
positions.push(pos);
match map[pos.0 as usize][pos.1 as usize] {
'[' => positions.push((pos.0 + 0, pos.1 + 1)),
']' => positions.push((pos.0 + 0, pos.1 - 1)),
_ => (),
}
let mut recursive_positions: Vec<(i32, i32)> = Vec::new();
for position in &positions {
let next = (position.0 + dir.0, position.1 + dir.1);
let res = push_recursive(map, next, dir);
if res.is_none() {
return None;
}
recursive_positions.extend(res.unwrap());
}
positions.extend(recursive_positions);
} else {
// Left or right
positions.push(pos);
let mut current = pos;
loop {
current = (current.0, current.1 + dir.1);
match map[current.0 as usize][current.1 as usize] {
'#' => return None,
'.' => break,
_ => (),
}
positions.push(current);
}
}
Some(positions)
}
pub fn part_one(input: &str) -> Option<u32> {
let mut parts = input.split("\n\n");
let mut map: Vec<Vec<char>> = parts
.next()
.unwrap()
.lines()
.map(|line| line.chars().collect())
.collect();
let instructions = parts
.next()
.unwrap()
.chars()
.filter_map(|c| match c {
'<' => Some((0, -1)),
'>' => Some((0, 1)),
'^' => Some((-1, 0)),
'v' => Some((1, 0)),
_ => None,
})
.collect::<Vec<_>>();
let mut robot = (0, 0);
for (i, row) in map.iter().enumerate() {
for (j, c) in row.iter().enumerate() {
if *c == '@' {
robot = (i as i32, j as i32);
}
}
}
map[robot.0 as usize][robot.1 as usize] = '.';
for instruction in instructions {
let empty = find_empty(&map, robot, instruction);
if empty.is_none() {
continue;
}
let next = (robot.0 + instruction.0, robot.1 + instruction.1);
if map[next.0 as usize][next.1 as usize] == '.' {
robot = next;
} else if map[next.0 as usize][next.1 as usize] == 'O' {
robot = next;
map[next.0 as usize][next.1 as usize] = '.';
map[empty.unwrap().0 as usize][empty.unwrap().1 as usize] = 'O';
}
}
Some(
map.iter()
.enumerate()
.map(|(i, row)| {
row.iter()
.enumerate()
.filter_map(|(j, cell)| match cell {
'O' => Some(100 * i + j),
_ => None,
})
.sum::<usize>()
})
.sum::<usize>() as u32,
)
}
pub fn part_two(input: &str) -> Option<u32> {
let mut parts = input.split("\n\n");
let mut map: Vec<Vec<char>> = parts
.next()
.unwrap()
.lines()
.map(|line| {
line.chars()
.flat_map(|c| {
match c {
'#' => "##",
'O' => "[]",
'.' => "..",
'@' => "@.",
_ => panic!(),
}
.chars()
})
.collect()
})
.collect();
let instructions = parts
.next()
.unwrap()
.chars()
.filter_map(|c| match c {
'<' => Some((0, -1)),
'>' => Some((0, 1)),
'^' => Some((-1, 0)),
'v' => Some((1, 0)),
_ => None,
})
.collect::<Vec<_>>();
let mut robot = (0, 0);
for (i, row) in map.iter().enumerate() {
for (j, c) in row.iter().enumerate() {
if *c == '@' {
robot = (i as i32, j as i32);
}
}
}
map[robot.0 as usize][robot.1 as usize] = '.';
for instruction in instructions {
let next = (robot.0 + instruction.0, robot.1 + instruction.1);
if map[next.0 as usize][next.1 as usize] == '.' {
robot = next;
} else {
let to_push = push_recursive(&mut map, next, instruction);
match to_push {
Some(positions) => {
robot = next;
let mut new_positions = Vec::new();
for position in &positions {
new_positions.push((
position.0 + instruction.0,
position.1 + instruction.1,
map[position.0 as usize][position.1 as usize],
));
}
for position in &positions {
map[position.0 as usize][position.1 as usize] = '.';
}
for position in new_positions {
map[position.0 as usize][position.1 as usize] = position.2;
}
}
None => (),
}
}
}
Some(
map.iter()
.enumerate()
.map(|(i, row)| {
row.iter()
.enumerate()
.filter_map(|(j, cell)| match cell {
'[' => Some(100 * i + j),
_ => None,
})
.sum::<usize>()
})
.sum::<usize>() as 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(10092));
}
#[test]
fn test_part_two() {
let result = part_two(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, Some(9021));
}
}