summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorReiner Herrmann <reiner@reiner-h.de>2023-12-15 12:14:05 +0100
committerReiner Herrmann <reiner@reiner-h.de>2023-12-15 12:14:05 +0100
commit60f3cb8d528318a1f4411c77738cf56e5dc1f693 (patch)
tree55d91841278a4d5b9e32caffb948a79ef10f2312
parent61c1184449250dc52abd17c07b984ea161fc273b (diff)
day15 solution 1
-rw-r--r--src/bin/day15.rs38
1 files changed, 38 insertions, 0 deletions
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);
+ }
+}