From e0df7d08efd6a4124a30be4e1716fc78f941c71a Mon Sep 17 00:00:00 2001 From: Quentin Boyer Date: Thu, 11 Dec 2025 19:23:12 +0100 Subject: [PATCH] Day 11 --- src/bin/day11.rs | 194 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 194 insertions(+) create mode 100644 src/bin/day11.rs diff --git a/src/bin/day11.rs b/src/bin/day11.rs new file mode 100644 index 0000000..87b5b6e --- /dev/null +++ b/src/bin/day11.rs @@ -0,0 +1,194 @@ +use std::{collections::HashMap, time::Instant}; + +use aoc_2025::{load, print_res}; +use bstr::{BString, ByteSlice}; + +#[derive(PartialEq, Eq, Hash, Clone, Copy)] +pub struct Label([u8; 3]); + +impl std::fmt::Debug for Label { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_tuple("Label").field(&self.0.as_bstr()).finish() + } +} + +impl Label { + pub fn new(b: &[u8]) -> color_eyre::Result { + color_eyre::eyre::ensure!(b.len() == 3); + Ok(Self([b[0], b[1], b[2]])) + } +} + +type Parsed = HashMap>; + +#[inline(never)] +pub fn parsing(input: &BString) -> color_eyre::Result { + input + .lines() + .map(|l| { + let Some((label, downstream)) = l.split_once_str(b": ") else { + color_eyre::eyre::bail!("Malformed line: {}", l.as_bstr()); + }; + + Ok(( + Label::new(label)?, + downstream + .split_str(" ") + .map(Label::new) + .collect::>()?, + )) + }) + .collect() +} + +fn path_count(at: Label, graph: &Parsed, cache: &mut HashMap) -> usize { + if at == Label::new(b"out").unwrap() { + return 1; + } + + match cache.get(&at) { + Some(&v) => v, + None => { + let count = graph + .get(&at) + .unwrap() + .iter() + .map(|&v| path_count(v, graph, cache)) + .sum(); + cache.insert(at, count); + count + } + } +} + +#[inline(never)] +pub fn part1(input: Parsed) { + let mut cache = HashMap::new(); + let paths = path_count(Label::new(b"you").unwrap(), &input, &mut cache); + print_res!("Path from you -> out: {paths}"); +} + +#[allow(unused)] +fn paths_by( + current: Vec