hi clubby

This commit is contained in:
nora 2024-12-01 13:34:16 +01:00
parent 3490086a7f
commit 1b194e4f1c
5 changed files with 187 additions and 1 deletions

View file

@ -1,4 +1,7 @@
use helper::{Day, IteratorExt, Variants};
use std::{collections::HashMap, hash::BuildHasherDefault};
use helper::{Day, IteratorExt, NoHasher, Variants};
use rustc_hash::FxHashMap;
pub fn main() {
helper::main::<Day01>(include_str!("../input.txt"));
@ -13,6 +16,11 @@ helper::define_variants! {
}
part2 {
basic => crate::part2;
hash => crate::part2_hash;
hash_no_hash => crate::part2_hash_nohash;
faster_parsing => crate::part2_parsing;
array => crate::part2_array;
μopt_parsing => crate::part2_μopt_parsing;
}
}
@ -61,6 +69,150 @@ fn part2(input: &str) -> u64 {
score
}
fn part2_hash(input: &str) -> u64 {
let (left, right) = parse(input);
let mut right_map = FxHashMap::<u64, u32>::default();
for number in right {
*right_map.entry(number).or_default() += 1;
}
let mut score = 0;
for number in left {
let occurs = right_map.get(&number).copied().unwrap_or_default();
score += number * (occurs as u64);
}
score
}
fn part2_hash_nohash(input: &str) -> u64 {
let (left, right) = parse(input);
let mut right_map = HashMap::<u64, u32, BuildHasherDefault<NoHasher>>::default();
for number in right {
*right_map.entry(number).or_default() += 1;
}
let mut score = 0;
for number in left {
let occurs = right_map.get(&number).copied().unwrap_or_default();
score += number * (occurs as u64);
}
score
}
fn part2_parsing(input: &str) -> u64 {
let mut right_map = FxHashMap::<u64, u32>::with_capacity_and_hasher(
input.len() / 8,
rustc_hash::FxBuildHasher::default(),
);
let mut left = Vec::with_capacity(input.len() / 8);
let mut input = input;
loop {
let Some(space) = input.as_bytes().iter().position(|b| *b == b' ') else {
break;
};
let number = input[..space].parse::<u64>().unwrap();
left.push(number);
input = &input[(space + 3)..];
let Some(newline) = input.as_bytes().iter().position(|b| *b == b'\n') else {
break;
};
let number = input[..space].parse::<u64>().unwrap();
*right_map.entry(number).or_default() += 1;
input = &input[newline..];
// handle lack of trailing newline
if !input.is_empty() {
input = &input[1..];
}
}
let mut score = 0;
for number in left {
let occurs = right_map.get(&number).copied().unwrap_or_default();
score += number * (occurs as u64);
}
score
}
fn part2_array(input: &str) -> u64 {
const BIGGEST_ELEMENT: usize = 100_000;
let mut right_map = vec![0_u16; BIGGEST_ELEMENT];
let mut left = Vec::with_capacity(input.len() / 8);
let mut input = input;
loop {
let Some(space) = input.as_bytes().iter().position(|b| *b == b' ') else {
break;
};
let number = input[..space].parse::<u64>().unwrap();
left.push(number);
input = &input[(space + 3)..];
let Some(newline) = input.as_bytes().iter().position(|b| *b == b'\n') else {
break;
};
let number = input[..space].parse::<u64>().unwrap();
right_map[number as usize] += 1;
input = &input[newline..];
// handle lack of trailing newline
if !input.is_empty() {
input = &input[1..];
}
}
let mut score = 0;
for number in left {
let occurs = right_map[number as usize];
score += number * (occurs as u64);
}
score
}
fn part2_μopt_parsing(input: &str) -> u64 {
const BIGGEST_ELEMENT: usize = 100_000;
let mut right_map = vec![0_u16; BIGGEST_ELEMENT];
let mut left = Vec::with_capacity(input.len() / 8);
let mut input = input;
loop {
let Some(space) = input.as_bytes().iter().position(|b| *b == b' ') else {
break;
};
let number = input[..space].parse::<u64>().unwrap();
left.push(number);
input = &input[(space + 3)..];
let Some(newline) = input.as_bytes().iter().position(|b| *b == b'\n') else {
break;
};
let number = input[..space].parse::<u64>().unwrap();
right_map[number as usize] += 1;
input = &input[newline..];
// handle lack of trailing newline
if !input.is_empty() {
input = &input[1..];
}
}
let mut score = 0;
for number in left {
let occurs = right_map[number as usize];
score += number * (occurs as u64);
}
score
}
helper::tests! {
day01 Day01;
part1 {