generated from fspoettel/advent-of-code-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path23.rs
185 lines (152 loc) · 4.49 KB
/
23.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
advent_of_code::solution!(23);
use advent_of_code::maneatingape::grid::*;
use advent_of_code::maneatingape::hash::*;
use advent_of_code::maneatingape::point::*;
type NodeId = usize;
fn parse_data(input: &str) -> Grid<u8> {
Grid::parse(input)
}
fn generate_graph<F>(
grid: &Grid<u8>,
start_location: Point,
get_neighbors: F,
) -> FastMap<(Point, Point), u32>
where
F: Fn(&Grid<u8>, Point, Point) -> Vec<Point>,
{
let mut graph = FastMap::new();
let mut queue = vec![(start_location, start_location, 0)];
while let Some((cross_loc, path_loc, init_cost)) = queue.pop() {
let mut prev_loc = cross_loc;
let mut loc = path_loc;
let mut cost = init_cost;
let mut neighbors;
loop {
neighbors = get_neighbors(grid, prev_loc, loc);
if neighbors.len() != 1 {
break;
}
prev_loc = loc;
loc = neighbors[0];
cost += 1;
}
if !graph.contains_key(&(cross_loc, loc)) && cost > 1 {
graph.insert((cross_loc, loc), cost);
queue.extend(neighbors.into_iter().map(|n| (loc, n, 1)));
}
}
graph
}
fn simplify_graph(
graph: FastMap<(Point, Point), u32>,
start_location: &Point,
end_location: &Point,
) -> (Vec<Vec<(NodeId, u32)>>, NodeId, NodeId) {
let idx_mapper = graph
.keys()
.flat_map(|(from, to)| [from, to])
.collect::<FastSet<_>>()
.into_iter()
.enumerate()
.map(|(i, x)| (x, i))
.collect::<FastMap<_, _>>();
let mut graph_as_vec = vec![vec![]; idx_mapper.len()];
for ((from, to), cost) in &graph {
graph_as_vec[idx_mapper[from]].push((idx_mapper[to], *cost));
}
(
graph_as_vec,
idx_mapper[start_location],
idx_mapper[end_location],
)
}
fn find_all_paths<F>(
graph: &[Vec<(NodeId, u32)>],
node: NodeId,
cost: u32,
dfs_visitor: &mut F,
visited: &mut [bool],
) where
F: FnMut(NodeId, u32),
{
visited[node] = true;
dfs_visitor(node, cost);
for &(new_node, new_cost) in &graph[node] {
if !visited[new_node] {
find_all_paths(graph, new_node, cost + new_cost, dfs_visitor, visited);
}
}
visited[node] = false;
}
fn part_x<F>(grid: &Grid<u8>, get_neighbors: F) -> u32
where
F: Fn(&Grid<u8>, Point, Point) -> Vec<Point>,
{
let start_location = Point::new(1, 0);
let end_location = Point::new(grid.width - 2, grid.height - 1);
let graph = generate_graph(grid, start_location, get_neighbors);
let (graph, start, end) = simplify_graph(graph, &start_location, &end_location);
let mut result = 0;
let mut dfs_visitor = |node, cost| {
if node == end {
result = u32::max(result, cost)
}
};
find_all_paths(
&graph,
start,
0,
&mut dfs_visitor,
&mut vec![false; graph.len()],
);
result
}
pub fn part_one(input: &str) -> Option<u32> {
let grid = parse_data(input);
fn get_neighbors(grid: &Grid<u8>, prev_loc: Point, loc: Point) -> Vec<Point> {
let options_iter = match grid[loc] {
b'.' => Vec::from(ORTHOGONAL.map(|x| loc + x)),
b'^' => vec![loc + UP],
b'v' => vec![loc + DOWN],
b'<' => vec![loc + LEFT],
b'>' => vec![loc + RIGHT],
_ => unreachable!(),
};
options_iter
.into_iter()
.filter(|&o| o != prev_loc)
.filter(|&o| grid.contains(o))
.filter(|&o| grid[o] != b'#')
.collect::<Vec<_>>()
}
let result = part_x(&grid, get_neighbors);
Some(result)
}
pub fn part_two(input: &str) -> Option<u32> {
let grid = parse_data(input);
fn get_neighbors(grid: &Grid<u8>, prev_loc: Point, loc: Point) -> Vec<Point> {
ORTHOGONAL
.into_iter()
.map(|x| loc + x)
.filter(|&o| o != prev_loc)
.filter(|&o| grid.contains(o))
.filter(|&o| grid[o] != b'#')
.collect::<Vec<_>>()
}
let result = part_x(&grid, get_neighbors);
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(94));
}
#[test]
fn test_part_two() {
let result = part_two(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, Some(154));
}
}