|
| 1 | +use std::collections::HashMap; |
| 2 | + |
| 3 | +use aoclp::positioning::direction::four_points::Direction4; |
| 4 | +use aoclp::positioning::pt::{matrix_to_map, Pt}; |
| 5 | +use aoclp::solvers_impl::input::safe_get_input_as_terrain; |
| 6 | +use derive_where::derive_where; |
| 7 | +use itertools::Itertools; |
| 8 | + |
| 9 | +pub fn part_1() -> usize { |
| 10 | + manifoldize().splits |
| 11 | +} |
| 12 | + |
| 13 | +pub fn part_2() -> usize { |
| 14 | + manifoldize().worlds |
| 15 | +} |
| 16 | + |
| 17 | +#[derive(Debug, Clone)] |
| 18 | +struct Manifold { |
| 19 | + parts: HashMap<Pt, char>, |
| 20 | + starting_point: Pt, |
| 21 | +} |
| 22 | + |
| 23 | +impl Default for Manifold { |
| 24 | + fn default() -> Self { |
| 25 | + let parts = matrix_to_map(input()); |
| 26 | + let starting_point = *parts.iter().find(|(_, c)| **c == 'S').unwrap().0; |
| 27 | + Self { parts, starting_point } |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +#[derive(Debug, Copy, Clone)] |
| 32 | +#[derive_where(PartialEq, Eq, PartialOrd, Ord, Hash)] |
| 33 | +struct Particle { |
| 34 | + pos: Pt, |
| 35 | + #[derive_where(skip)] |
| 36 | + worlds: usize, |
| 37 | +} |
| 38 | + |
| 39 | +impl Particle { |
| 40 | + fn initial(manifold: &Manifold) -> Self { |
| 41 | + Self { pos: manifold.starting_point, worlds: 1 } |
| 42 | + } |
| 43 | + |
| 44 | + fn travel(self, dir: Direction4) -> Self { |
| 45 | + Self { pos: self.pos + dir, ..self } |
| 46 | + } |
| 47 | + |
| 48 | + fn onward(self, manifold: &Manifold) -> Vec<Self> { |
| 49 | + let next = self.travel(Direction4::Down); |
| 50 | + if !manifold.parts.contains_key(&next.pos) { |
| 51 | + return vec![self]; |
| 52 | + } |
| 53 | + |
| 54 | + if manifold.parts.get(&next.pos).is_some_and(|c| *c == '^') { |
| 55 | + vec![next.travel(Direction4::Left), next.travel(Direction4::Right)] |
| 56 | + } else { |
| 57 | + vec![next] |
| 58 | + } |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +struct ManifoldizationResult { |
| 63 | + splits: usize, |
| 64 | + worlds: usize, |
| 65 | +} |
| 66 | + |
| 67 | +fn manifoldize() -> ManifoldizationResult { |
| 68 | + let manifold = Manifold::default(); |
| 69 | + let mut particles = vec![Particle::initial(&manifold)]; |
| 70 | + |
| 71 | + let mut splits = 0; |
| 72 | + loop { |
| 73 | + let new_particles = particles |
| 74 | + .iter() |
| 75 | + .flat_map(|particle| { |
| 76 | + let moved = particle.onward(&manifold); |
| 77 | + if moved.len() > 1 { |
| 78 | + splits += 1; |
| 79 | + } |
| 80 | + moved |
| 81 | + }) |
| 82 | + .sorted() |
| 83 | + .coalesce(|a, b| { |
| 84 | + if a == b { |
| 85 | + Ok(Particle { pos: a.pos, worlds: a.worlds + b.worlds }) |
| 86 | + } else { |
| 87 | + Err((a, b)) |
| 88 | + } |
| 89 | + }) |
| 90 | + .collect_vec(); |
| 91 | + |
| 92 | + if new_particles == particles { |
| 93 | + break; |
| 94 | + } |
| 95 | + particles = new_particles; |
| 96 | + } |
| 97 | + |
| 98 | + let worlds = particles.into_iter().map(|p| p.worlds).sum(); |
| 99 | + ManifoldizationResult { splits, worlds } |
| 100 | +} |
| 101 | + |
| 102 | +fn input() -> Vec<Vec<char>> { |
| 103 | + safe_get_input_as_terrain(2025, 7) |
| 104 | +} |
0 commit comments