summary refs log tree commit diff homepage
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/bin/day05.rs38
1 files changed, 35 insertions, 3 deletions
diff --git a/src/bin/day05.rs b/src/bin/day05.rs
index a860e53..eefa3df 100644
--- a/src/bin/day05.rs
+++ b/src/bin/day05.rs
@@ -5,7 +5,7 @@ use std::io::{self, Read};
 use crypto::digest::Digest;
 use crypto::md5::Md5;
 
-fn solve(input: &str) -> String {
+fn solve1(input: &str) -> String {
     let mut password = String::new();
     let mut index = 0u64;
 
@@ -25,15 +25,47 @@ fn solve(input: &str) -> String {
     password
 }
 
+fn solve2(input: &str) -> String {
+    let mut password = [None; 8];
+    let mut index = 0u64;
+
+    while !password.iter().all(Option::is_some) {
+        let mut md5 = Md5::new();
+        md5.input_str(input);
+        md5.input_str(&index.to_string());
+        let digest = md5.result_str();
+
+        if &digest[0..5] == "00000" {
+            if let Some(pos @ 0 ... 7) = digest[5..6].parse().ok() {
+                password[pos] = password[pos].or(digest[6..7].chars().next());
+            }
+        }
+
+        index += 1;
+    }
+
+    password.iter()
+        .cloned()
+        .map(Option::unwrap)
+        .collect()
+}
+
 fn main() {
     let mut input = String::new();
     io::stdin().read_to_string(&mut input).unwrap();
 
-    println!("Part 1: {}", solve(input.trim()));
+    println!("Part 1: {}", solve1(input.trim()));
+    println!("Part 2: {}", solve2(input.trim()));
 }
 
 #[test]
 #[ignore]
 fn part1() {
-    assert_eq!("18f47a30", solve("abc"));
+    assert_eq!("18f47a30", solve1("abc"));
+}
+
+#[test]
+#[ignore]
+fn part2() {
+    assert_eq!("05ace8e3", solve2("abc"));
 }
47de1f73d36f89&follow=1'>Remove tweets text fileJune McEnroe 2022-08-04Add Conversations With FriendsJune McEnroe 2022-07-30Add Normal PeopleJune McEnroe 2022-07-26Rewrite glitch from new pngoJune McEnroe 2022-07-26Update Care with time-to-ID and piercingsJune McEnroe 2022-07-26Add -w to upJune McEnroe 2022-07-13Set push.autoSetupRemoteJune McEnroe 2022-07-08Remove TOURJune McEnroe 2022-07-03Add The Bone Shard EmperorJune McEnroe 2022-06-25Bump xterm font size to 12June McEnroe 2022-06-10Handle subshells (and functions) inside substitutionsJune McEnroe 2022-06-10Switch to jorts Install scriptJune McEnroe 2022-06-08Indicate if still reading or no resultsJune McEnroe 2022-06-08Add Maiden, Mother, CroneJune McEnroe 2022-06-05FIRST SHOW IN 2.5 YEARS BABEY!!!June McEnroe 2022-06-03Set line number on File linesJune McEnroe 2022-06-03Stop polling stdin after EOFJune McEnroe 2022-06-02Set TABSIZE=4June McEnroe 2022-06-02Do basic match highlightingJune McEnroe 2022-06-02Clean up parsing a littleJune McEnroe 2022-06-02Don't duplicate path stringJune McEnroe 2022-06-02Use stderr instead of /dev/tty, realloc buffer if lines too longJune McEnroe 2022-06-02Add initial working version of qfJune McEnroe 2022-05-29Set prompt for okshJune McEnroe