summaryrefslogtreecommitdiff
path: root/2023/src/bin/day_15.rs
diff options
context:
space:
mode:
authorJustin Wernick <justin@worthe-it.co.za>2023-12-15 09:01:19 +0200
committerJustin Wernick <justin@worthe-it.co.za>2023-12-15 09:01:19 +0200
commitdc97d6fe01ca7afd2618540f9a4470b6a3849674 (patch)
tree9ee4c6ee515b63f0925c9bc9c5a1385300ec4496 /2023/src/bin/day_15.rs
parent148112498bd4c89e132c1014c19d19631b6fa537 (diff)
Day 15 part 1
Diffstat (limited to '2023/src/bin/day_15.rs')
-rw-r--r--2023/src/bin/day_15.rs44
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);
}