From 6cac42e2dfb575acbd51b42f2ca94b4c9f1d7d3e Mon Sep 17 00:00:00 2001 From: Kristofers Solo Date: Sun, 7 Dec 2025 14:28:55 +0200 Subject: [PATCH] Finish part-2 --- 2025/day-07/src/part2.rs | 75 +++++++++++++++++++++++++++++++++++----- 1 file changed, 67 insertions(+), 8 deletions(-) diff --git a/2025/day-07/src/part2.rs b/2025/day-07/src/part2.rs index ede34a7..3806309 100644 --- a/2025/day-07/src/part2.rs +++ b/2025/day-07/src/part2.rs @@ -1,11 +1,55 @@ -use miette::Result; +use std::collections::HashMap; +use tracing::info; + +type Column = usize; +type PathCount = usize; + +#[derive(Debug, Clone)] +struct PathCounts { + positions: HashMap, +} + +impl PathCounts { + fn new(start_column: Column) -> Self { + let mut positions = HashMap::new(); + positions.insert(start_column, 1); + Self { positions } + } + fn apply_row(&self, line: &str) -> Self { + let mut new_positions = HashMap::new(); + + for (&column, &count) in &self.positions { + if line.as_bytes()[column] == b'^' { + info!(column, "split at"); + *new_positions.entry(column - 1).or_insert(0) += count; + *new_positions.entry(column + 1).or_insert(0) += count; + } else { + *new_positions.entry(column).or_insert(0) += count; + } + } + + Self { + positions: new_positions, + } + } + + fn total_paths(&self) -> usize { + self.positions.values().sum() + } +} #[tracing::instrument] #[allow(clippy::missing_panics_doc)] #[allow(clippy::missing_errors_doc)] -pub fn process(input: &str) -> Result { - todo!("day xx - part 2"); - Ok(0) +pub fn process(input: &str) -> miette::Result { + let mut lines = input.lines().enumerate(); + let (_, first_line) = lines.next().unwrap(); + let start_column = first_line.chars().position(|ch| ch == 'S').unwrap(); + + let final_state = lines.fold(PathCounts::new(start_column), |state, (_, line)| { + state.apply_row(line) + }); + Ok(final_state.total_paths()) } #[cfg(test)] @@ -13,10 +57,25 @@ mod tests { use super::*; #[test] - fn test_process() -> Result<()> { - let input = ""; - todo!("haven't built test yet"); - let result = 0; + fn test_process() -> miette::Result<()> { + let input = ".......S....... +............... +.......^....... +............... +......^.^...... +............... +.....^.^.^..... +............... +....^.^...^.... +............... +...^.^...^.^... +............... +..^...^.....^.. +............... +.^.^.^.^.^...^. +............... +"; + let result = 40; assert_eq!(process(input)?, result); Ok(()) }