Advent of Code solutions

Day 11: Some cleanup

bwc9876.dev 9ae06fd4 95726946

verified
+13 -16
+13 -16
years/2025/src/day_11.rs
··· 5 5 pub struct Day11; 6 6 7 7 type Graph = HashMap<String, Vec<String>>; 8 - type Seen<'a> = HashMap<(&'a String, bool, bool), usize>; 8 + type Seen<'a> = HashMap<&'a String, usize>; 9 + type SeenConstrained<'a> = HashMap<(&'a String, bool, bool), usize>; 9 10 10 - fn all_paths_to_out<'a>( 11 - node: &'a String, 12 - graph: &'a Graph, 13 - seen: &mut HashMap<&'a String, usize>, 14 - ) -> usize { 11 + fn all_paths_to_out<'a>(node: &'a String, graph: &'a Graph, seen: &mut Seen<'a>) -> usize { 15 12 if let Some(memo) = seen.get(&node) { 16 13 *memo 17 14 } else if let Some(nexts) = graph.get(node) { ··· 30 27 } 31 28 } 32 29 33 - fn all_paths_to_out_with_constraints<'a>( 30 + fn all_paths_to_out_constrained<'a>( 34 31 node: &'a String, 35 32 saw_dac: bool, 36 33 saw_fft: bool, 37 34 graph: &'a Graph, 38 - seen: &mut Seen<'a>, 35 + seen: &mut SeenConstrained<'a>, 39 36 ) -> usize { 40 37 if let Some(memo) = seen.get(&(node, saw_dac, saw_fft)) { 41 38 *memo 42 39 } else if let Some(nexts) = graph.get(node) { 43 40 let mut amnt = 0; 44 41 for next in nexts.iter() { 45 - if next == "out" && saw_fft && saw_dac { 46 - amnt += 1; 42 + if next == "out" { 43 + if saw_dac && saw_fft { 44 + amnt += 1; 45 + } 47 46 } else { 48 - let is_dac = next == "dac"; 49 - let is_fft = next == "fft"; 50 - amnt += all_paths_to_out_with_constraints( 47 + amnt += all_paths_to_out_constrained( 51 48 next, 52 - saw_dac || is_dac, 53 - saw_fft || is_fft, 49 + saw_dac || next == "dac", 50 + saw_fft || next == "fft", 54 51 graph, 55 52 seen, 56 53 ); ··· 76 73 fn part_2(input: Self::Input) -> Option<String> { 77 74 let mut seen = HashMap::with_capacity(input.len()); 78 75 let start = "svr".to_string(); 79 - let ans = all_paths_to_out_with_constraints(&start, false, false, &input, &mut seen); 76 + let ans = all_paths_to_out_constrained(&start, false, false, &input, &mut seen); 80 77 Some(ans.to_string()) 81 78 } 82 79