generated from fspoettel/advent-of-code-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
16.rs
205 lines (174 loc) · 6.55 KB
/
16.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
use std::collections::HashSet;
advent_of_code::solution!(16);
pub fn part_one(input: &str) -> Option<usize> {
let contraption = Contraption::from_input(input);
Some(contraption.tiles_visited(Path::new(0, 0, Direction::Right)))
}
/**
* Would be much more efficient to store how many tiles will be visited when starting at each coordinate I visited previously and cache those values between calls to `contraption.tiles_visited`.
* The current implementation is fast enough and didn't require any significant code changes from part one, so I didn't bother.
*/
pub fn part_two(input: &str) -> Option<usize> {
let contraption = Contraption::from_input(input);
// Assuming all lines have the same length
let x_len = contraption.grid[0].len();
let y_len = contraption.grid.len();
let mut max_tiles_visited = 0;
for y in 0..y_len {
let tiles_visited_from_left = contraption.tiles_visited(Path::new(0, y, Direction::Right));
let tiles_visited_from_right =
contraption.tiles_visited(Path::new(x_len - 1, y, Direction::Left));
if tiles_visited_from_left > max_tiles_visited {
max_tiles_visited = tiles_visited_from_left;
}
if tiles_visited_from_right > max_tiles_visited {
max_tiles_visited = tiles_visited_from_right;
}
}
for x in 0..x_len {
let tiles_visited_from_top = contraption.tiles_visited(Path::new(x, 0, Direction::Down));
let tiles_visited_from_bottom =
contraption.tiles_visited(Path::new(x, y_len - 1, Direction::Up));
if tiles_visited_from_top > max_tiles_visited {
max_tiles_visited = tiles_visited_from_top;
}
if tiles_visited_from_bottom > max_tiles_visited {
max_tiles_visited = tiles_visited_from_bottom;
}
}
Some(max_tiles_visited)
}
struct Contraption<'a> {
grid: Vec<&'a [u8]>,
}
impl Contraption<'_> {
fn from_input(input: &str) -> Contraption<'_> {
Contraption {
grid: input.lines().map(|line| line.as_bytes()).collect(),
}
}
fn tiles_visited(&self, start_path: Path) -> usize {
let mut visited_paths = HashSet::new();
let mut paths = vec![start_path];
while let Some(path) = paths.pop() {
if visited_paths.contains(&path) {
continue;
}
let char = self.grid[path.coordinate.y][path.coordinate.x];
match char {
b'.' => self.add_next_path(&path, path.entering_direction, &mut paths),
b'/' => {
let next_direction = match path.entering_direction {
Direction::Up => Direction::Right,
Direction::Down => Direction::Left,
Direction::Left => Direction::Down,
Direction::Right => Direction::Up,
};
self.add_next_path(&path, next_direction, &mut paths);
}
b'\\' => {
let next_direction = match path.entering_direction {
Direction::Up => Direction::Left,
Direction::Down => Direction::Right,
Direction::Left => Direction::Up,
Direction::Right => Direction::Down,
};
self.add_next_path(&path, next_direction, &mut paths);
}
b'|' => match path.entering_direction {
Direction::Left | Direction::Right => {
self.add_next_path(&path, Direction::Up, &mut paths);
self.add_next_path(&path, Direction::Down, &mut paths);
}
_ => self.add_next_path(&path, path.entering_direction, &mut paths),
},
b'-' => match path.entering_direction {
Direction::Up | Direction::Down => {
self.add_next_path(&path, Direction::Left, &mut paths);
self.add_next_path(&path, Direction::Right, &mut paths);
}
_ => self.add_next_path(&path, path.entering_direction, &mut paths),
},
_ => panic!("Unknown character {}", char as char),
}
visited_paths.insert(path);
}
visited_paths
.into_iter()
.map(|path| path.coordinate)
.collect::<HashSet<_>>()
.len()
}
fn add_next_path(&self, path: &Path, direction: Direction, paths: &mut Vec<Path>) {
if self.is_in_bounds(&path.coordinate, direction) {
paths.push(path.next(direction))
}
}
fn is_in_bounds(&self, coordinate: &Coordinate, direction: Direction) -> bool {
(direction == Direction::Up && coordinate.y != 0)
|| (direction == Direction::Left && coordinate.x != 0)
|| (direction == Direction::Down && coordinate.y != self.grid.len() - 1)
|| (direction == Direction::Right && coordinate.x != self.grid[coordinate.y].len() - 1)
}
}
#[derive(Hash, Eq, PartialEq)]
struct Path {
coordinate: Coordinate,
entering_direction: Direction,
}
impl Path {
fn new(x: usize, y: usize, direction: Direction) -> Path {
Path {
coordinate: Coordinate { x, y },
entering_direction: direction,
}
}
fn next(&self, direction: Direction) -> Path {
Path {
coordinate: self.coordinate.next(direction),
entering_direction: direction,
}
}
}
#[derive(Hash, Eq, PartialEq)]
struct Coordinate {
x: usize,
y: usize,
}
impl Coordinate {
fn next(&self, direction: Direction) -> Coordinate {
Coordinate {
x: match direction {
Direction::Left => self.x - 1,
Direction::Right => self.x + 1,
_ => self.x,
},
y: match direction {
Direction::Up => self.y - 1,
Direction::Down => self.y + 1,
_ => self.y,
},
}
}
}
#[derive(Hash, Eq, PartialEq, Copy, Clone)]
enum Direction {
Up,
Down,
Left,
Right,
}
#[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(46));
}
#[test]
fn test_part_two() {
let result = part_two(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, Some(51));
}
}