From 11bb1d95aacee6526598e11cb95332aa99f711e7 Mon Sep 17 00:00:00 2001 From: Ben C Date: Thu, 11 Dec 2025 07:54:42 -0500 Subject: [PATCH] Day 11 --- years/2025/src/day_10.rs | 33 +++++------ years/2025/src/day_11.rs | 84 ++++++++++++++++++++++++++-- years/2025/src/examples/day_11/1.txt | 10 ++++ years/2025/src/examples/day_11/2.txt | 13 +++++ 4 files changed, 116 insertions(+), 24 deletions(-) diff --git a/years/2025/src/day_10.rs b/years/2025/src/day_10.rs index ed01672..3bd240b 100644 --- a/years/2025/src/day_10.rs +++ b/years/2025/src/day_10.rs @@ -24,11 +24,10 @@ impl Day for Day10 { let buttons = mach .buttons .iter() - .map(|b| b.iter().map(|n| 2_usize.pow(*n as u32)).sum()) + .map(|b| b.iter().map(|n| 1 << *n).fold(0, |acc, x| acc | x)) .collect::>(); - let mut queue: VecDeque<(usize, usize, Option)> = - VecDeque::with_capacity(30); + let mut queue = VecDeque::with_capacity(30); queue.push_front((0, 0, None)); let mut guy = None; while let Some((val, curr, prev)) = queue.pop_front() { @@ -78,7 +77,10 @@ impl Day for Day10 { let sol = problem.solve().expect("womp womp"); - press_vars.iter().map(|&v| sol.value(v)).sum::() as usize + press_vars + .iter() + .map(|&v| sol.value(v) as usize) + .sum::() }) .sum::(); @@ -90,35 +92,28 @@ impl Day for Day10 { .lines() .map(|l| { let split = l.split(' ').collect::>(); - let diag = split - .first() - .unwrap() + + let first = split[0]; + let diag = first[1..(first.len() - 1)] .chars() - .filter(|c| *c == '.' || *c == '#') .enumerate() - .map(|(i, d)| { - (if d == '.' { 0_usize } else { 1_usize }) * 2_usize.pow(i as u32) - }) - .sum(); + .map(|(i, d)| (if d == '.' { 0_usize } else { 1_usize }) * (1 << i)) + .fold(0, |acc, x| acc | x); let buttons = split .iter() .take(split.len() - 1) .skip(1) .map(|b| { - b.trim_matches('(') - .trim_matches(')') + b[1..b.len() - 1] .split(',') .map(|n| n.parse().unwrap()) .collect() }) .collect(); - let target_counters = split - .last() - .unwrap() - .trim_matches('{') - .trim_matches('}') + let last = split[split.len() - 1]; + let target_counters = last[1..last.len() - 1] .split(',') .map(|n| n.parse().unwrap()) .collect(); diff --git a/years/2025/src/day_11.rs b/years/2025/src/day_11.rs index 46d6808..b8f5485 100644 --- a/years/2025/src/day_11.rs +++ b/years/2025/src/day_11.rs @@ -1,15 +1,89 @@ +use std::collections::HashMap; + use advent_core::{day_stuff, ex_for_day, Day}; pub struct Day11; +type Graph = HashMap>; +type Seen = HashMap<(String, bool, bool), usize>; + +fn all_paths_to_out(node: &String, graph: &Graph, seen: &mut HashMap) -> usize { + if let Some(memo) = seen.get(node).copied() { + memo + } else if let Some(nexts) = graph.get(node) { + let mut amnt = 0; + for next in nexts.iter() { + if next == "out" { + amnt += 1; + } else { + amnt += all_paths_to_out(next, graph, seen); + } + } + seen.insert(node.clone(), amnt); + amnt + } else { + 0 + } +} + +fn all_paths_to_out_with_constraints( + node: &String, + saw_dac: bool, + saw_fft: bool, + graph: &Graph, + seen: &mut Seen, +) -> usize { + if let Some(memo) = seen.get(&(node.clone(), saw_dac, saw_fft)).copied() { + memo + } else if let Some(nexts) = graph.get(node) { + let mut amnt = 0; + for next in nexts.iter() { + if next == "out" && saw_fft && saw_dac { + amnt += 1; + } else { + let is_dac = next == "dac"; + let is_fft = next == "fft"; + amnt += all_paths_to_out_with_constraints( + next, + saw_dac || is_dac, + saw_fft || is_fft, + graph, + seen, + ); + } + } + seen.insert((node.clone(), saw_dac, saw_fft), amnt); + amnt + } else { + 0 + } +} + impl Day for Day11 { - day_stuff!(11, "", ""); + day_stuff!(11, "5", "2", Graph); + + fn part_1(input: Self::Input) -> Option { + let mut seen = HashMap::with_capacity(input.len()); + let start = "you".to_string(); + let ans = all_paths_to_out(&start, &input, &mut seen); + Some(ans.to_string()) + } - fn part_1(_input: Self::Input) -> Option { - None + fn part_2(input: Self::Input) -> Option { + let mut seen = HashMap::with_capacity(input.len()); + let start = "svr".to_string(); + let ans = all_paths_to_out_with_constraints(&start, false, false, &input, &mut seen); + Some(ans.to_string()) } - fn part_2(_input: Self::Input) -> Option { - None + fn parse_input(input: &str) -> Self::Input { + input + .lines() + .map(|l| { + let (k, v) = l.split_once(':').unwrap(); + let v = v.trim().split(' ').map(str::to_string).collect::>(); + (k.to_string(), v) + }) + .collect() } } diff --git a/years/2025/src/examples/day_11/1.txt b/years/2025/src/examples/day_11/1.txt index e69de29..01e5b43 100644 --- a/years/2025/src/examples/day_11/1.txt +++ b/years/2025/src/examples/day_11/1.txt @@ -0,0 +1,10 @@ +aaa: you hhh +you: bbb ccc +bbb: ddd eee +ccc: ddd eee fff +ddd: ggg +eee: out +fff: out +ggg: out +hhh: ccc fff iii +iii: out diff --git a/years/2025/src/examples/day_11/2.txt b/years/2025/src/examples/day_11/2.txt index e69de29..d787665 100644 --- a/years/2025/src/examples/day_11/2.txt +++ b/years/2025/src/examples/day_11/2.txt @@ -0,0 +1,13 @@ +svr: aaa bbb +aaa: fft +fft: ccc +bbb: tty +tty: ccc +ccc: ddd eee +ddd: hub +hub: fff +eee: dac +dac: fff +fff: ggg hhh +ggg: out +hhh: out -- 2.51.2