Add day 19 part 2 solution

This commit is contained in:
Wesley Moore 2017-12-20 08:03:16 +11:00
parent a2dcabfc80
commit 8f022e1d02
2 changed files with 33 additions and 4 deletions

View file

@ -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.) 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.

View file

@ -86,18 +86,22 @@ fn main() {
let diagram = Diagram::new(input); 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 // 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 col = diagram.start_col();
let mut row = 0; let mut row = 0;
let mut direction = Down; let mut direction = Down;
let mut path = String::new(); let mut path = String::new();
let mut steps = 0;
loop { loop {
steps += 1;
let c = diagram.char_at(row, col); let c = diagram.char_at(row, col);
// println!("Visit ({}, {}): '{}'", col, row, c); // println!("Visit ({}, {}): '{}'", col, row, c);
@ -139,7 +143,7 @@ fn navigate(diagram: &Diagram) -> String {
break; break;
} }
path (steps, path)
} }
#[test] #[test]
@ -153,5 +157,5 @@ let data = b" |
"; ";
let diagram = Diagram::new(data.to_vec()); let diagram = Diagram::new(data.to_vec());
assert_eq!(navigate(&diagram), "ABCDEF".to_string()) assert_eq!(navigate(&diagram), (38, "ABCDEF".to_string()))
} }