diff --git a/cli/src/main.rs b/cli/src/main.rs index 55d9ed4..c8df5e9 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -152,7 +152,11 @@ async fn main() { } timings.drain(..=(warmup as usize)); let avg = (timings.iter().sum::() / n).as_nanos(); - let deviation = (((timings.iter().max().unwrap().as_nanos() - timings.iter().min().unwrap().as_nanos()).pow(2) / 16) as f64).sqrt() as u128; + let deviation = (((timings.iter().max().unwrap().as_nanos() + - timings.iter().min().unwrap().as_nanos()) + .pow(2) + / 16) as f64) + .sqrt() as u128; let (avg, avg_units) = if avg >= 1000000000 { (avg as f64 / 1000000000.0, "s") diff --git a/lib/src/day15/mod.rs b/lib/src/day15/mod.rs index 9c50551..6da0bf0 100644 --- a/lib/src/day15/mod.rs +++ b/lib/src/day15/mod.rs @@ -1,5 +1,3 @@ -use std::collections::HashMap; - use crate::utils::Day; #[derive(Debug, Default)] @@ -9,65 +7,61 @@ pub struct Day15 { impl Day for Day15 { fn part1(&mut self) -> String { - let mut current = 0usize; - self.input .split(',') .map(|text| { - current = 0; - - for c in text.chars() { - current += c as usize; - current *= 17; - current %= 256; - } - - current + text.chars() + .fold(0usize, |acc, c| ((acc + c as usize) * 17) % 256) }) .sum::() .to_string() } fn part2(&mut self) -> String { - let mut map = - HashMap::>::from_iter((0..=255usize).map(|i| (i, vec![]))); + const ARRAY_REPEAT_VALUE: Vec<(&str, u8)> = Vec::new(); + let mut boxes: [Vec<(&str, u8)>; 256] = [ARRAY_REPEAT_VALUE; 256]; for operation in self.input.split(',') { - let (label, focal_length) = operation.split_once(['-', '=']).unwrap(); - let box_id = { - let mut current = 0; + let ((box_id, label), focal_length) = operation + .split_once(['-', '=']) + .map(|(label, focal_length)| { + ( + ( + label + .chars() + .fold(0usize, |acc, c| ((acc + c as usize) * 17) % 256), + label, + ), + focal_length.parse::().ok(), + ) + }) + .unwrap(); - for c in label.chars() { - current += c as usize; - current *= 17; - current %= 256; + match focal_length { + Some(focal_length) => { + let lenses = boxes.get_mut(box_id).unwrap(); + match lenses.iter().position(|(l, _)| l == &label) { + Some(position) => lenses[position].1 = focal_length, + None => lenses.push((label, focal_length)), + } } - - current - }; - if focal_length == "" { - let lenses = map.get_mut(&box_id).unwrap(); - if let Some(position) = lenses.iter().position(|(l, _)| l == &label) { - lenses.remove(position); - } - } else { - let focal_length = focal_length.parse::().unwrap(); - - let lenses = map.get_mut(&box_id).unwrap(); - match lenses.iter().position(|(l, _)| l == &label) { - Some(position) => lenses[position].1 = focal_length, - None => lenses.push((label, focal_length)), + None => { + let lenses = boxes.get_mut(box_id).unwrap(); + if let Some(position) = lenses.iter().position(|(l, _)| l == &label) { + lenses.remove(position); + } } } } (0..=255usize) .map(|i| { - map.remove(&i) + boxes + .get(i) .unwrap() .into_iter() .enumerate() - .map(|(j, (_, focal_length))| focal_length as usize * (j + 1) * (i + 1)) + .map(|(j, (_, focal_length))| *focal_length as usize * (j + 1) * (i + 1)) .sum::() }) .sum::() diff --git a/rust-toolchain.toml b/rust-toolchain.toml new file mode 100644 index 0000000..5d56faf --- /dev/null +++ b/rust-toolchain.toml @@ -0,0 +1,2 @@ +[toolchain] +channel = "nightly"