Advent of Code solutions

Day 7

bwc9876.dev 62c99d3e db044414

verified
+115 -5
+83 -5
years/2025/src/day_7.rs
··· 1 + use std::collections::{HashMap, HashSet, VecDeque}; 1 2 2 3 use advent_core::{Day, day_stuff, ex_for_day}; 4 + use utils::{dir::Direction, pos::Position, tiles}; 3 5 4 6 pub struct Day7; 5 7 8 + tiles!(Tile, [ 9 + 'S' => Start, 10 + '^' => Splitter, 11 + '.' => Empty, 12 + ]); 13 + 14 + type Grid = utils::grid::Grid<Tile>; 15 + 6 16 impl Day for Day7 { 17 + day_stuff!(7, "21", "40", Grid); 7 18 8 - day_stuff!(7, "", ""); 19 + fn part_1(input: Self::Input) -> Option<String> { 20 + let (start, _) = input.iter().find(|(_, t)| **t == Tile::Start).unwrap(); 21 + let mut queue = VecDeque::<Position>::new(); 22 + let mut seen = HashSet::<Position>::with_capacity(input.size().1); 23 + 24 + queue.push_back(start); 25 + 26 + let mut splits = 0; 9 27 10 - fn part_1(_input: Self::Input) -> Option<String> { 11 - None 28 + while let Some(pos) = queue.pop_front() { 29 + if seen.contains(&pos) { 30 + continue; 31 + } 32 + seen.insert(pos); 33 + let next_pos = Position::new(pos.x, pos.y + 1); 34 + match input.get(next_pos) { 35 + Some(Tile::Splitter) => { 36 + splits += 1; 37 + queue.push_back(Position::new(pos.x + 1, pos.y)); 38 + queue.push_back(Position::new(pos.x - 1, pos.y)); 39 + } 40 + Some(Tile::Empty) => { 41 + queue.push_back(next_pos); 42 + } 43 + _ => {} 44 + } 45 + } 46 + 47 + Some(splits.to_string()) 12 48 } 13 49 14 - fn part_2(_input: Self::Input) -> Option<String> { 15 - None 50 + fn part_2(input: Self::Input) -> Option<String> { 51 + let (start, _) = input.iter().find(|(_, t)| **t == Tile::Start).unwrap(); 52 + let mut queue = VecDeque::<Position>::new(); 53 + let mut seen = HashMap::<Position, usize>::new(); 54 + 55 + seen.insert(start, 1); 56 + queue.push_front(start); 57 + 58 + let mut splits = 0; 59 + 60 + while let Some(pos) = queue.pop_front() { 61 + let amnt = seen.get(&pos).copied().unwrap_or_default(); 62 + let next_pos = pos.move_dir(Direction::South); 63 + match input.get(next_pos) { 64 + Some(Tile::Splitter) => { 65 + for (next_pos, _) in next_pos.relatives(&[Direction::West, Direction::East]) { 66 + if let Some(curr) = seen.get_mut(&next_pos) { 67 + *curr += amnt; 68 + } else { 69 + seen.insert(next_pos, amnt); 70 + queue.push_back(next_pos); 71 + } 72 + } 73 + } 74 + Some(Tile::Empty) => { 75 + if let Some(curr) = seen.get_mut(&next_pos) { 76 + *curr += amnt; 77 + } else { 78 + seen.insert(next_pos, amnt); 79 + queue.push_back(next_pos); 80 + } 81 + } 82 + None => { 83 + splits += amnt; 84 + } 85 + _ => {} 86 + } 87 + } 88 + 89 + Some(splits.to_string()) 90 + } 91 + 92 + fn parse_input(input: &str) -> Self::Input { 93 + Grid::parse(input) 16 94 } 17 95 }
+16
years/2025/src/examples/day_7/1.txt
··· 1 + .......S....... 2 + ............... 3 + .......^....... 4 + ............... 5 + ......^.^...... 6 + ............... 7 + .....^.^.^..... 8 + ............... 9 + ....^.^...^.... 10 + ............... 11 + ...^.^...^.^... 12 + ............... 13 + ..^...^.....^.. 14 + ............... 15 + .^.^.^.^.^...^. 16 + ...............
+16
years/2025/src/examples/day_7/2.txt
··· 1 + .......S....... 2 + ............... 3 + .......^....... 4 + ............... 5 + ......^.^...... 6 + ............... 7 + .....^.^.^..... 8 + ............... 9 + ....^.^...^.... 10 + ............... 11 + ...^.^...^.^... 12 + ............... 13 + ..^...^.....^.. 14 + ............... 15 + .^.^.^.^.^...^. 16 + ...............