Day 15 improvements + nightly b/c yeah
This commit is contained in:
parent
a1a8cd3af2
commit
4e4cc58bec
3 changed files with 40 additions and 40 deletions
|
@ -152,7 +152,11 @@ async fn main() {
|
|||
}
|
||||
timings.drain(..=(warmup as usize));
|
||||
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 {
|
||||
(avg as f64 / 1000000000.0, "s")
|
||||
|
|
|
@ -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::<usize>()
|
||||
.to_string()
|
||||
}
|
||||
|
||||
fn part2(&mut self) -> String {
|
||||
let mut map =
|
||||
HashMap::<usize, Vec<(&str, u8)>>::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::<u8>().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::<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)),
|
||||
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::<usize>()
|
||||
})
|
||||
.sum::<usize>()
|
||||
|
|
2
rust-toolchain.toml
Normal file
2
rust-toolchain.toml
Normal file
|
@ -0,0 +1,2 @@
|
|||
[toolchain]
|
||||
channel = "nightly"
|
Loading…
Reference in a new issue