From b522357495aef44d039ea2cf2dbc28f29248da66 Mon Sep 17 00:00:00 2001 From: Jan-Bulthuis Date: Sun, 8 Dec 2024 14:54:14 +0100 Subject: [PATCH] Added hashbrown --- aoc_2024/Cargo.lock | 30 ++++++++++++++++++++++++++++++ aoc_2024/Cargo.toml | 1 + aoc_2024/src/day8.rs | 21 +++++++-------------- 3 files changed, 38 insertions(+), 14 deletions(-) diff --git a/aoc_2024/Cargo.lock b/aoc_2024/Cargo.lock index 54509ae..1eb54fe 100644 --- a/aoc_2024/Cargo.lock +++ b/aoc_2024/Cargo.lock @@ -11,6 +11,12 @@ dependencies = [ "memchr", ] +[[package]] +name = "allocator-api2" +version = "0.2.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "683d7910e743518b0e34f1186f92494becacb047c7b6bf616c96772180fef923" + [[package]] name = "aoc-runner" version = "0.3.0" @@ -46,6 +52,7 @@ version = "0.1.0" dependencies = [ "aoc-runner", "aoc-runner-derive", + "hashbrown", "nom", "num", "regex", @@ -58,6 +65,29 @@ version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26" +[[package]] +name = "equivalent" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" + +[[package]] +name = "foldhash" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f81ec6369c545a7d40e4589b5597581fa1c441fe1cce96dd1de43159910a36a2" + +[[package]] +name = "hashbrown" +version = "0.15.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bf151400ff0baff5465007dd2f3e717f3fe502074ca563069ce3a6629d07b289" +dependencies = [ + "allocator-api2", + "equivalent", + "foldhash", +] + [[package]] name = "itoa" version = "1.0.14" diff --git a/aoc_2024/Cargo.toml b/aoc_2024/Cargo.toml index ab25eaf..72c8b23 100644 --- a/aoc_2024/Cargo.toml +++ b/aoc_2024/Cargo.toml @@ -6,6 +6,7 @@ edition = "2021" [dependencies] aoc-runner = "0.3.0" aoc-runner-derive = "0.3.0" +hashbrown = "0.15.2" nom = "7.1.3" num = "0.4.3" regex = "1.11.1" diff --git a/aoc_2024/src/day8.rs b/aoc_2024/src/day8.rs index 533a776..94096ab 100644 --- a/aoc_2024/src/day8.rs +++ b/aoc_2024/src/day8.rs @@ -1,4 +1,4 @@ -use std::collections::{HashMap, HashSet}; +use hashbrown::{HashMap, HashSet}; use aoc_runner_derive::{aoc, aoc_generator}; use num::integer::gcd; @@ -15,11 +15,9 @@ fn parse(input: &str) -> Input { .enumerate() .filter(|(_, c)| c != &'.') .for_each(|(x, c)| { - if let std::collections::hash_map::Entry::Vacant(e) = map.entry(c) { - e.insert(vec![(x as isize, y as isize)]); - } else { - map.get_mut(&c).unwrap().push((x as isize, y as isize)); - } + map.entry(c) + .or_insert(Vec::new()) + .push((x as isize, y as isize)); }); }); let vec = map.into_values().collect(); @@ -28,7 +26,7 @@ fn parse(input: &str) -> Input { #[aoc(day8, part1)] fn part1(input: &Input) -> usize { - let mut set = HashSet::with_capacity(input.1.len() * input.1.len()); + let mut set = HashSet::new(); input.1.iter().for_each(|vec| { for l in 0..vec.len() { for r in l + 1..vec.len() { @@ -58,7 +56,6 @@ fn part2(input: &Input) -> usize { let a2 = vec[r]; let s = a1; let v = (a2.0 - a1.0, a2.1 - a1.1); - let v = (v.0 / gcd(v.0, v.1), v.1 / gcd(v.0, v.1)); let mut d = 0; loop { let c = (s.0 + d * v.0, s.1 + d * v.1); @@ -82,12 +79,6 @@ fn part2(input: &Input) -> usize { } } }); - // for y in 0..input.0 .1 { - // println!(); - // for x in 0..input.0 .0 { - // print!("{}", if set.contains(&(x, y)) { '#' } else { '.' }) - // } - // } set.len() } @@ -113,5 +104,7 @@ mod tests { )), 34 ); + + assert_eq!(part2(&parse(include_str!("../input/2024/day8.txt"))), 1017); } }