summaryrefslogtreecommitdiff
path: root/src/bin/day4.rs
diff options
context:
space:
mode:
authorReiner Herrmann <reiner@reiner-h.de>2022-08-27 22:51:20 +0200
committerReiner Herrmann <reiner@reiner-h.de>2022-08-27 22:51:20 +0200
commitc2e25913a56bac4b0760febe0a2943e20c195d27 (patch)
tree25e68be737594677da13f27d96fb57fc98ff11f1 /src/bin/day4.rs
parentfeea729aecf0de8d1f3a86ca56ff1914b655a4dd (diff)
day4
Diffstat (limited to 'src/bin/day4.rs')
-rw-r--r--src/bin/day4.rs31
1 files changed, 31 insertions, 0 deletions
diff --git a/src/bin/day4.rs b/src/bin/day4.rs
new file mode 100644
index 0000000..c6963bb
--- /dev/null
+++ b/src/bin/day4.rs
@@ -0,0 +1,31 @@
+fn main() {
+ let input = "ckczppom";
+ println!("4a: {}", find_hash_count(input, "00000"));
+ println!("4b: {}", find_hash_count(input, "000000"));
+}
+
+fn find_hash_count(input: &str, start: &str) -> u32 {
+ let mut i = 0;
+
+ loop {
+ let text = input.to_string() + &i.to_string();
+ let digest = format!("{:?}", md5::compute(&text));
+
+ if digest.starts_with(start) {
+ return i;
+ }
+
+ i += 1;
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn test() {
+ assert_eq!(find_hash_count("abcdef", "00000"), 609043);
+ assert_eq!(find_hash_count("pqrstuv", "00000"), 1048970);
+ }
+}