From dc97d6fe01ca7afd2618540f9a4470b6a3849674 Mon Sep 17 00:00:00 2001 From: Justin Wernick Date: Fri, 15 Dec 2023 09:01:19 +0200 Subject: Day 15 part 1 --- 2023/src/bin/day_15.rs | 44 ++++++++++++++++++++++++++++++++++++-------- 1 file changed, 36 insertions(+), 8 deletions(-) (limited to '2023/src') diff --git a/2023/src/bin/day_15.rs b/2023/src/bin/day_15.rs index b3a610b..badb34f 100644 --- a/2023/src/bin/day_15.rs +++ b/2023/src/bin/day_15.rs @@ -1,19 +1,47 @@ -use nom::IResult; use std::fs; fn main() -> Result<(), Box> { - let input = fs::read_to_string("inputs/day_2.txt")?; - let parsed = Example::parser(&input).unwrap().1; - dbg!(&parsed); + let input = fs::read_to_string("inputs/day_15.txt")?; + let parsed = InitializationInstructions::parse(&input); + dbg!(&parsed.hash_sum()); Ok(()) } #[derive(Debug)] -struct Example; +struct InitializationInstructions(Vec); -impl Example { - fn parser(_input: &str) -> IResult<&str, Self> { - todo!() +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); } -- cgit v1.2.3