diff --git a/Cargo.lock b/Cargo.lock index 1ff9aa1..1422a84 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8,14 +8,12 @@ version = "0.9.2" dependencies = [ "hashlink", "itertools", - "lazy_static", "nom", "num-traits", "ordered-float", "pico-args", "rstar", "strum", - "strum_macros", "tinyvec", ] @@ -31,21 +29,6 @@ dependencies = [ "zerocopy", ] -[[package]] -name = "allocator-api2" -version = "0.2.16" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0942ffc6dcaadf03badf6e6a2d0228460359d5e34b57ccdc720b7382dfbd5ec5" - -[[package]] -name = "atomic-polyfill" -version = "1.0.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8cf2bce30dfe09ef0bfaef228b9d414faaf7e563035494d7fe092dba54b300f4" -dependencies = [ - "critical-section", -] - [[package]] name = "autocfg" version = "1.1.0" @@ -64,12 +47,6 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" -[[package]] -name = "critical-section" -version = "1.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7059fff8937831a9ae6f0fe4d658ffabf58f2ca96aa9dec1c889f936f705f216" - [[package]] name = "either" version = "1.9.0" @@ -78,9 +55,9 @@ checksum = "a26ae43d7bcc3b814de94796a5e736d4029efb0ee900c12e2d54c993ad1a1e07" [[package]] name = "hash32" -version = "0.2.1" +version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b0c35f58762feb77d74ebe43bdbc3210f09be9fe6742234d573bacc26ed92b67" +checksum = "47d60b12902ba28e2730cd37e95b8c9223af2808df9e902d4df49588d1470606" dependencies = [ "byteorder", ] @@ -92,68 +69,48 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "290f1a1d9242c78d09ce40a5e87e7554ee637af1351968159f4952f028f75604" dependencies = [ "ahash", - "allocator-api2", ] [[package]] name = "hashlink" -version = "0.8.4" +version = "0.9.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e8094feaf31ff591f651a2664fb9cfd92bba7a60ce3197265e9482ebe753c8f7" +checksum = "6ba4ff7128dee98c7dc9794b6a411377e1404dba1c97deb8d1a55297bd25d8af" dependencies = [ "hashbrown", ] [[package]] name = "heapless" -version = "0.7.17" +version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cdc6457c0eb62c71aac4bc17216026d8410337c4126773b9c5daba343f17964f" +checksum = "0bfb9eb618601c89945a70e254898da93b13be0388091d42117462b265bb3fad" dependencies = [ - "atomic-polyfill", "hash32", - "rustc_version", - "spin", "stable_deref_trait", ] [[package]] name = "heck" -version = "0.4.1" +version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" +checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" [[package]] name = "itertools" -version = "0.12.0" +version = "0.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "25db6b064527c5d482d0423354fcd07a89a2dfe07b67892e62411946db7f07b0" +checksum = "413ee7dfc52ee1a4949ceeb7dbc8a33f2d6c088194d9f922fb8318faf1f01186" dependencies = [ "either", ] -[[package]] -name = "lazy_static" -version = "1.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" - [[package]] name = "libm" version = "0.2.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4ec2a862134d2a7d32d7983ddcdd1c4923530833c9f2ea1a44fc5fa473989058" -[[package]] -name = "lock_api" -version = "0.4.11" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3c168f8615b12bc01f9c17e2eb0cc07dcae1940121185446edc3744920e8ef45" -dependencies = [ - "autocfg", - "scopeguard", -] - [[package]] name = "memchr" version = "2.6.4" @@ -178,9 +135,9 @@ dependencies = [ [[package]] name = "num-traits" -version = "0.2.17" +version = "0.2.19" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "39e3200413f237f41ab11ad6d161bc7239c84dcb631773ccd7de3dfe4b5c267c" +checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" dependencies = [ "autocfg", "libm", @@ -194,9 +151,9 @@ checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92" [[package]] name = "ordered-float" -version = "4.2.0" +version = "4.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a76df7075c7d4d01fdcb46c912dd17fba5b60c78ea480b475f2b6ab6f666584e" +checksum = "c65ee1f9701bf938026630b455d5315f490640234259037edb259798b3bcf85e" dependencies = [ "num-traits", ] @@ -227,41 +184,20 @@ dependencies = [ [[package]] name = "rstar" -version = "0.11.0" +version = "0.12.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "73111312eb7a2287d229f06c00ff35b51ddee180f017ab6dec1f69d62ac098d6" +checksum = "421400d13ccfd26dfa5858199c30a5d76f9c54e0dba7575273025b43c5175dbb" dependencies = [ "heapless", "num-traits", "smallvec", ] -[[package]] -name = "rustc_version" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bfa0f585226d2e68097d4f95d113b15b83a82e819ab25717ec0590d9584ef366" -dependencies = [ - "semver", -] - [[package]] name = "rustversion" -version = "1.0.14" +version = "1.0.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7ffc183a10b4478d04cbbbfc96d0873219d962dd5accaff2ffbd4ceb7df837f4" - -[[package]] -name = "scopeguard" -version = "1.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" - -[[package]] -name = "semver" -version = "1.0.20" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "836fa6a3e1e547f9a2c4040802ec865b5d85f4014efe00555d7090a3dcaa1090" +checksum = "0e819f2bc632f285be6d7cd36e25940d45b2391dd6d9b939e79de557f7014248" [[package]] name = "smallvec" @@ -269,15 +205,6 @@ version = "1.11.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4dccd0940a2dcdf68d092b8cbab7dc0ad8fa938bf95787e1b916b0e3d0e8e970" -[[package]] -name = "spin" -version = "0.9.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6980e8d7511241f8acf4aebddbb1ff938df5eebe98691418c4468d0b72a96a67" -dependencies = [ - "lock_api", -] - [[package]] name = "stable_deref_trait" version = "1.2.0" @@ -286,15 +213,18 @@ checksum = "a8f112729512f8e442d81f95a8a7ddf2b7c6b8a1a6f509a95864142b30cab2d3" [[package]] name = "strum" -version = "0.25.0" +version = "0.26.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "290d54ea6f91c969195bdbcd7442c8c2a2ba87da8bf60a7ee86a235d4bc1e125" +checksum = "8fec0f0aef304996cf250b31b5a10dee7980c85da9d759361292b8bca5a18f06" +dependencies = [ + "strum_macros", +] [[package]] name = "strum_macros" -version = "0.25.3" +version = "0.26.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "23dc1fa9ac9c169a78ba62f0b841814b7abae11bdd047b9c58f893439e309ea0" +checksum = "4c6bee85a5a24955dc440386795aa378cd9cf82acd5f764469152d2270e581be" dependencies = [ "heck", "proc-macro2", @@ -316,9 +246,9 @@ dependencies = [ [[package]] name = "tinyvec" -version = "1.6.0" +version = "1.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "87cc5ceb3875bb20c2890005a4e226a4651264a5c75edb2421b52861a0a0cb50" +checksum = "445e881f4f6d382d5f27c034e25eb92edd7c784ceab92a0937db7f2e9471b938" dependencies = [ "tinyvec_macros", ] diff --git a/Cargo.toml b/Cargo.toml index 25bbf8f..4b20752 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,13 +15,11 @@ test_lib = [] [dependencies] pico-args = "0.5.0" -lazy_static = "1.4.0" nom = "7.1.3" -itertools = "0.12.0" -strum = "0.25.0" -strum_macros = "0.25.0" -tinyvec = { version = "1.6.0", features = ["alloc"]} -hashlink = "0.8.4" -rstar = "0.11.0" -num-traits = "0.2.15" -ordered-float = "4.2.0" \ No newline at end of file +itertools = "0.13.0" +tinyvec = { version = "1.8.0", features = ["alloc"]} +hashlink = "0.9.1" +rstar = "0.12.2" +num-traits = "0.2.19" +ordered-float = "4.5.0" +strum = { version = "0.26.3", features = ["derive"] } diff --git a/README.md b/README.md index 180fce3..614de95 100644 --- a/README.md +++ b/README.md @@ -14,11 +14,11 @@ Solutions for [Advent of Code](https://adventofcode.com/) in [Rust](https://www. ## Benchmarks -| Day | Part 1 | Part 2 | -| :---: | :----: | :----: | - +| Day | Part 1 | Part 2 | +| :---: | :---: | :---: | +| [Day 1](./src/bin/01.rs) | `36.5µs` | `48.1µs` | -**Total: 0ms** +**Total: 0.08ms** --- diff --git a/data/examples/01.txt b/data/examples/01.txt new file mode 100644 index 0000000..dfca0b1 --- /dev/null +++ b/data/examples/01.txt @@ -0,0 +1,6 @@ +3 4 +4 3 +2 5 +1 3 +3 9 +3 3 \ No newline at end of file diff --git a/src/bin/01.rs b/src/bin/01.rs new file mode 100644 index 0000000..9d0cbae --- /dev/null +++ b/src/bin/01.rs @@ -0,0 +1,67 @@ +use std::collections::HashMap; + +use advent_of_code::ws; +use nom::{character::complete::u32, sequence::pair, IResult}; + +advent_of_code::solution!(1); + +fn parse_line(line: &str) -> (u32, u32) { + let result: IResult<_, _> = pair(ws(u32), ws(u32))(line); + result.expect("😎").1 +} + +pub fn part_one(input: &str) -> Option { + let (mut left, mut right): (Vec<_>, Vec<_>) = + input.lines().map(parse_line).unzip(); + + left.sort(); + right.sort(); + + Some( + left.into_iter() + .zip(right) + .map(|(l, r)| l.abs_diff(r)) + .sum(), + ) +} + +pub fn part_two(input: &str) -> Option { + let (left, right): (Vec<_>, Vec<_>) = input.lines().map(parse_line).unzip(); + + let mut right_counts = HashMap::::new(); + + for v in right.into_iter() { + right_counts + .entry(v) + .and_modify(|existing| *existing += 1) + .or_insert(1); + } + + let result = left + .into_iter() + .map(|left| { + (right_counts.get(&left).copied().unwrap_or_default(), left) + }) + .map(|(left, right)| left * right) + .sum(); + Some(result) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_part_one() { + let result = + part_one(&advent_of_code::template::read_file("examples", DAY)); + assert_eq!(result, Some(11)); + } + + #[test] + fn test_part_two() { + let result = + part_two(&advent_of_code::template::read_file("examples", DAY)); + assert_eq!(result, Some(31)); + } +} diff --git a/src/lib.rs b/src/lib.rs index 53b2400..e4ac4be 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -13,8 +13,7 @@ use nom::{ sequence::{delimited, preceded}, Parser, }; -use strum::IntoEnumIterator; -use strum_macros::EnumIter; +use strum::{EnumIter, IntoEnumIterator}; use tinyvec::TinyVec; pub type TV4 = TinyVec<[K; 4]>; @@ -185,9 +184,8 @@ impl Grid { } pub fn escaping(&self, i: usize) -> impl Iterator + '_ { - Compass::iter().filter_map(move |dir| { - self.step_from_index(i, dir).is_none().then_some(dir) - }) + Compass::iter() + .filter(move |dir| self.step_from_index(i, *dir).is_some()) } pub fn min_dist(&self, from: usize, to: usize) -> usize {