summary refs log tree commit diff homepage
path: root/src/bin/day14.rs
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/bin/day14.rs47
1 files changed, 37 insertions, 10 deletions
diff --git a/src/bin/day14.rs b/src/bin/day14.rs
index b22e88d..352982d 100644
--- a/src/bin/day14.rs
+++ b/src/bin/day14.rs
@@ -7,17 +7,37 @@ use std::iter;
 use crypto::digest::Digest;
 use crypto::md5::Md5;
 
-fn md5(salt: &str, index: u32) -> String {
-    let mut md5 = Md5::new();
-    md5.input_str(salt);
-    md5.input_str(&index.to_string());
-    md5.result_str()
+trait Hash {
+    fn hash(salt: &str, index: u32) -> String;
 }
 
-fn solve(salt: &str) -> u32 {
+struct Part1;
+impl Hash for Part1 {
+    fn hash(salt: &str, index: u32) -> String {
+        let mut md5 = Md5::new();
+        md5.input_str(salt);
+        md5.input_str(&index.to_string());
+        md5.result_str()
+    }
+}
+
+struct Part2;
+impl Hash for Part2 {
+    fn hash(salt: &str, index: u32) -> String {
+        let mut hash = Part1::hash(salt, index);
+        for _ in 0..2016 {
+            let mut md5 = Md5::new();
+            md5.input_str(&hash);
+            hash = md5.result_str();
+        }
+        hash
+    }
+}
+
+fn solve<H: Hash>(salt: &str) -> u32 {
     let mut hashes = VecDeque::new();
     for i in 0..1001 {
-        hashes.push_back(md5(salt, i));
+        hashes.push_back(H::hash(salt, i));
     }
 
     let mut keys = 0;
@@ -40,7 +60,7 @@ fn solve(salt: &str) -> u32 {
         }
 
         index += 1;
-        hashes.push_back(md5(salt, 1000 + index));
+        hashes.push_back(H::hash(salt, 1000 + index));
     }
 
     unreachable!()
@@ -50,11 +70,18 @@ fn main() {
     let mut input = String::new();
     io::stdin().read_to_string(&mut input).unwrap();
 
-    println!("Part 1: {}", solve(input.trim()));
+    println!("Part 1: {}", solve::<Part1>(input.trim()));
+    println!("Part 2: {}", solve::<Part2>(input.trim()));
 }
 
 #[test]
 #[ignore]
 fn part1() {
-    assert_eq!(22728, solve("abc"));
+    assert_eq!(22728, solve::<Part1>("abc"));
+}
+
+#[test]
+#[ignore]
+fn part2() {
+    assert_eq!(22551, solve::<Part2>("abc"));
 }
'>2020-08-27Remove rc scriptsJune McEnroe 2020-08-27contrib/palaver: Fix documented database pathJune McEnroe 2020-08-27contrib/palaver: Remove rc scriptJune McEnroe 2020-08-27contrib/palaver: Fix database search and creationJune McEnroe 2020-08-27contrib/palaver: Use pounce's XDG directoryJune McEnroe 2020-08-27contrib/palaver: Only allow HTTPSJune McEnroe 2020-08-25Support the pounce_env rc variableJune McEnroe 2020-08-25Remove deprecated option namesJune McEnroe The next release will be 2.0 so these can be removed now. 2020-08-25Document configuration and data file searchJune McEnroe 2020-08-24Use dataOpen for save fileJune McEnroe 2020-08-24Use configOpen to load localCAJune McEnroe 2020-08-24Use configPath to load client cert/privJune McEnroe 2020-08-24Use configOpen in getopt_configJune McEnroe 2020-08-24Import xdg.c from catgirlJune McEnroe 2020-08-23Replace “RAND_bytes” by “getentropy”Issam E. Maghni This removes the dependency on libcrypto. Signed-off-by: Issam E. Maghni <issam.e.maghni@mailbox.org> 2020-08-16contrib/palaver: Add no message preview flagsJune McEnroe 2020-08-13contrib/palaver: Don't set channel for PMsJune McEnroe 2020-08-13Fix unintended interception of NICK after registrationJune McEnroe Another bug caused by trying to support broken clients. I'm annoyed. 2020-08-12Add Additional Components section to READMEJune McEnroe 2020-08-12Document -L / palaver optionJune McEnroe 2020-08-11contrib/palaver: Document service configurationJune McEnroe 2020-08-11contrib/palaver: Add install target and rc scriptJune McEnroe 2020-08-11contrib/palaver: Implement command and notificationsJune McEnroe