-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path03.rs
91 lines (83 loc) · 2.73 KB
/
03.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
#![feature(test)]
use itertools::{FoldWhile::Continue, Itertools};
use rustc_hash::FxHashMap;
type Input = Vec<Vec<Cell>>;
#[derive(Debug, Clone, Copy)]
enum Cell {
Digit(u8),
Symbol { gear: bool },
Empty,
}
fn setup(input: &str) -> Input {
input
.lines()
.map(|line| {
line.bytes()
.map(|b| match b {
b'0'..=b'9' => Cell::Digit(b - b'0'),
b'.' => Cell::Empty,
b'*' => Cell::Symbol { gear: true },
_ => Cell::Symbol { gear: false },
})
.collect()
})
.collect()
}
fn find_part_numbers(
input: &Input,
) -> impl Iterator<Item = (u32, impl Iterator<Item = (usize, usize)> + '_)> + '_ {
input.iter().enumerate().flat_map(move |(i, line)| {
line.iter()
.copied()
.enumerate()
.batching(|it| {
let (num, j) = it.find_map(|(j, c)| match c {
Cell::Digit(d) => Some((d as u32, j)),
_ => None,
})?;
let (num, k) = it
.fold_while((num, j), |acc @ (num, _), (k, c)| {
match match c {
Cell::Digit(d) => Some(d),
_ => None,
} {
Some(d) => Continue((num * 10 + d as u32, k)),
None => itertools::FoldWhile::Done(acc),
}
})
.into_inner();
Some((num, j, k))
})
.map(move |(num, j, k)| {
let gears = (i.saturating_sub(1)..=(i + 1).min(input.len() - 1))
.dedup()
.flat_map(move |i| {
(j.saturating_sub(1)..=(k + 1).min(input[i].len() - 1))
.filter(move |&j| matches!(input[i][j], Cell::Symbol { .. }))
.map(move |j| (i, j))
});
(num, gears)
})
})
}
fn part1(input: &Input) -> u32 {
find_part_numbers(input)
.filter_map(|(num, mut parts)| parts.next().map(|_| num))
.sum()
}
fn part2(input: &Input) -> u32 {
let mut gears = FxHashMap::<(usize, usize), _>::default();
for (num, gs) in find_part_numbers(input) {
for g in gs {
gears.entry(g).or_insert_with(Vec::new).push(num);
}
}
gears
.into_iter()
.filter(|&((i, j), ref nums)| {
matches!(input[i][j], Cell::Symbol { gear: true }) && nums.len() == 2
})
.map(|(_, nums)| nums.into_iter().product::<u32>())
.sum()
}
aoc::main!(2023, 3, ex: 1);