mirror of
https://github.com/Noratrieb/advent-of-code.git
synced 2026-01-14 17:45:02 +01:00
day03, faster hash
This commit is contained in:
parent
3e0ef21379
commit
e80cf475a1
5 changed files with 81 additions and 4 deletions
|
|
@ -37,12 +37,12 @@ fn part2(_input: &str) -> u64 {
|
|||
helper::tests! {
|
||||
day00 Day00;
|
||||
part1 {
|
||||
small => 8;
|
||||
default => 1931;
|
||||
small => 0;
|
||||
default => 0;
|
||||
}
|
||||
part2 {
|
||||
small => 2286;
|
||||
default => 83105;
|
||||
small => 0;
|
||||
default => 0;
|
||||
}
|
||||
}
|
||||
helper::benchmarks! {}
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@ edition = "2021"
|
|||
nom.workspace = true
|
||||
helper.workspace = true
|
||||
divan.workspace = true
|
||||
rustc-hash = "1.1.0"
|
||||
|
||||
[[bench]]
|
||||
name = "benches"
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
mod p2faster_hash;
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
use helper::Day;
|
||||
|
|
@ -15,6 +17,7 @@ helper::define_variants! {
|
|||
}
|
||||
part2 {
|
||||
basic => crate::part2;
|
||||
fasher_hash => crate::p2faster_hash::part2;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
66
2023/day03/src/p2faster_hash.rs
Normal file
66
2023/day03/src/p2faster_hash.rs
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
use rustc_hash::FxHashMap;
|
||||
|
||||
pub fn part2(input: &str) -> u64 {
|
||||
fn contains_gear(s: &str) -> Option<usize> {
|
||||
s.chars().position(|c| !c.is_ascii_digit() && c != '.')
|
||||
}
|
||||
|
||||
let len = input.lines().next().unwrap().len();
|
||||
let empty_border = std::iter::repeat('.').take(len).collect::<String>();
|
||||
|
||||
let mut prev2 = empty_border.as_str();
|
||||
let mut prev1 = input.lines().next().unwrap();
|
||||
|
||||
let mut gears: FxHashMap<u32, Vec<u64>> = FxHashMap::default();
|
||||
|
||||
for (line_number, next) in input
|
||||
.lines()
|
||||
.skip(1)
|
||||
.chain(Some(empty_border.as_str()))
|
||||
.enumerate()
|
||||
{
|
||||
let mut numbers = prev1.char_indices().peekable();
|
||||
while let Some((start, c)) = numbers.next() {
|
||||
if c.is_ascii_digit() {
|
||||
let mut end = start;
|
||||
while let Some((idx, '0'..='9')) = numbers.next() {
|
||||
end = idx;
|
||||
}
|
||||
|
||||
let box_bounds = (start.saturating_sub(1))..std::cmp::min(end + 2, len);
|
||||
let number = prev1[start..=end].parse::<u64>().unwrap();
|
||||
|
||||
let mut push = |key: (usize, usize)| {
|
||||
gears
|
||||
.entry(((key.1 as u32) << 16) | (key.0 as u32))
|
||||
.or_default()
|
||||
.push(number)
|
||||
};
|
||||
|
||||
if let Some(position) = contains_gear(&prev2[box_bounds.clone()]) {
|
||||
let key = (line_number - 1, box_bounds.start + position);
|
||||
push(key);
|
||||
}
|
||||
|
||||
if let Some(position) = contains_gear(&prev1[box_bounds.clone()]) {
|
||||
let key = (line_number, box_bounds.start + position);
|
||||
push(key);
|
||||
}
|
||||
|
||||
if let Some(position) = contains_gear(&next[box_bounds.clone()]) {
|
||||
let key = (line_number + 1, box_bounds.start + position);
|
||||
push(key);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
prev2 = prev1;
|
||||
prev1 = next;
|
||||
}
|
||||
|
||||
gears
|
||||
.values()
|
||||
.filter(|v| v.len() == 2)
|
||||
.map(|v| v[0] * v[1])
|
||||
.sum()
|
||||
}
|
||||
7
Cargo.lock
generated
7
Cargo.lock
generated
|
|
@ -129,6 +129,7 @@ dependencies = [
|
|||
"divan",
|
||||
"helper",
|
||||
"nom",
|
||||
"rustc-hash",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
|
@ -231,6 +232,12 @@ version = "0.1.5"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "30b661b2f27137bdbc16f00eda72866a92bb28af1753ffbd56744fb6e2e9cd8e"
|
||||
|
||||
[[package]]
|
||||
name = "rustc-hash"
|
||||
version = "1.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2"
|
||||
|
||||
[[package]]
|
||||
name = "rustix"
|
||||
version = "0.38.26"
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue