Day 15 works.

This commit is contained in:
Drew Galbraith 2023-12-15 14:23:15 -08:00
parent 95885fe72d
commit 9df32a9067
5 changed files with 114 additions and 0 deletions

18
Cargo.lock generated
View File

@ -5,3 +5,21 @@ version = 3
[[package]]
name = "aoc23"
version = "0.1.0"
dependencies = [
"itertools",
]
[[package]]
name = "either"
version = "1.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a26ae43d7bcc3b814de94796a5e736d4029efb0ee900c12e2d54c993ad1a1e07"
[[package]]
name = "itertools"
version = "0.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "25db6b064527c5d482d0423354fcd07a89a2dfe07b67892e62411946db7f07b0"
dependencies = [
"either",
]

View File

@ -6,3 +6,4 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
itertools = "0.12.0"

1
input/day15.txt Normal file

File diff suppressed because one or more lines are too long

91
src/day15.rs Normal file
View File

@ -0,0 +1,91 @@
use itertools::Itertools;
use std::io;
fn hash(s: &str) -> u64 {
let mut acc = 0;
for c in s.chars() {
acc += (c as u8) as u64;
acc *= 17;
acc %= 256;
}
acc as u64
}
pub fn hash_sequence() -> u64 {
let mut buffer = String::new();
io::stdin().read_line(&mut buffer).unwrap();
buffer.trim().split(',').map(hash).sum()
}
#[derive(Eq, PartialEq, Copy, Clone, Hash)]
enum Op<'a> {
Add(&'a str, u64),
Rem(&'a str),
}
fn same_operand(a: &Op, b: &Op) -> bool {
match a {
Op::Add(s, _) | Op::Rem(s) => match b {
Op::Add(t, _) | Op::Rem(t) => s == t,
},
}
}
fn parseop<'a>(op: &'a str) -> Op<'a> {
if op.ends_with('-') {
Op::Rem(&op[0..op.len() - 1])
} else {
let ind = op.find('=').unwrap();
Op::Add(&op[0..ind], op[ind + 1..].parse().expect("Could not parse"))
}
}
fn parse<'a>(ops: &Vec<&'a str>) -> Vec<Op<'a>> {
ops.iter().map(|s| parseop(*s)).collect()
}
fn condense<'a>(ops: &Vec<Op<'a>>) -> Vec<Op<'a>> {
let remove_deletes = ops.iter().enumerate().filter(|(i, op)| match op {
Op::Rem(_) => false,
Op::Add(_, _) => !ops
.iter()
.rposition(|lop| same_operand(op, lop) && matches!(*lop, Op::Rem(_)))
.is_some_and(|j| j > *i),
});
let remove_deletes_clone = remove_deletes.clone();
let map_to_last = remove_deletes.map(|(_, op)| {
*remove_deletes_clone
.clone()
.rfind(|(_, rop)| same_operand(op, rop))
.unwrap()
.1
});
let map_unique = map_to_last.into_iter().unique();
map_unique.collect()
}
fn perform_ops(ops: &Vec<&str>) -> u64 {
let reduced = condense(&parse(ops));
let mut sum = 0;
let mut boxes = vec![1; 256];
for r in reduced {
if let Op::Add(op, val) = r {
let h = hash(op);
let slot = boxes[h as usize];
boxes[h as usize] += 1;
sum += (h + 1) * slot * val;
}
}
sum
}
pub fn hash_map() -> u64 {
let mut buffer = String::new();
io::stdin().read_line(&mut buffer).unwrap();
perform_ops(&buffer.trim().split(',').collect())
}

View File

@ -6,6 +6,7 @@ mod day11;
mod day12;
mod day13;
mod day14;
mod day15;
mod day2;
mod day3;
mod day4;
@ -50,6 +51,8 @@ fn main() {
"day13b" => println!("Summary: {}", day13::summarize(true)),
"day14a" => println!("Load: {}", day14::total_load()),
"day14b" => println!("Load: {}", day14::total_load_many_cycles()),
"day15a" => println!("Sum: {}", day15::hash_sequence()),
"day15b" => println!("Sum: {}", day15::hash_map()),
_ => println!("Unrecognized day: {}", args[1]),
}
}