-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
259 lines (236 loc) · 6.72 KB
/
main.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
248
249
250
251
252
253
254
255
256
257
258
259
use std::collections::HashMap;
use std::error::Error;
use std::io::{self, Read, Write};
use std::time::Instant;
#[allow(unused_macros)]
macro_rules! err {
($($tt:tt)*) => { Err(Box::<dyn Error>::from(format!($($tt)*))) }
}
type Result<T> = ::std::result::Result<T, Box<dyn Error>>;
type Coord = (usize, usize);
fn parse_input<T: AsRef<str>>(input: T) -> Vec<Vec<char>> {
input
.as_ref()
.split_whitespace()
.map(|l| l.chars().collect())
.collect()
}
fn neighbors(pos: Coord, trails: &[Vec<char>], part2: bool) -> Vec<Coord> {
let (x, y) = pos;
let possible: &[_] = if !part2 {
match trails[x][y] {
'.' => &[(-1, 0), (1, 0), (0, -1), (0, 1)],
'>' => &[(0, 1)],
'<' => &[(0, -1)],
'^' => &[(-1, 0)],
'v' => &[(1, 0)],
_ => return vec![],
}
} else if trails[x][y] != '#' {
&[(-1, 0), (1, 0), (0, -1), (0, 1)]
} else {
return vec![];
};
let mut result = vec![];
for (dx, dy) in possible {
let nx = x as isize + dx;
let ny = y as isize + dy;
if nx < 0 || ny < 0 || nx as usize >= trails.len() || ny as usize >= trails[0].len() {
continue;
}
let nx = nx as usize;
let ny = ny as usize;
if trails[nx][ny] != '#' {
result.push((nx, ny));
}
}
result
}
fn dfs_grid(
pos: Coord,
trails: &[Vec<char>],
visited: &mut [Vec<bool>],
part2: bool,
) -> Option<usize> {
if pos.0 == trails.len() - 1 {
if trails[pos.0][pos.1] == '.' {
Some(0)
} else {
None
}
} else {
let mut count = 0;
for next in neighbors(pos, trails, part2) {
if !visited[next.0][next.1] {
visited[next.0][next.1] = true;
if let Some(remain) = dfs_grid(next, trails, visited, part2) {
count = count.max(1 + remain);
}
visited[next.0][next.1] = false;
}
}
if count == 0 {
None
} else {
Some(count)
}
}
}
fn gen_graph(trails: &[Vec<char>], part2: bool) -> HashMap<Coord, Vec<(Coord, usize)>> {
let mut graph: HashMap<_, Vec<_>> = HashMap::new();
for i in 0..trails.len() {
for j in 0..trails[0].len() {
if trails[i][j] == '#' {
continue;
}
let e = graph.entry((i, j)).or_default();
e.extend(neighbors((i, j), trails, part2).into_iter().map(|p| (p, 1)));
}
}
graph
}
fn prune_graph(graph: &mut HashMap<Coord, Vec<(Coord, usize)>>) {
while let Some((&pos, _)) = graph.iter().find(|(_, v)| v.len() == 2) {
let neighbors = graph.remove(&pos).unwrap();
let (a, ad) = neighbors[0];
let (b, bd) = neighbors[1];
let ea = graph.entry(a).or_default();
if let Some(i) = ea.iter().position(|(p, _)| p == &pos) {
ea[i] = (b, ad + bd);
}
let eb = graph.entry(b).or_default();
if let Some(i) = eb.iter().position(|(p, _)| p == &pos) {
eb[i] = (a, ad + bd);
}
}
}
fn dfs_graph(
pos: Coord,
end: Coord,
graph: &HashMap<Coord, Vec<(Coord, usize)>>,
visited: &mut [Vec<bool>],
) -> Option<usize> {
if pos == end {
return Some(0);
}
graph[&pos]
.iter()
.filter_map(|&(next, d)| {
if !visited[next.0][next.1] {
visited[next.0][next.1] = true;
let d = dfs_graph(next, end, graph, visited).map(|r| r + d);
visited[next.0][next.1] = false;
d
} else {
None
}
})
.max()
}
fn part1(trails: &[Vec<char>]) -> Result<usize> {
let _start = Instant::now();
let start = (
0,
(0..trails[0].len()).find(|&y| trails[0][y] == '.').unwrap(),
);
let result = dfs_grid(
start,
trails,
&mut vec![vec![false; trails[0].len()]; trails.len()],
false,
)
.unwrap();
writeln!(io::stdout(), "Part 1: {result}")?;
writeln!(io::stdout(), "> Time elapsed is: {:?}", _start.elapsed())?;
Ok(result)
}
#[allow(dead_code)]
fn part2_grid(trails: &[Vec<char>]) -> Result<usize> {
let _start = Instant::now();
let start = (
0,
(0..trails[0].len()).find(|&y| trails[0][y] == '.').unwrap(),
);
let result = dfs_grid(
start,
trails,
&mut vec![vec![false; trails[0].len()]; trails.len()],
true,
)
.unwrap();
writeln!(io::stdout(), "Part 2 with grid: {result}")?;
writeln!(io::stdout(), "> Time elapsed is: {:?}", _start.elapsed())?;
Ok(result)
}
fn part2_graph(trails: &[Vec<char>]) -> Result<usize> {
let _start = Instant::now();
let start = (
0,
(0..trails[0].len()).find(|&y| trails[0][y] == '.').unwrap(),
);
let end = (
trails.len() - 1,
(0..trails[0].len())
.find(|&y| trails[trails.len() - 1][y] == '.')
.unwrap(),
);
let mut graph = gen_graph(trails, true);
prune_graph(&mut graph);
let result = dfs_graph(
start,
end,
&graph,
&mut vec![vec![false; trails[0].len()]; trails.len()],
)
.unwrap();
writeln!(io::stdout(), "Part 2 with graph: {result}")?;
writeln!(io::stdout(), "> Time elapsed is: {:?}", _start.elapsed())?;
Ok(result)
}
fn main() -> Result<()> {
let mut input = String::new();
io::stdin().read_to_string(&mut input)?;
let trails = parse_input(input);
part1(&trails)?;
part2_graph(&trails)?;
// part2_grid(&trails)?;
Ok(())
}
#[test]
fn example_input() {
let input = "#.#####################
#.......#########...###
#######.#########.#.###
###.....#.>.>.###.#.###
###v#####.#v#.###.#.###
###.>...#.#.#.....#...#
###v###.#.#.#########.#
###...#.#.#.......#...#
#####.#.#.#######.#.###
#.....#.#.#.......#...#
#.#####.#.#.#########v#
#.#...#...#...###...>.#
#.#.#v#######v###.###v#
#...#.>.#...>.>.#.###.#
#####v#.#.###v#.#.###.#
#.....#...#...#.#.#...#
#.#########.###.#.#.###
#...###...#...#...#.###
###.###.#.###v#####v###
#...#...#.#.>.>.#.>.###
#.###.###.#.###.#.#v###
#.....###...###...#...#
#####################.#";
let trails = parse_input(input);
assert_eq!(part1(&trails).unwrap(), 94);
assert_eq!(part2_graph(&trails).unwrap(), 154);
assert_eq!(part2_grid(&trails).unwrap(), 154);
}
#[test]
fn real_input() {
let input = std::fs::read_to_string("input/input.txt").unwrap();
let trails = parse_input(input);
assert_eq!(part1(&trails).unwrap(), 2394);
assert_eq!(part2_graph(&trails).unwrap(), 6554);
// assert_eq!(part2_grid(&trails).unwrap(), 6554);
}