diff --git a/2023/Cargo.lock b/2023/Cargo.lock index f503427..178fc9a 100644 --- a/2023/Cargo.lock +++ b/2023/Cargo.lock @@ -197,6 +197,114 @@ dependencies = [ "slab", ] +[[package]] +name = "glam" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "333928d5eb103c5d4050533cec0384302db6be8ef7d3cebd30ec6a35350353da" + +[[package]] +name = "glam" +version = "0.15.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3abb554f8ee44336b72d522e0a7fe86a29e09f839a36022fa869a7dfe941a54b" + +[[package]] +name = "glam" +version = "0.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4126c0479ccf7e8664c36a2d719f5f2c140fbb4f9090008098d2c291fa5b3f16" + +[[package]] +name = "glam" +version = "0.17.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e01732b97afd8508eee3333a541b9f7610f454bb818669e66e90f5f57c93a776" + +[[package]] +name = "glam" +version = "0.18.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "525a3e490ba77b8e326fb67d4b44b4bd2f920f44d4cc73ccec50adc68e3bee34" + +[[package]] +name = "glam" +version = "0.19.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b8509e6791516e81c1a630d0bd7fbac36d2fa8712a9da8662e716b52d5051ca" + +[[package]] +name = "glam" +version = "0.20.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f43e957e744be03f5801a55472f593d43fabdebf25a4585db250f04d86b1675f" + +[[package]] +name = "glam" +version = "0.21.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "518faa5064866338b013ff9b2350dc318e14cc4fcd6cb8206d7e7c9886c98815" + +[[package]] +name = "glam" +version = "0.22.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "12f597d56c1bd55a811a1be189459e8fad2bbc272616375602443bdfb37fa774" + +[[package]] +name = "glam" +version = "0.23.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e4afd9ad95555081e109fe1d21f2a30c691b5f0919c67dfa690a2e1eb6bd51c" + +[[package]] +name = "glam" +version = "0.24.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b5418c17512bdf42730f9032c74e1ae39afc408745ebb2acf72fbc4691c17945" + +[[package]] +name = "glam" +version = "0.25.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "151665d9be52f9bb40fc7966565d39666f2d1e69233571b71b87791c7e0528b3" + +[[package]] +name = "glam" +version = "0.27.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9e05e7e6723e3455f4818c7b26e855439f7546cf617ef669d1adedb8669e5cb9" + +[[package]] +name = "glam" +version = "0.28.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "779ae4bf7e8421cf91c0b3b64e7e8b40b862fba4d393f59150042de7c4965a94" + +[[package]] +name = "glam" +version = "0.29.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8babf46d4c1c9d92deac9f7be466f76dfc4482b6452fc5024b5e8daf6ffeb3ee" + +[[package]] +name = "glam" +version = "0.30.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "19fc433e8437a212d1b6f1e68c7824af3aed907da60afa994e7f542d18d12aa9" + +[[package]] +name = "glam" +version = "0.31.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "556f6b2ea90b8d15a74e0e7bb41671c9bdf38cd9f78c284d750b9ce58a2b5be7" + +[[package]] +name = "glam" +version = "0.32.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f70749695b063ecbf6b62949ccccde2e733ec3ecbbd71d467dca4e5c6c97cca0" + [[package]] name = "glob" version = "0.3.4" @@ -306,11 +414,29 @@ checksum = "cf8baf1c55e62ffcace7a9f06f4bd9cd3f0c4beb022d3b367256b91b87513d98" [[package]] name = "nalgebra" -version = "0.31.4" +version = "0.34.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "20bd243ab3dbb395b39ee730402d2e5405e448c75133ec49cc977762c4cba3d1" +checksum = "df76ea0ff5c7e6b88689085804d6132ded0ddb9de5ca5b8aeb9eeadc0508a70a" dependencies = [ "approx", + "glam 0.14.0", + "glam 0.15.2", + "glam 0.16.0", + "glam 0.17.3", + "glam 0.18.0", + "glam 0.19.0", + "glam 0.20.5", + "glam 0.21.3", + "glam 0.22.0", + "glam 0.23.0", + "glam 0.24.2", + "glam 0.25.0", + "glam 0.27.0", + "glam 0.28.0", + "glam 0.29.3", + "glam 0.30.10", + "glam 0.31.1", + "glam 0.32.1", "matrixmultiply", "nalgebra-macros", "num-complex", @@ -322,13 +448,13 @@ dependencies = [ [[package]] name = "nalgebra-macros" -version = "0.1.0" +version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "01fcc0b8149b4632adc89ac3b7b31a12fb6099a0317a4eb2ebff574ef7de7218" +checksum = "973e7178a678cfd059ccec50887658d482ce16b0aa9da3888ddeab5cd5eb4889" dependencies = [ "proc-macro2", "quote", - "syn 1.0.109", + "syn 2.0.119", ] [[package]] @@ -446,9 +572,9 @@ checksum = "f120c8d5021b34abd2f2ffd7ee96687cbcd6c54a3e68d86b6169f4a5cd480ffe" [[package]] name = "polyfit-rs" -version = "0.2.1" +version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ab98d9704c7300e37472a6790a447eaf501d664a1889737faa53c26790d2b697" +checksum = "e6bf78964c64ab1e28e9901f81b35d958daa1852e381241ce4e8daf93c9caa05" dependencies = [ "nalgebra", ] @@ -656,9 +782,9 @@ dependencies = [ [[package]] name = "simba" -version = "0.7.3" +version = "0.9.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2f3fd720c48c53cace224ae62bef1bbff363a70c68c4802a78b5cc6159618176" +checksum = "c99284beb21666094ba2b75bbceda012e610f5479dfcc2d6e2426f53197ffd95" dependencies = [ "approx", "num-complex", diff --git a/2023/Cargo.toml b/2023/Cargo.toml index bf93bc4..0fff452 100644 --- a/2023/Cargo.toml +++ b/2023/Cargo.toml @@ -13,7 +13,7 @@ ndarray = "0.15.6" num = "0.4.1" num-traits = "0.2.17" petgraph = "0.6.4" -polyfit-rs = "=0.2.1" +polyfit-rs = "=0.2.2" primes = "0.3.0" rayon = "1.11.0" regex = "1.11.1" diff --git a/2023/src/day21.rs b/2023/src/day21.rs index 321681e..ba033c0 100644 --- a/2023/src/day21.rs +++ b/2023/src/day21.rs @@ -1,8 +1,9 @@ use aoc_runner_derive::aoc; use core::panic; use indicatif::{ProgressBar, ProgressStyle}; +use itertools::Itertools; use rayon::prelude::*; -use std::collections::HashSet; +use std::collections::{HashMap, HashSet, VecDeque}; use std::str::Lines; // PARSE @@ -89,31 +90,31 @@ impl GardenMap { } panic!("didn't find the start square!"); } - fn reachable_after(&self, from: &Position, n: usize) -> u64 { - let bar = ProgressBar::new(n as u64).with_style( - ProgressStyle::with_template( - "[{elapsed_precise}/{eta_precise}] {bar:40.cyan/blue} {pos:>7}/{len:7} {per_sec}", - ) - .unwrap(), - ); - let mut visited_after: Vec> = Vec::new(); - visited_after.push(HashSet::from([*from])); - for i in 1..n + 1 { - visited_after.push( - visited_after[i - 1] - .iter() - .flat_map(|last| self.adjacent_to(last)) - .collect(), - ); - bar.inc(1); - // if primes::is_prime(i as u64) { - // println!("count after {} steps: {}", i, visited_after[i].len()); - // } - } + fn distances(&self, from: &Position, n: usize) -> HashMap { + let mut map = HashMap::new(); + let mut q = VecDeque::new(); - visited_after[n].len() as u64 + q.push_back((*from, 0)); + + while let Some((next, dist)) = q.pop_front() { + if dist > n { + continue; + } + if !map.contains_key(&next) { + map.insert(next, dist); + for adj in self.adjacent_to(&next) { + q.push_back((adj, dist + 1)) + } + } + } + map } - fn reachable_count_after(&self, from: &Position, n: usize) -> u64 { + fn reachable_after(&mut self, from: &Position, n: usize) -> u64 { + let distances = self.distances(from, n); + + distances.values().filter(|d| **d <= n && **d % 2 == n % 2).count() as u64 + } + fn reachable_count_after(&mut self, from: &Position, n: usize) -> u64 { let dim = self.width() as f64; let target_mod = (n % self.width()) as f64; let x_values: Vec = vec![target_mod, target_mod + dim, target_mod + 2. * dim]; @@ -135,9 +136,9 @@ impl GardenMap { fn draw_with_bounds(&self, from: &Position, to: &Position) { for row in from.1..to.1 + 1 { for col in from.0..to.0 + 1 { - // print!("{}", self.at(&(col, row)).c); + print!("{}", self.at(&(col, row)).c); } - // println!(); + println!(); } } } @@ -154,13 +155,14 @@ impl GardenMap { // PROBLEM 1 solution fn problem1_impl(input: &str, n: usize) -> u64 { - let map = GardenMap::from(input.lines()); + let mut map = GardenMap::from(input.lines()); // println!("map: {:?} start: {:?}", map.map, &map.start); // map.draw_with_bounds( // &(-(map.width() as isize), -(map.height() as isize)), // &(map.width() as isize * 2 + 1, map.height() as isize * 2 + 1), // ); - map.reachable_count_after(&map.start, n) + let start = map.start; + map.reachable_after(&start, n) } #[aoc(day21, part1)] @@ -171,7 +173,9 @@ fn part1(input: &str) -> u64 { // PROBLEM 2 solution #[aoc(day21, part2)] fn part2(input: &str) -> u64 { - problem1_impl(input, 26501365) + let mut map = GardenMap::from(input.lines()); + let start = map.start; + map.reachable_count_after(&start, 26501365) } #[cfg(test)] diff --git a/2026/src/lib.rs b/2026/src/lib.rs index b498953..8df955b 100644 --- a/2026/src/lib.rs +++ b/2026/src/lib.rs @@ -1,5 +1,6 @@ mod day1; mod day2; +mod day3; use aoc_runner_derive::aoc_lib; diff --git a/README.md b/README.md index 2a0b1e4..a231063 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@

- Advent of Code - 142/554 ⭐ + Advent of Code - 142/556 ⭐

2026 - 4 ⭐ - Rust