-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmain.rs
37 lines (34 loc) · 1.29 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
const NEXT: [(usize, usize); 4] = [(1, 0), (usize::MAX, 0), (0, 1), (0, usize::MAX)];
pub fn main() {
let data = include_str!("../input.txt");
let mut map: Vec<_> = data
.bytes()
.filter(|b| b != &b'\n')
.map(|b| b.to_ascii_lowercase() - b'a')
.collect();
let w = data.bytes().position(|b| b == b'\n').unwrap();
let h = map.len() / w;
let mut start = data.bytes().position(|b| b == b'S').unwrap();
let mut end = data.bytes().position(|b| b == b'E').unwrap();
(start, end, map[start], map[end]) = (start - start / (w + 1), end - end / (w + 1), 0, 25);
println!(
"{}",
map.iter()
.enumerate()
.filter(|(_, b)| **b == 0)
.filter_map(|(start, _)| pathfinding::directed::bfs::bfs(
&(start % w, start / w),
|(x, y)| {
let cur = map[y * w + x];
NEXT.iter()
.map(|(xx, yy)| (x.wrapping_add(*xx), y.wrapping_add(*yy)))
.filter(|(x, y)| x < &w && y < &h && map[y * w + x] <= cur + 1)
.collect::<Vec<_>>()
},
|&p| p == (end % w, end / w),
)
.map(|r| r.len() - 1))
.min()
.unwrap()
);
}