/ Rust / 2022 / 08.rs
08.rs
 1  #![feature(test)]
 2  
 3  use aoc::{grid::Direction, iter_ext::IterExt};
 4  
 5  type Input = Vec<Vec<u8>>;
 6  
 7  struct CoordIterator {
 8      x: usize,
 9      y: usize,
10      width: usize,
11      height: usize,
12      direction: Direction,
13  }
14  
15  impl Iterator for CoordIterator {
16      type Item = (usize, usize);
17  
18      fn next(&mut self) -> Option<Self::Item> {
19          (self.x, self.y) = self
20              .direction
21              .step(self.x, self.y, self.width, self.height)?;
22          Some((self.x, self.y))
23      }
24  }
25  
26  fn setup(input: &str) -> Input {
27      input
28          .trim()
29          .lines()
30          .map(|line| line.bytes().map(|x| x - b'0').collect())
31          .collect()
32  }
33  
34  fn part1(grid: &Input) -> usize {
35      let (height, width) = (grid.len(), grid[0].len());
36  
37      (0..height)
38          .flat_map(|y| {
39              (0..width).filter(move |&x| {
40                  Direction::iter().any(|direction| {
41                      CoordIterator {
42                          x,
43                          y,
44                          width,
45                          height,
46                          direction,
47                      }
48                      .all(|(j, i)| grid[y][x] > grid[i][j])
49                  })
50              })
51          })
52          .count()
53  }
54  
55  fn part2(grid: &Input) -> usize {
56      let (height, width) = (grid.len(), grid[0].len());
57  
58      (0..height)
59          .flat_map(|y| {
60              (0..width).map(move |x| {
61                  Direction::iter()
62                      .map(|direction| {
63                          CoordIterator {
64                              x,
65                              y,
66                              width,
67                              height,
68                              direction,
69                          }
70                          .take_while_inclusive(|&(j, i)| grid[y][x] > grid[i][j])
71                          .count()
72                      })
73                      .product::<usize>()
74              })
75          })
76          .max()
77          .unwrap()
78  }
79  
80  aoc::main!(2022, 8, ex: 1);