-
Notifications
You must be signed in to change notification settings - Fork 0
/
08.rs
80 lines (69 loc) · 1.9 KB
/
08.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
#![feature(test)]
use aoc::{grid::Direction, iter_ext::IterExt};
type Input = Vec<Vec<u8>>;
struct CoordIterator {
x: usize,
y: usize,
width: usize,
height: usize,
direction: Direction,
}
impl Iterator for CoordIterator {
type Item = (usize, usize);
fn next(&mut self) -> Option<Self::Item> {
(self.x, self.y) = self
.direction
.step(self.x, self.y, self.width, self.height)?;
Some((self.x, self.y))
}
}
fn setup(input: &str) -> Input {
input
.trim()
.lines()
.map(|line| line.bytes().map(|x| x - b'0').collect())
.collect()
}
fn part1(grid: &Input) -> usize {
let (height, width) = (grid.len(), grid[0].len());
(0..height)
.flat_map(|y| {
(0..width).filter(move |&x| {
Direction::iter().any(|direction| {
CoordIterator {
x,
y,
width,
height,
direction,
}
.all(|(j, i)| grid[y][x] > grid[i][j])
})
})
})
.count()
}
fn part2(grid: &Input) -> usize {
let (height, width) = (grid.len(), grid[0].len());
(0..height)
.flat_map(|y| {
(0..width).map(move |x| {
Direction::iter()
.map(|direction| {
CoordIterator {
x,
y,
width,
height,
direction,
}
.take_while_inclusive(|&(j, i)| grid[y][x] > grid[i][j])
.count()
})
.product::<usize>()
})
})
.max()
.unwrap()
}
aoc::main!(2022, 8, ex: 1);