summary refs log tree commit diff homepage
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/bin/day21.rs30
1 files changed, 27 insertions, 3 deletions
diff --git a/src/bin/day21.rs b/src/bin/day21.rs
index c9abb76..982bf8a 100644
--- a/src/bin/day21.rs
+++ b/src/bin/day21.rs
@@ -87,7 +87,7 @@ impl<'a> From<&'a str> for Operation {
     }
 }
 
-fn solve(password: &str, input: &str) -> String {
+fn solve1(password: &str, input: &str) -> String {
     let mut password = password.as_bytes().to_owned();
 
     for operation in input.lines().map(Operation::from) {
@@ -97,11 +97,35 @@ fn solve(password: &str, input: &str) -> String {
     String::from_utf8(password).unwrap()
 }
 
+fn solve2(scrambled: &str, input: &str) -> String {
+    let operations: Vec<_> = input.lines().map(Operation::from).collect();
+
+    let mut password = "abcdefgh".as_bytes().to_owned();
+    let len = password.len();
+
+    loop {
+        let k = (0..(len - 1)).rev().find(|&k| password[k] < password [k + 1]).unwrap();
+        let l = ((k + 1)..len).rev().find(|&l| password[k] < password[l]).unwrap();
+        password.swap(k, l);
+        password[(k + 1)..].reverse();
+
+        let mut candidate = password.clone();
+        for operation in &operations {
+            operation.apply(&mut candidate);
+        }
+
+        if candidate == scrambled.as_bytes() {
+            return String::from_utf8(password).unwrap();
+        }
+    }
+}
+
 fn main() {
     let mut input = String::new();
     io::stdin().read_to_string(&mut input).unwrap();
 
-    println!("Part 1: {}", solve("abcdefgh", &input));
+    println!("Part 1: {}", solve1("abcdefgh", &input));
+    println!("Part 2: {}", solve2("fbgdceah", &input));
 }
 
 #[test]
@@ -116,5 +140,5 @@ move position 3 to position 0
 rotate based on position of letter b
 rotate based on position of letter d
 ";
-    assert_eq!("decab", solve("abcde", input.trim()));
+    assert_eq!("decab", solve1("abcde", input.trim()));
 }
-10-27Allow cd host:path over sshJune McEnroe 2020-10-07Use mandoc -T utf8 for text.June McEnroe 2020-09-20Add The Awakened KingdomJune McEnroe 2020-09-12Move /opt/local back, cheat port select to use system manJune McEnroe 2020-09-12Move /opt/local behind /usr againJune McEnroe 2020-09-12Enable toc in cgit renderings of man pagesJune McEnroe 2020-09-11Install mandoc on macOSJune McEnroe 2020-09-11Rewrite install script yet againJune McEnroe 2020-09-11Remove NetBSD from install scriptJune McEnroe 2020-09-11Use MacPorts rather than pkgsrcJune McEnroe 2020-09-11Add debian VM name to sshJune McEnroe 2020-09-11Add influencer tweetJune McEnroe 2020-09-10Add The Kingdom of GodsJune McEnroe 2020-09-07Add SunglassesJune McEnroe 2020-09-06Add Between the BreathsJune McEnroe 2020-09-04Open /dev/tty in nudgeJune McEnroe 2020-09-04Add nudgeJune McEnroe 2020-09-03Build fbclock with -lzJune McEnroe 2020-08-29Add tweets from retweetsJune McEnroe