From e45aaad1c470bc03f27377d2887d0c529383b8d2 Mon Sep 17 00:00:00 2001 From: Bert Peters Date: Fri, 9 Dec 2022 11:43:33 +0100 Subject: [PATCH] Faster hash set --- 2022/Cargo.toml | 1 + 2022/benches/days.rs | 33 +++++++++++++++------------------ 2022/src/day09.rs | 9 +++------ 3 files changed, 19 insertions(+), 24 deletions(-) diff --git a/2022/Cargo.toml b/2022/Cargo.toml index 9c1cf0d..581f37a 100644 --- a/2022/Cargo.toml +++ b/2022/Cargo.toml @@ -6,6 +6,7 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +ahash = "0.8.2" anyhow = "1.0.66" clap = { version = "4.0.19", features = ["derive"] } itertools = "0.10.5" diff --git a/2022/benches/days.rs b/2022/benches/days.rs index 500f70b..68dd88d 100644 --- a/2022/benches/days.rs +++ b/2022/benches/days.rs @@ -10,34 +10,31 @@ use criterion::Criterion; /// Number of days we have an implementation to benchmark const DAYS_IMPLEMENTED: u8 = 25; -fn read_input(day: u8) -> Vec { +fn read_input(day: u8) -> std::io::Result> { let input_path = format!("inputs/{:02}.txt", day); let mut buffer = Vec::new(); - File::open(input_path) - .expect("Failed to open input file") - .read_to_end(&mut buffer) - .expect("Failed to read input file"); + File::open(input_path)?.read_to_end(&mut buffer)?; - buffer + Ok(buffer) } pub fn benchmark_days(c: &mut Criterion) { for day in 1..=DAYS_IMPLEMENTED { - let input = read_input(day); + if let Ok(input) = read_input(day) { + let part1 = get_implementation(day, false).unwrap(); - let part1 = get_implementation(day, false).unwrap(); - - c.bench_with_input(BenchmarkId::new("part1", day), &input, |b, i| { - b.iter(|| part1(i)); - }); - - if day < 25 { - let part2 = get_implementation(day, true).unwrap(); - - c.bench_with_input(BenchmarkId::new("part2", day), &input, |b, i| { - b.iter(|| part2(i)); + c.bench_with_input(BenchmarkId::new("part1", day), &input, |b, i| { + b.iter(|| part1(i)); }); + + if day < 25 { + let part2 = get_implementation(day, true).unwrap(); + + c.bench_with_input(BenchmarkId::new("part2", day), &input, |b, i| { + b.iter(|| part2(i)); + }); + } } } } diff --git a/2022/src/day09.rs b/2022/src/day09.rs index 48817a1..ed17fac 100644 --- a/2022/src/day09.rs +++ b/2022/src/day09.rs @@ -1,9 +1,9 @@ -use std::collections::HashSet; use std::ops::Add; use std::ops::Index; use std::ops::IndexMut; use std::ops::Sub; +use ahash::AHashSet; use anyhow::Result; use nom::bytes::complete::tag; use nom::bytes::complete::take; @@ -101,7 +101,7 @@ fn part_generic(input: &[u8]) -> Result { let mut head_pos = Vec2([0, 0]); let mut tails = [head_pos; N]; - let mut visited = HashSet::new(); + let mut visited = AHashSet::new(); visited.insert(head_pos); for (direction, steps) in moves { @@ -112,7 +112,7 @@ fn part_generic(input: &[u8]) -> Result { let mut ref_pos = head_pos; - for (i, tail_pos) in tails.iter_mut().enumerate() { + for tail_pos in &mut tails { let delta = ref_pos - *tail_pos; if delta[0].abs() <= 1 && delta[1].abs() <= 1 { @@ -123,9 +123,6 @@ fn part_generic(input: &[u8]) -> Result { *tail_pos = *tail_pos + step; - if i == N - 1 { - visited.insert(*tail_pos); - } ref_pos = *tail_pos; }