-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.rs
33 lines (33 loc) · 1.18 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
pub fn main() {
let map = include_bytes!("../input.txt");
let width = map.iter().position(|b| b == &b'\n').unwrap() as isize;
let mut nums = vec![];
println!(
"{}",
(0..map.len() - 2)
.filter(|i| map[*i] == b'*')
.filter_map(|i| {
nums.clear();
nums.extend(
(-width - 2..=-width)
.chain([-1, 1])
.chain(width..=width + 2)
.map(|pos| (i as isize + pos) as usize)
.filter(|pos| map[*pos].is_ascii_digit())
.flat_map(|pos| {
(pos.saturating_sub(2)..=pos)
.rev()
.take_while(|i| map[*i].is_ascii_digit())
.last()
}),
);
nums.dedup();
(nums.len() == 2).then(|| {
nums.iter()
.map(|i| atoi::atoi::<usize>(&map[*i..*i + 3]).unwrap())
.product::<usize>()
})
})
.sum::<usize>()
);
}