From 8f022e1d026172aec054749a5f46b0c8ad3968eb Mon Sep 17 00:00:00 2001 From: Wesley Moore Date: Wed, 20 Dec 2017 08:03:16 +1100 Subject: [PATCH] Add day 19 part 2 solution --- 2017/day/19/problem.txt | 25 +++++++++++++++++++++++++ 2017/day/19/src/main.rs | 12 ++++++++---- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/2017/day/19/problem.txt b/2017/day/19/problem.txt index 50ca463..7348291 100644 --- a/2017/day/19/problem.txt +++ b/2017/day/19/problem.txt @@ -24,3 +24,28 @@ Following the path to the end, the letters it sees on its path are ABCDEF. The little packet looks up at you, hoping you can help it find the way. What letters will it see (in the order it would see them) if it follows the path? (The routing diagram is very wide; make sure you view it without line wrapping.) +--- Part Two --- + +The packet is curious how many steps it needs to go. + +For example, using the same routing diagram from the example above... + + | + | +--+ + A | C + F---|--|-E---+ + | | | D + +B-+ +--+ + +...the packet would go: + + 6 steps down (including the first line at the top of the diagram). + 3 steps right. + 4 steps up. + 3 steps right. + 4 steps down. + 3 steps right. + 2 steps up. + 13 steps left (including the F it stops on). + +This would result in a total of 38 steps. diff --git a/2017/day/19/src/main.rs b/2017/day/19/src/main.rs index 213e6c3..3165b29 100644 --- a/2017/day/19/src/main.rs +++ b/2017/day/19/src/main.rs @@ -86,18 +86,22 @@ fn main() { let diagram = Diagram::new(input); - println!("{}", navigate(&diagram)); + let (steps, path) = navigate(&diagram); + + println!("{} in {} steps", path, steps); } // This is pretty ugly... can be improved -fn navigate(diagram: &Diagram) -> String { +fn navigate(diagram: &Diagram) -> (usize, String) { let mut col = diagram.start_col(); let mut row = 0; let mut direction = Down; let mut path = String::new(); + let mut steps = 0; loop { + steps += 1; let c = diagram.char_at(row, col); // println!("Visit ({}, {}): '{}'", col, row, c); @@ -139,7 +143,7 @@ fn navigate(diagram: &Diagram) -> String { break; } - path + (steps, path) } #[test] @@ -153,5 +157,5 @@ let data = b" | "; let diagram = Diagram::new(data.to_vec()); - assert_eq!(navigate(&diagram), "ABCDEF".to_string()) + assert_eq!(navigate(&diagram), (38, "ABCDEF".to_string())) }