Advent of Code solutions
0
fork

Configure Feed

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

Day 10

Ben C 4503c6bf 4ee1fdec

+266 -54
+124
Cargo.lock
··· 36 36 ] 37 37 38 38 [[package]] 39 + name = "autocfg" 40 + version = "1.5.0" 41 + source = "registry+https://github.com/rust-lang/crates.io-index" 42 + checksum = "c08606f8c3cbf4ce6ec8e28fb0014a2c086708fe954eaa885384a6165172e7e8" 43 + 44 + [[package]] 39 45 name = "bumpalo" 40 46 version = "3.19.0" 41 47 source = "registry+https://github.com/rust-lang/crates.io-index" ··· 98 104 checksum = "34aa73646ffb006b8f5147f3dc182bd4bcb190227ce861fc4a4844bf8e3cb2c0" 99 105 100 106 [[package]] 107 + name = "fnv" 108 + version = "1.0.7" 109 + source = "registry+https://github.com/rust-lang/crates.io-index" 110 + checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" 111 + 112 + [[package]] 113 + name = "good_lp" 114 + version = "1.14.2" 115 + source = "registry+https://github.com/rust-lang/crates.io-index" 116 + checksum = "776aa1ba88ac058e78408c17f4dbff826a51ae08ed6642f71ca0edd7fe9383f3" 117 + dependencies = [ 118 + "fnv", 119 + "microlp", 120 + ] 121 + 122 + [[package]] 101 123 name = "indicatif" 102 124 version = "0.18.3" 103 125 source = "registry+https://github.com/rust-lang/crates.io-index" ··· 127 149 checksum = "37c93d8daa9d8a012fd8ab92f088405fb202ea0b6ab73ee2482ae66af4f42091" 128 150 129 151 [[package]] 152 + name = "log" 153 + version = "0.4.28" 154 + source = "registry+https://github.com/rust-lang/crates.io-index" 155 + checksum = "34080505efa8e45a4b816c349525ebe327ceaa8559756f0356cba97ef3bf7432" 156 + 157 + [[package]] 130 158 name = "macros" 131 159 version = "0.1.0" 132 160 dependencies = [ ··· 135 163 ] 136 164 137 165 [[package]] 166 + name = "matrixmultiply" 167 + version = "0.3.10" 168 + source = "registry+https://github.com/rust-lang/crates.io-index" 169 + checksum = "a06de3016e9fae57a36fd14dba131fccf49f74b40b7fbdb472f96e361ec71a08" 170 + dependencies = [ 171 + "autocfg", 172 + "rawpointer", 173 + ] 174 + 175 + [[package]] 138 176 name = "memchr" 139 177 version = "2.7.6" 140 178 source = "registry+https://github.com/rust-lang/crates.io-index" 141 179 checksum = "f52b00d39961fc5b2736ea853c9cc86238e165017a493d1d5c8eac6bdc4cc273" 142 180 143 181 [[package]] 182 + name = "microlp" 183 + version = "0.2.11" 184 + source = "registry+https://github.com/rust-lang/crates.io-index" 185 + checksum = "51d1790c73b93164ff65868f63164497cb32339458a9297e17e212d91df62258" 186 + dependencies = [ 187 + "log", 188 + "sprs", 189 + ] 190 + 191 + [[package]] 192 + name = "ndarray" 193 + version = "0.17.1" 194 + source = "registry+https://github.com/rust-lang/crates.io-index" 195 + checksum = "0c7c9125e8f6f10c9da3aad044cc918cf8784fa34de857b1aa68038eb05a50a9" 196 + dependencies = [ 197 + "matrixmultiply", 198 + "num-complex", 199 + "num-integer", 200 + "num-traits", 201 + "portable-atomic", 202 + "portable-atomic-util", 203 + "rawpointer", 204 + ] 205 + 206 + [[package]] 207 + name = "num-complex" 208 + version = "0.4.6" 209 + source = "registry+https://github.com/rust-lang/crates.io-index" 210 + checksum = "73f88a1307638156682bada9d7604135552957b7818057dcef22705b4d509495" 211 + dependencies = [ 212 + "num-traits", 213 + ] 214 + 215 + [[package]] 216 + name = "num-integer" 217 + version = "0.1.46" 218 + source = "registry+https://github.com/rust-lang/crates.io-index" 219 + checksum = "7969661fd2958a5cb096e56c8e1ad0444ac2bbcd0061bd28660485a44879858f" 220 + dependencies = [ 221 + "num-traits", 222 + ] 223 + 224 + [[package]] 225 + name = "num-traits" 226 + version = "0.2.19" 227 + source = "registry+https://github.com/rust-lang/crates.io-index" 228 + checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" 229 + dependencies = [ 230 + "autocfg", 231 + ] 232 + 233 + [[package]] 144 234 name = "once_cell" 145 235 version = "1.21.3" 146 236 source = "registry+https://github.com/rust-lang/crates.io-index" ··· 153 243 checksum = "f84267b20a16ea918e43c6a88433c2d54fa145c92a811b5b047ccbe153674483" 154 244 155 245 [[package]] 246 + name = "portable-atomic-util" 247 + version = "0.2.4" 248 + source = "registry+https://github.com/rust-lang/crates.io-index" 249 + checksum = "d8a2f0d8d040d7848a709caf78912debcc3f33ee4b3cac47d73d1e1069e83507" 250 + dependencies = [ 251 + "portable-atomic", 252 + ] 253 + 254 + [[package]] 156 255 name = "proc-macro2" 157 256 version = "1.0.103" 158 257 source = "registry+https://github.com/rust-lang/crates.io-index" ··· 169 268 dependencies = [ 170 269 "proc-macro2", 171 270 ] 271 + 272 + [[package]] 273 + name = "rawpointer" 274 + version = "0.2.1" 275 + source = "registry+https://github.com/rust-lang/crates.io-index" 276 + checksum = "60a357793950651c4ed0f3f52338f53b2f809f32d83a07f72909fa13e4c6c1e3" 172 277 173 278 [[package]] 174 279 name = "rayon" ··· 224 329 version = "1.0.22" 225 330 source = "registry+https://github.com/rust-lang/crates.io-index" 226 331 checksum = "b39cdef0fa800fc44525c84ccb54a029961a8215f9619753635a9c0d2538d46d" 332 + 333 + [[package]] 334 + name = "smallvec" 335 + version = "1.15.1" 336 + source = "registry+https://github.com/rust-lang/crates.io-index" 337 + checksum = "67b1b7a3b5fe4f1376887184045fcf45c69e92af734b7aaddc05fb777b6fbd03" 338 + 339 + [[package]] 340 + name = "sprs" 341 + version = "0.11.4" 342 + source = "registry+https://github.com/rust-lang/crates.io-index" 343 + checksum = "6dca58a33be2188d4edc71534f8bafa826e787cc28ca1c47f31be3423f0d6e55" 344 + dependencies = [ 345 + "ndarray", 346 + "num-complex", 347 + "num-traits", 348 + "smallvec", 349 + ] 227 350 228 351 [[package]] 229 352 name = "syn" ··· 349 472 dependencies = [ 350 473 "advent-hack", 351 474 "advent_core", 475 + "good_lp", 352 476 "macros", 353 477 "rayon", 354 478 "utils",
+9 -9
flake.lock
··· 21 21 "rust-analyzer-src": "rust-analyzer-src" 22 22 }, 23 23 "locked": { 24 - "lastModified": 1765003156, 25 - "narHash": "sha256-k4YrPUhRj7Ciq385vREU57RHiDFycY5RaJwdCOmsLhU=", 24 + "lastModified": 1765252472, 25 + "narHash": "sha256-byMt/uMi7DJ8tRniFopDFZMO3leSjGp6GS4zWOFT+uQ=", 26 26 "owner": "nix-community", 27 27 "repo": "fenix", 28 - "rev": "e8361cc010853d17740a63aae00385061ac9de51", 28 + "rev": "8456b985f6652e3eef0632ee9992b439735c5544", 29 29 "type": "github" 30 30 }, 31 31 "original": { ··· 54 54 }, 55 55 "nixpkgs": { 56 56 "locked": { 57 - "lastModified": 1764667669, 58 - "narHash": "sha256-7WUCZfmqLAssbDqwg9cUDAXrSoXN79eEEq17qhTNM/Y=", 57 + "lastModified": 1764950072, 58 + "narHash": "sha256-BmPWzogsG2GsXZtlT+MTcAWeDK5hkbGRZTeZNW42fwA=", 59 59 "owner": "nixos", 60 60 "repo": "nixpkgs", 61 - "rev": "418468ac9527e799809c900eda37cbff999199b6", 61 + "rev": "f61125a668a320878494449750330ca58b78c557", 62 62 "type": "github" 63 63 }, 64 64 "original": { ··· 111 111 "rust-analyzer-src": { 112 112 "flake": false, 113 113 "locked": { 114 - "lastModified": 1764925941, 115 - "narHash": "sha256-zldc1SrUIhGMdQp+0woSqvBS51Mi8PW6JukONBQXZBY=", 114 + "lastModified": 1765120009, 115 + "narHash": "sha256-nG76b87rkaDzibWbnB5bYDm6a52b78A+fpm+03pqYIw=", 116 116 "owner": "rust-lang", 117 117 "repo": "rust-analyzer", 118 - "rev": "2cbf3587d36dfc7b701ebb744d3dd5064355d04f", 118 + "rev": "5e3e9c4e61bba8a5e72134b9ffefbef8f531d008", 119 119 "type": "github" 120 120 }, 121 121 "original": {
-40
justfile
··· 1 - 2 - _default: 3 - @just --list --unsorted --justfile {{justfile()}} 4 - 5 - checkall: 6 - nix flake check 7 - 8 - year := `date +%Y` 9 - day := `nu -c 'date now | format date "%_d" | str trim'` 10 - 11 - # Run a specific part of today's problem 12 - p P in="" ARGS="": 13 - cargo run {{ARGS}} -- solve {{year}}:{{day}}:{{P}} {{in}} 14 - 15 - # Run a specific day and part of this year 16 - dp DP in="" ARGS="": 17 - cargo run {{ARGS}} -- solve {{year}}:{{DP}} {{in}} 18 - 19 - # Run a specific year's day's part 20 - dyp DYP in="" ARGS="": 21 - cargo run {{ARGS}} -- solve {{DYP}} {{in}} 22 - 23 - # Create a new year crate 24 - prep: 25 - cargo run --release -- new {{year}} 26 - 27 - # Test today's solution against examples 28 - test: 29 - cargo test -p y_{{year}} --release day_{{day}}_part_ 30 - 31 - # Test all of this year's solutions against examples 32 - test-all: 33 - cargo test -p y_{{year}} --release 34 - 35 - # Open Editor to today's file 36 - start: 37 - nvim years/{{year}}/src/day_{{day}}.rs 38 - 39 - hakari: 40 - cargo hakari generate
+3
years/2025/Cargo.toml
··· 10 10 rayon = "1.11.0" 11 11 utils = { path = "../../utils" } 12 12 advent-hack = { version = "0.1", path = "../../advent-hack" } 13 + good_lp = { version = "1.14.2", features = [ 14 + "microlp", 15 + ], default-features = false }
+124 -5
years/2025/src/day_10.rs
··· 1 + use std::collections::VecDeque; 2 + 1 3 use advent_core::{day_stuff, ex_for_day, Day}; 4 + use rayon::iter::{IntoParallelIterator, ParallelIterator}; 5 + 6 + use good_lp::*; 2 7 3 8 pub struct Day10; 4 9 10 + #[derive(Debug, Clone)] 11 + pub struct Machine { 12 + target_state: usize, 13 + buttons: Vec<Vec<usize>>, 14 + target_counters: Vec<usize>, 15 + } 16 + 5 17 impl Day for Day10 { 6 - day_stuff!(10, "", ""); 18 + day_stuff!(10, "7", "33", Vec<Machine>); 19 + 20 + fn part_1(input: Self::Input) -> Option<String> { 21 + let ans = input 22 + .into_par_iter() 23 + .map(|mach| { 24 + let buttons = mach 25 + .buttons 26 + .iter() 27 + .map(|b| b.iter().map(|n| 2_usize.pow(*n as u32)).sum()) 28 + .collect::<Vec<_>>(); 29 + 30 + let mut queue: VecDeque<(usize, usize, Option<usize>)> = 31 + VecDeque::with_capacity(30); 32 + queue.push_front((0, 0, None)); 33 + let mut guy = None; 34 + while let Some((val, curr, prev)) = queue.pop_front() { 35 + if val == mach.target_state { 36 + guy = Some(curr); 37 + break; 38 + } 39 + 40 + for butt in buttons.iter().copied() { 41 + if prev.is_none_or(|v| v != butt) { 42 + queue.push_back((val ^ butt, curr + 1, Some(butt))); 43 + } 44 + } 45 + } 46 + guy.expect("womp womp") 47 + }) 48 + .sum::<usize>(); 49 + 50 + Some(ans.to_string()) 51 + } 52 + 53 + fn part_2(input: Self::Input) -> Option<String> { 54 + let ans = input 55 + .into_par_iter() 56 + .map(|mach| { 57 + let mut vars = variables!(); 58 + let press_vars = mach 59 + .buttons 60 + .iter() 61 + .map(|_| vars.add(variable().min(0).integer())) 62 + .collect::<Vec<_>>(); 63 + 64 + let mut problem = vars 65 + .minimise(press_vars.iter().sum::<Expression>()) 66 + .using(microlp); 67 + 68 + let mut exprs = vec![0.into_expression(); mach.target_counters.len()]; 69 + for (i, butt) in mach.buttons.iter().enumerate() { 70 + for &x in butt { 71 + exprs[x] += press_vars[i]; 72 + } 73 + } 7 74 8 - fn part_1(_input: Self::Input) -> Option<String> { 9 - None 75 + for (e, j) in exprs.into_iter().zip(mach.target_counters) { 76 + problem.add_constraint(e.eq(j as f64)); 77 + } 78 + 79 + let sol = problem.solve().expect("womp womp"); 80 + 81 + press_vars.iter().map(|&v| sol.value(v)).sum::<f64>() as usize 82 + }) 83 + .sum::<usize>(); 84 + 85 + Some(ans.to_string()) 10 86 } 11 87 12 - fn part_2(_input: Self::Input) -> Option<String> { 13 - None 88 + fn parse_input(input: &str) -> Self::Input { 89 + input 90 + .lines() 91 + .map(|l| { 92 + let split = l.split(' ').collect::<Vec<_>>(); 93 + let diag = split 94 + .first() 95 + .unwrap() 96 + .chars() 97 + .filter(|c| *c == '.' || *c == '#') 98 + .enumerate() 99 + .map(|(i, d)| { 100 + (if d == '.' { 0_usize } else { 1_usize }) * 2_usize.pow(i as u32) 101 + }) 102 + .sum(); 103 + 104 + let buttons = split 105 + .iter() 106 + .take(split.len() - 1) 107 + .skip(1) 108 + .map(|b| { 109 + b.trim_matches('(') 110 + .trim_matches(')') 111 + .split(',') 112 + .map(|n| n.parse().unwrap()) 113 + .collect() 114 + }) 115 + .collect(); 116 + 117 + let target_counters = split 118 + .last() 119 + .unwrap() 120 + .trim_matches('{') 121 + .trim_matches('}') 122 + .split(',') 123 + .map(|n| n.parse().unwrap()) 124 + .collect(); 125 + 126 + Machine { 127 + target_state: diag, 128 + buttons, 129 + target_counters, 130 + } 131 + }) 132 + .collect() 14 133 } 15 134 }
+3
years/2025/src/examples/day_10/1.txt
··· 1 + [.##.] (3) (1,3) (2) (2,3) (0,2) (0,1) {3,5,4,7} 2 + [...#.] (0,2,3,4) (2,3) (0,4) (0,1,2) (1,2,3,4) {7,5,12,7,2} 3 + [.###.#] (0,1,2,3,4) (0,3,4) (0,1,2,4,5) (1,2) {10,11,11,5,10,5}
+3
years/2025/src/examples/day_10/2.txt
··· 1 + [.##.] (3) (1,3) (2) (2,3) (0,2) (0,1) {3,5,4,7} 2 + [...#.] (0,2,3,4) (2,3) (0,4) (0,1,2) (1,2,3,4) {7,5,12,7,2} 3 + [.###.#] (0,1,2,3,4) (0,3,4) (0,1,2,4,5) (1,2) {10,11,11,5,10,5}