summary refs log tree commit diff homepage
diff options
context:
space:
mode:
authorJune McEnroe <programble@gmail.com>2016-12-18 14:53:10 -0500
committerJune McEnroe <programble@gmail.com>2016-12-18 14:53:10 -0500
commita6f00bc8caeaef3581cc712731a72844e3e46331 (patch)
tree97d9e917fb807a20921de36d4e677c4b17eccd39
parentDay 17 (diff)
downloadaoc-a6f00bc8caeaef3581cc712731a72844e3e46331.tar.gz
aoc-a6f00bc8caeaef3581cc712731a72844e3e46331.zip
Day 17 part 2
-rw-r--r--src/bin/day17.rs36
1 files changed, 31 insertions, 5 deletions
diff --git a/src/bin/day17.rs b/src/bin/day17.rs
index a80cbb8..c769f32 100644
--- a/src/bin/day17.rs
+++ b/src/bin/day17.rs
@@ -93,7 +93,7 @@ impl State {
     }
 }
 
-fn solve(passcode: &str) -> String {
+fn solve1(passcode: &str) -> String {
     let mut states = VecDeque::new();
     states.push_back(State::default());
 
@@ -107,16 +107,42 @@ fn solve(passcode: &str) -> String {
     panic!("no path to vault")
 }
 
+fn solve2(passcode: &str) -> usize {
+    let mut states = VecDeque::new();
+    states.push_back(State::default());
+
+    let mut longest = None;
+
+    while let Some(state) = states.pop_front() {
+        if state.is_vault() {
+            longest = Some(state.clone());
+        } else {
+            state.generate_states(&mut states, passcode);
+        }
+    }
+
+    longest.unwrap().path.len()
+}
+
 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]
 fn part1() {
-    assert_eq!("DDRRRD", solve("ihgpwlah"));
-    assert_eq!("DDUDRLRRUDRD", solve("kglvqrro"));
-    assert_eq!("DRURDRUDDLLDLUURRDULRLDUUDDDRR", solve("ulqzkmiv"));
+    assert_eq!("DDRRRD", solve1("ihgpwlah"));
+    assert_eq!("DDUDRLRRUDRD", solve1("kglvqrro"));
+    assert_eq!("DRURDRUDDLLDLUURRDULRLDUUDDDRR", solve1("ulqzkmiv"));
+}
+
+#[test]
+#[ignore]
+fn part2() {
+    assert_eq!(370, solve2("ihgpwlah"));
+    assert_eq!(492, solve2("kglvqrro"));
+    assert_eq!(830, solve2("ulqzkmiv"));
 }