From 60f3cb8d528318a1f4411c77738cf56e5dc1f693 Mon Sep 17 00:00:00 2001 From: Reiner Herrmann Date: Fri, 15 Dec 2023 12:14:05 +0100 Subject: day15 solution 1 --- src/bin/day15.rs | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 src/bin/day15.rs (limited to 'src') diff --git a/src/bin/day15.rs b/src/bin/day15.rs new file mode 100644 index 0000000..648534a --- /dev/null +++ b/src/bin/day15.rs @@ -0,0 +1,38 @@ +static DAY: u8 = 15; + +fn main() { + let input = advent::read_file(DAY); + println!("{DAY}a: {}", hash_sum(&input)); + println!("{DAY}b: {}", 0); +} + +fn hash(input: &str) -> u32 { + let mut value = 0; + + for c in input.chars() { + value += c as u32; + value *= 17; + value %= 256; + } + + value +} + +fn hash_sum(input: &str) -> u32 { + let input = input.trim_end(); + input.split(',') + .map(|x| hash(x)) + .sum() +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test() { + let input = "rn=1,cm-,qp=3,cm=2,qp-,pc=4,ot=9,ab=5,pc-,pc=6,ot=7"; + assert_eq!(hash("HASH"), 52); + assert_eq!(hash_sum(input), 1320); + } +} -- cgit v1.2.3