-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday06.rs
186 lines (157 loc) · 5.03 KB
/
day06.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
use std::collections::HashSet;
use std::thread;
#[derive(Clone, Debug, PartialEq)]
enum Entity {
None,
Guard,
Obstacle,
}
const DIRECTIONS: [(i64, i64); 4] = [
/* up */ (-1, 0),
/* right */ (0, 1),
/* down */ (1, 0),
/* left */ (0, -1),
];
fn parse_input(input: &Vec<String>) -> Vec<Vec<Entity>> {
input
.iter()
.map(|line| {
line.chars()
.map(|c| match c {
'.' => Entity::None,
'^' => Entity::Guard,
'#' => Entity::Obstacle,
_ => panic!("Invalid entity type"),
})
.collect()
})
.collect()
}
fn find_guard_coordinates(grid: &Vec<Vec<Entity>>) -> (i64, i64) {
for (row_index, row) in grid.iter().enumerate() {
for (col_index, entity) in row.iter().enumerate() {
if entity == &Entity::Guard {
return (row_index as i64, col_index as i64);
}
}
}
panic!("Guard not found");
}
fn next_position(grid: &[Vec<Entity>], x: i64, y: i64, (direction_x, direction_y): (i64, i64)) -> Option<(i64, i64)> {
let (next_x, next_y) = (x + direction_x, y + direction_y);
if is_in_grid_bounds(grid, next_x, next_y) {
Some((next_x, next_y))
} else {
None
}
}
fn is_in_grid_bounds(grid: &[Vec<Entity>], x: i64, y: i64) -> bool {
x >= 0 && y >= 0 && x < grid.len() as i64 && y < grid[0].len() as i64
}
fn is_obstacle(grid: &[Vec<Entity>], x: i64, y: i64) -> bool {
grid[x as usize][y as usize] == Entity::Obstacle
}
fn is_loop(grid: &[Vec<Entity>], (mut x, mut y): (i64, i64)) -> bool {
let mut visited = HashSet::new();
let mut direction_index = 0;
loop {
let key = (x, y, direction_index);
if !visited.insert(key) {
return true; // Loop detected
}
if let Some((next_x, next_y)) = next_position(grid, x, y, DIRECTIONS[direction_index]) {
// When we hit an obstacle, we need to change direction, just like in
// part 1.
if is_obstacle(grid, next_x, next_y) {
direction_index = (direction_index + 1) % DIRECTIONS.len();
} else {
x = next_x;
y = next_y;
}
} else {
// The next position is out of bounds, so we're done. Placing the
// obstacle did not create a loop.
return false;
}
}
}
fn count_loops_for_row(grid: &[Vec<Entity>], start_pos: (i64, i64), x: usize) -> i64 {
grid[x]
.iter()
.enumerate()
.filter(|(_, &ref cell)| cell == &Entity::None)
.filter_map(|(y, _)| {
let mut local_grid = grid.to_vec();
local_grid[x][y] = Entity::Obstacle;
is_loop(&local_grid, start_pos).then_some(1)
})
.sum()
}
pub fn part1(input: &Vec<String>) -> i64 {
let grid = parse_input(input);
let (mut x, mut y) = find_guard_coordinates(&grid);
let mut direction_index = 0;
let mut visited = vec![vec![false; grid[0].len()]; grid.len()];
visited[x as usize][y as usize] = true;
while let Some((next_x, next_y)) = next_position(&grid, x, y, DIRECTIONS[direction_index]) {
if is_obstacle(&grid, next_x, next_y) {
// Move clockwise. Up -> Right -> Down -> Left -> Up -> ...
direction_index = (direction_index + 1) % DIRECTIONS.len();
} else {
// Continue moving and mark the cell as visited.
x = next_x;
y = next_y;
visited[x as usize][y as usize] = true;
}
}
visited
.into_iter()
.flat_map(|row| row.into_iter())
.filter(|&tile| tile)
.count() as i64
}
pub fn part2(input: &Vec<String>) -> i64 {
part2_threads(input)
}
/**
* Faster, multi-threaded solution.
*
* Probably not very Rust, but eh, it works. Yay for parallelism!
*
* Takes less than a second when compiled with `--release` and
* about 5-7 seconds without it (for both the example and the puzzle input).
*/
pub fn part2_threads(input: &Vec<String>) -> i64 {
let grid = parse_input(input);
let start_pos = find_guard_coordinates(&grid);
let mut threads = vec![];
for row_index in 0..grid.len() {
let grid_clone = grid.clone();
threads.push(thread::spawn(move || {
count_loops_for_row(&grid_clone, start_pos, row_index)
}));
}
let mut total_loops = 0;
for handle in threads {
total_loops += handle.join().unwrap();
}
total_loops
}
/**
* Naive, single-threaded solution.
*
* Takes about 10 seconds when compiled with `--release`, but
* well over 1 minute in unoptimized debug builds.
*
* Keeping it here for reference :D
*/
#[allow(dead_code)]
pub fn part2_slow(input: &Vec<String>) -> i64 {
let grid = parse_input(input);
let start_pos = find_guard_coordinates(&grid);
let mut loops = 0;
for row_index in 0..grid.len() {
loops += count_loops_for_row(&grid, start_pos, row_index);
}
loops
}