Advent of Code solutions
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Day 11

Ben C 11bb1d95 4503c6bf

+116 -24
+14 -19
years/2025/src/day_10.rs
··· 24 24 let buttons = mach 25 25 .buttons 26 26 .iter() 27 - .map(|b| b.iter().map(|n| 2_usize.pow(*n as u32)).sum()) 27 + .map(|b| b.iter().map(|n| 1 << *n).fold(0, |acc, x| acc | x)) 28 28 .collect::<Vec<_>>(); 29 29 30 - let mut queue: VecDeque<(usize, usize, Option<usize>)> = 31 - VecDeque::with_capacity(30); 30 + let mut queue = VecDeque::with_capacity(30); 32 31 queue.push_front((0, 0, None)); 33 32 let mut guy = None; 34 33 while let Some((val, curr, prev)) = queue.pop_front() { ··· 78 77 79 78 let sol = problem.solve().expect("womp womp"); 80 79 81 - press_vars.iter().map(|&v| sol.value(v)).sum::<f64>() as usize 80 + press_vars 81 + .iter() 82 + .map(|&v| sol.value(v) as usize) 83 + .sum::<usize>() 82 84 }) 83 85 .sum::<usize>(); 84 86 ··· 90 92 .lines() 91 93 .map(|l| { 92 94 let split = l.split(' ').collect::<Vec<_>>(); 93 - let diag = split 94 - .first() 95 - .unwrap() 95 + 96 + let first = split[0]; 97 + let diag = first[1..(first.len() - 1)] 96 98 .chars() 97 - .filter(|c| *c == '.' || *c == '#') 98 99 .enumerate() 99 - .map(|(i, d)| { 100 - (if d == '.' { 0_usize } else { 1_usize }) * 2_usize.pow(i as u32) 101 - }) 102 - .sum(); 100 + .map(|(i, d)| (if d == '.' { 0_usize } else { 1_usize }) * (1 << i)) 101 + .fold(0, |acc, x| acc | x); 103 102 104 103 let buttons = split 105 104 .iter() 106 105 .take(split.len() - 1) 107 106 .skip(1) 108 107 .map(|b| { 109 - b.trim_matches('(') 110 - .trim_matches(')') 108 + b[1..b.len() - 1] 111 109 .split(',') 112 110 .map(|n| n.parse().unwrap()) 113 111 .collect() 114 112 }) 115 113 .collect(); 116 114 117 - let target_counters = split 118 - .last() 119 - .unwrap() 120 - .trim_matches('{') 121 - .trim_matches('}') 115 + let last = split[split.len() - 1]; 116 + let target_counters = last[1..last.len() - 1] 122 117 .split(',') 123 118 .map(|n| n.parse().unwrap()) 124 119 .collect();
+79 -5
years/2025/src/day_11.rs
··· 1 + use std::collections::HashMap; 2 + 1 3 use advent_core::{day_stuff, ex_for_day, Day}; 2 4 3 5 pub struct Day11; 4 6 7 + type Graph = HashMap<String, Vec<String>>; 8 + type Seen = HashMap<(String, bool, bool), usize>; 9 + 10 + fn all_paths_to_out(node: &String, graph: &Graph, seen: &mut HashMap<String, usize>) -> usize { 11 + if let Some(memo) = seen.get(node).copied() { 12 + memo 13 + } else if let Some(nexts) = graph.get(node) { 14 + let mut amnt = 0; 15 + for next in nexts.iter() { 16 + if next == "out" { 17 + amnt += 1; 18 + } else { 19 + amnt += all_paths_to_out(next, graph, seen); 20 + } 21 + } 22 + seen.insert(node.clone(), amnt); 23 + amnt 24 + } else { 25 + 0 26 + } 27 + } 28 + 29 + fn all_paths_to_out_with_constraints( 30 + node: &String, 31 + saw_dac: bool, 32 + saw_fft: bool, 33 + graph: &Graph, 34 + seen: &mut Seen, 35 + ) -> usize { 36 + if let Some(memo) = seen.get(&(node.clone(), saw_dac, saw_fft)).copied() { 37 + memo 38 + } else if let Some(nexts) = graph.get(node) { 39 + let mut amnt = 0; 40 + for next in nexts.iter() { 41 + if next == "out" && saw_fft && saw_dac { 42 + amnt += 1; 43 + } else { 44 + let is_dac = next == "dac"; 45 + let is_fft = next == "fft"; 46 + amnt += all_paths_to_out_with_constraints( 47 + next, 48 + saw_dac || is_dac, 49 + saw_fft || is_fft, 50 + graph, 51 + seen, 52 + ); 53 + } 54 + } 55 + seen.insert((node.clone(), saw_dac, saw_fft), amnt); 56 + amnt 57 + } else { 58 + 0 59 + } 60 + } 61 + 5 62 impl Day for Day11 { 6 - day_stuff!(11, "", ""); 63 + day_stuff!(11, "5", "2", Graph); 7 64 8 - fn part_1(_input: Self::Input) -> Option<String> { 9 - None 65 + fn part_1(input: Self::Input) -> Option<String> { 66 + let mut seen = HashMap::with_capacity(input.len()); 67 + let start = "you".to_string(); 68 + let ans = all_paths_to_out(&start, &input, &mut seen); 69 + Some(ans.to_string()) 10 70 } 11 71 12 - fn part_2(_input: Self::Input) -> Option<String> { 13 - None 72 + fn part_2(input: Self::Input) -> Option<String> { 73 + let mut seen = HashMap::with_capacity(input.len()); 74 + let start = "svr".to_string(); 75 + let ans = all_paths_to_out_with_constraints(&start, false, false, &input, &mut seen); 76 + Some(ans.to_string()) 77 + } 78 + 79 + fn parse_input(input: &str) -> Self::Input { 80 + input 81 + .lines() 82 + .map(|l| { 83 + let (k, v) = l.split_once(':').unwrap(); 84 + let v = v.trim().split(' ').map(str::to_string).collect::<Vec<_>>(); 85 + (k.to_string(), v) 86 + }) 87 + .collect() 14 88 } 15 89 }
+10
years/2025/src/examples/day_11/1.txt
··· 1 + aaa: you hhh 2 + you: bbb ccc 3 + bbb: ddd eee 4 + ccc: ddd eee fff 5 + ddd: ggg 6 + eee: out 7 + fff: out 8 + ggg: out 9 + hhh: ccc fff iii 10 + iii: out
+13
years/2025/src/examples/day_11/2.txt
··· 1 + svr: aaa bbb 2 + aaa: fft 3 + fft: ccc 4 + bbb: tty 5 + tty: ccc 6 + ccc: ddd eee 7 + ddd: hub 8 + hub: fff 9 + eee: dac 10 + dac: fff 11 + fff: ggg hhh 12 + ggg: out 13 + hhh: out