use std::fs; fn main() -> Result<(), Box> { let input = fs::read_to_string("inputs/day_15.txt")?; let parsed = InitializationInstructions::parse(&input); dbg!(&parsed.hash_sum()); Ok(()) } #[derive(Debug)] struct InitializationInstructions(Vec); impl InitializationInstructions { fn parse(input: &str) -> Self { Self(input.trim().split(",").map(|s| s.to_owned()).collect()) } fn hash_sum(&self) -> u32 { self.0.iter().map(|s| hash(&s)).sum() } } fn hash(input: &str) -> u32 { let mut result: u32 = 0; for c in input.bytes() { result += Into::::into(c); result *= 17; result %= 256; } result } #[test] fn examples() { assert_eq!(hash("rn=1"), 30); assert_eq!(hash("cm-"), 253); assert_eq!(hash("qp=3"), 97); assert_eq!(hash("cm=2"), 47); assert_eq!(hash("qp-"), 14); assert_eq!(hash("pc=4"), 180); assert_eq!(hash("ot=9"), 9); assert_eq!(hash("ab=5"), 197); assert_eq!(hash("pc-"), 48); assert_eq!(hash("pc=6"), 214); assert_eq!(hash("ot=7"), 231); }