|
| 1 | +use std::collections::HashMap; |
| 2 | + |
| 3 | +use aoclp::dij; |
| 4 | +use aoclp::positioning::direction::four_points::Direction4; |
| 5 | +use aoclp::positioning::pt::{matrix_to_map, Pt}; |
| 6 | +use aoclp::solvers_impl::input::safe_get_input_as_terrain; |
| 7 | +use strum::IntoEnumIterator; |
| 8 | + |
| 9 | +pub fn part_1() -> usize { |
| 10 | + Map::default().trailheads().map(|h| h.score).sum() |
| 11 | +} |
| 12 | + |
| 13 | +pub fn part_2() -> usize { |
| 14 | + Map::default().trailheads().map(|h| h.rating).sum() |
| 15 | +} |
| 16 | + |
| 17 | +#[derive(Debug, Copy, Clone)] |
| 18 | +struct Tile { |
| 19 | + height: usize, |
| 20 | +} |
| 21 | + |
| 22 | +impl From<char> for Tile { |
| 23 | + fn from(c: char) -> Self { |
| 24 | + Self { height: c.to_digit(10).unwrap() as usize } |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +#[derive(Debug, Copy, Clone)] |
| 29 | +struct Trailhead { |
| 30 | + start: Pt, |
| 31 | + score: usize, |
| 32 | + rating: usize, |
| 33 | +} |
| 34 | + |
| 35 | +#[derive(Debug)] |
| 36 | +struct Map { |
| 37 | + heightmap: HashMap<Pt, usize>, |
| 38 | +} |
| 39 | + |
| 40 | +impl Map { |
| 41 | + fn trailheads(&self) -> impl Iterator<Item = Trailhead> + use<'_> { |
| 42 | + self.heightmap |
| 43 | + .iter() |
| 44 | + .filter(|(_, &t)| t == 0) |
| 45 | + .filter_map(move |(&p, _)| { |
| 46 | + let dist = dij::build(self, p).dist; |
| 47 | + let score = dist.values().filter(|d| **d == 9).count(); |
| 48 | + if score == 0 { |
| 49 | + return None; |
| 50 | + } |
| 51 | + |
| 52 | + let mut cache = HashMap::new(); |
| 53 | + let trailhead = Trailhead { |
| 54 | + start: p, |
| 55 | + score, |
| 56 | + rating: dist |
| 57 | + .iter() |
| 58 | + .filter(|(_, d)| **d == 9) |
| 59 | + .map(|(&p, _)| Self::path_count(p, &dist, &mut cache)) |
| 60 | + .sum(), |
| 61 | + }; |
| 62 | + Some(trailhead) |
| 63 | + }) |
| 64 | + } |
| 65 | + |
| 66 | + fn path_count(p: Pt, dist: &HashMap<Pt, usize>, cache: &mut HashMap<Pt, usize>) -> usize { |
| 67 | + if let Some(count) = cache.get(&p) { |
| 68 | + return *count; |
| 69 | + } |
| 70 | + |
| 71 | + let p_dist = dist[&p]; |
| 72 | + if p_dist == 0 { |
| 73 | + return 1; |
| 74 | + } |
| 75 | + |
| 76 | + let count = Direction4::iter() |
| 77 | + .map(|d| p + d) |
| 78 | + .filter(|n| dist.get(n).is_some_and(|d| *d == p_dist - 1)) |
| 79 | + .map(|n| Self::path_count(n, dist, cache)) |
| 80 | + .sum(); |
| 81 | + cache.insert(p, count); |
| 82 | + |
| 83 | + count |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +impl dij::Graph<Pt> for Map { |
| 88 | + fn neighbours(&self, node: &Pt) -> impl Iterator<Item = Pt> { |
| 89 | + let height = self.heightmap[node]; |
| 90 | + Direction4::iter() |
| 91 | + .map(|d| *node + d) |
| 92 | + .filter(move |n| self.heightmap.get(n).is_some_and(|h| *h == height + 1)) |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +impl Default for Map { |
| 97 | + fn default() -> Self { |
| 98 | + Self { |
| 99 | + heightmap: matrix_to_map( |
| 100 | + safe_get_input_as_terrain::<Tile>(2024, 10) |
| 101 | + .into_iter() |
| 102 | + .map(|y| y.into_iter().map(|t| t.height)), |
| 103 | + ), |
| 104 | + } |
| 105 | + } |
| 106 | +} |
0 commit comments