Day 15 improvements + nightly b/c yeah

This commit is contained in:
Tyler Beckman 2023-12-15 19:47:03 -07:00
parent a1a8cd3af2
commit 4e4cc58bec
Signed by: Ty
GPG key ID: 2813440C772555A4
3 changed files with 40 additions and 40 deletions

View file

@ -152,7 +152,11 @@ async fn main() {
} }
timings.drain(..=(warmup as usize)); timings.drain(..=(warmup as usize));
let avg = (timings.iter().sum::<Duration>() / n).as_nanos(); let avg = (timings.iter().sum::<Duration>() / 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 { let (avg, avg_units) = if avg >= 1000000000 {
(avg as f64 / 1000000000.0, "s") (avg as f64 / 1000000000.0, "s")

View file

@ -1,5 +1,3 @@
use std::collections::HashMap;
use crate::utils::Day; use crate::utils::Day;
#[derive(Debug, Default)] #[derive(Debug, Default)]
@ -9,65 +7,61 @@ pub struct Day15 {
impl Day for Day15 { impl Day for Day15 {
fn part1(&mut self) -> String { fn part1(&mut self) -> String {
let mut current = 0usize;
self.input self.input
.split(',') .split(',')
.map(|text| { .map(|text| {
current = 0; text.chars()
.fold(0usize, |acc, c| ((acc + c as usize) * 17) % 256)
for c in text.chars() {
current += c as usize;
current *= 17;
current %= 256;
}
current
}) })
.sum::<usize>() .sum::<usize>()
.to_string() .to_string()
} }
fn part2(&mut self) -> String { fn part2(&mut self) -> String {
let mut map = const ARRAY_REPEAT_VALUE: Vec<(&str, u8)> = Vec::new();
HashMap::<usize, Vec<(&str, u8)>>::from_iter((0..=255usize).map(|i| (i, vec![]))); let mut boxes: [Vec<(&str, u8)>; 256] = [ARRAY_REPEAT_VALUE; 256];
for operation in self.input.split(',') { for operation in self.input.split(',') {
let (label, focal_length) = operation.split_once(['-', '=']).unwrap(); let ((box_id, label), focal_length) = operation
let box_id = { .split_once(['-', '='])
let mut current = 0; .map(|(label, focal_length)| {
(
(
label
.chars()
.fold(0usize, |acc, c| ((acc + c as usize) * 17) % 256),
label,
),
focal_length.parse::<u8>().ok(),
)
})
.unwrap();
for c in label.chars() { match focal_length {
current += c as usize; Some(focal_length) => {
current *= 17; let lenses = boxes.get_mut(box_id).unwrap();
current %= 256; match lenses.iter().position(|(l, _)| l == &label) {
Some(position) => lenses[position].1 = focal_length,
None => lenses.push((label, focal_length)),
}
} }
None => {
current let lenses = boxes.get_mut(box_id).unwrap();
}; if let Some(position) = lenses.iter().position(|(l, _)| l == &label) {
if focal_length == "" { lenses.remove(position);
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::<u8>().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)),
} }
} }
} }
(0..=255usize) (0..=255usize)
.map(|i| { .map(|i| {
map.remove(&i) boxes
.get(i)
.unwrap() .unwrap()
.into_iter() .into_iter()
.enumerate() .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::<usize>() .sum::<usize>()
}) })
.sum::<usize>() .sum::<usize>()

2
rust-toolchain.toml Normal file
View file

@ -0,0 +1,2 @@
[toolchain]
channel = "nightly"