forked from timvisee/advent-of-code-2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
59 lines (57 loc) · 1.9 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
pub fn main() {
let d = include_bytes!("../input.txt");
let s = d.iter().position(|b| b == &b'\n').unwrap();
let g: Vec<&[u8]> = d.split(|b| b == &b'\n').filter(|l| !l.is_empty()).collect();
let mut seen = rustc_hash::FxHashSet::default();
for i in 1..s - 1 {
seen.extend(
(1..s - 1)
.scan(g[0][i], |max, y| match (*max, g[y][i]) {
(m, n) if n > m => {
*max = n;
Some(Some(i + y * s))
}
(m, _) if m >= b'9' => None,
_ => Some(None),
})
.flatten(),
);
seen.extend(
(1..s - 1)
.scan(g[s - 1][i], |max, y| match (*max, g[s - y][i]) {
(m, n) if n > m => {
*max = n;
Some(Some(i + (s - y) * s))
}
(m, _) if m >= b'9' => None,
_ => Some(None),
})
.flatten(),
);
seen.extend(
(1..s - 1)
.scan(g[i][0], |max, x| match (*max, g[i][x]) {
(m, n) if n > m => {
*max = n;
Some(Some(x + i * s))
}
(m, _) if m >= b'9' => None,
_ => Some(None),
})
.flatten(),
);
seen.extend(
(1..s - 1)
.scan(g[i][s - 1], |max, x| match (*max, g[i][s - x]) {
(m, n) if n > m => {
*max = n;
Some(Some(s - x + i * s))
}
(m, _) if m >= b'9' => None,
_ => Some(None),
})
.flatten(),
);
}
println!("{}", seen.len() + ((s - 1) * 4));
}