#[path = "../lib.rs"] mod lib; fn hash(s: &str) -> usize { let mut value = 0; for b in s.bytes() { value += b as usize; value *= 17; value %= 256; } return value; } fn part2(boxes: &mut [Vec<(String, usize)>], s: &str) { if let Some((label, fl)) = s.split_once('=') { let focal = fl.parse().unwrap(); let i = hash(label); for j in 0..boxes[i].len() { if boxes[i][j].0 == label { boxes[i][j].1 = focal; return; } } boxes[i].push((label.to_string(), focal)); } else { let label = s.strip_suffix('-').unwrap(); let i = hash(label); for j in 0..boxes[i].len() { if boxes[i][j].0 == label { boxes[i].remove(j); return; } } } } fn main() { let mut sum1 = 0; let mut sum2 = 0; let mut boxes: [Vec<(String, usize)>; 256] = std::array::from_fn(|_| vec![]); for line in lib::iter_input() { for s in line.split(',') { sum1 += hash(s); part2(&mut boxes, s); } } for i in 0..boxes.len() { for (j, lense) in boxes[i].iter().enumerate() { sum2 += (i + 1) * (j + 1) * lense.1; } } println!("part1: {}", sum1); println!("part2: {}", sum2); }