Compare commits
No commits in common. "3e9c1ee1c1900e3a67023bd1f977883810c01dda" and "6a286a751e19dfc09e83f5dff0c40e7909c4ebf6" have entirely different histories.
3e9c1ee1c1
...
6a286a751e
6 changed files with 0 additions and 76 deletions
|
@ -1,2 +0,0 @@
|
|||
515210
|
||||
246762
|
File diff suppressed because one or more lines are too long
|
@ -1,9 +0,0 @@
|
|||
[package]
|
||||
name = "rust_2023_15"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
aoc = { path = "../../../common/rust" }
|
|
@ -1,56 +0,0 @@
|
|||
#![warn(clippy::pedantic)]
|
||||
|
||||
use std::io::{stdin, Read};
|
||||
|
||||
fn hash(s: &str) -> u8 {
|
||||
s.as_bytes()
|
||||
.iter()
|
||||
.copied()
|
||||
.fold(0, |acc, b| acc.wrapping_add(b).wrapping_mul(17))
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let mut input = String::new();
|
||||
stdin().read_to_string(&mut input).unwrap();
|
||||
let input = input.trim();
|
||||
|
||||
let steps = input.split(',');
|
||||
|
||||
println!(
|
||||
"{}",
|
||||
steps.clone().map(|s| { u64::from(hash(s)) }).sum::<u64>()
|
||||
);
|
||||
|
||||
let mut boxes: [Vec<(&str, usize)>; 256] = std::array::from_fn(|_| Vec::new());
|
||||
|
||||
for step in steps {
|
||||
let (label, focal_length) = if let Some(label) = step.strip_suffix('-') {
|
||||
(label, None)
|
||||
} else {
|
||||
let (label, focal_length) = step.split_once('=').unwrap();
|
||||
(label, Some(focal_length.parse().unwrap()))
|
||||
};
|
||||
|
||||
let b = &mut boxes[usize::from(hash(label))];
|
||||
if let Some(existing_pos) = b.iter().position(|&(l, _fl)| l == label) {
|
||||
if let Some(fl) = focal_length {
|
||||
b[existing_pos].1 = fl;
|
||||
} else {
|
||||
b.remove(existing_pos);
|
||||
}
|
||||
} else if let Some(fl) = focal_length {
|
||||
b.push((label, fl));
|
||||
}
|
||||
}
|
||||
|
||||
let focal_powers = boxes.into_iter().enumerate().flat_map(|(box_num, lenses)| {
|
||||
lenses
|
||||
.into_iter()
|
||||
.enumerate()
|
||||
.map(move |(lens_num, (_label, focal_length))| {
|
||||
(box_num + 1) * (lens_num + 1) * focal_length
|
||||
})
|
||||
});
|
||||
|
||||
println!("{}", focal_powers.sum::<usize>());
|
||||
}
|
7
Cargo.lock
generated
7
Cargo.lock
generated
|
@ -695,13 +695,6 @@ dependencies = [
|
|||
"itertools 0.12.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rust_2023_15"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"aoc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rustversion"
|
||||
version = "1.0.14"
|
||||
|
|
|
@ -36,5 +36,4 @@ members = [
|
|||
"2023/day8/rust",
|
||||
"2023/day9/rust",
|
||||
"2023/day11/rust",
|
||||
"2023/day15/rust",
|
||||
]
|
||||
|
|
Loading…
Reference in a new issue