diff options
Diffstat (limited to '2023/src/bin')
-rw-r--r-- | 2023/src/bin/day_15.rs | 44 |
1 files changed, 36 insertions, 8 deletions
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<dyn std::error::Error>> { - 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<String>); -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::<u32>::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); } |