summaryrefslogtreecommitdiff
path: root/src/bin/day15.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/bin/day15.rs')
-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);
+ }
+}