summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJustin Worthe <justin@worthe-it.co.za>2017-12-19 07:26:34 +0200
committerJustin Worthe <justin@worthe-it.co.za>2017-12-19 07:26:34 +0200
commitc59a26fec18b995e3dafeff764f25da884f02e3e (patch)
tree7ca9b0094db3b30ecb8b13c734b38f9f0306d52f /src
parent6038959cee487e03337bcd673ba656fb9ba45e7b (diff)
Day 19: Follow the path
I had to change my input stuff for this one. I hope no longer trimming leading whitespace won't break any previous solutions.
Diffstat (limited to 'src')
-rw-r--r--src/bin/day_19.rs78
-rw-r--r--src/lib.rs2
2 files changed, 79 insertions, 1 deletions
diff --git a/src/bin/day_19.rs b/src/bin/day_19.rs
new file mode 100644
index 0000000..b333c98
--- /dev/null
+++ b/src/bin/day_19.rs
@@ -0,0 +1,78 @@
+extern crate advent_of_code_2017;
+use advent_of_code_2017::*;
+
+fn main() {
+ use Direction::*;
+
+ let args = AdventArgs::init();
+
+ let input: Vec<Vec<char>> = args.input.iter().map(|line| line.chars().collect()).collect();
+
+ let mut position = Point {
+ x: input[0].iter().position(|&c| c == '|').unwrap() as i32,
+ y: 0
+ };
+
+ let mut direction = Down;
+ let mut path_ended = false;
+ let mut tokens = Vec::new();
+
+ // moving onto the map counts as one, but because of how I'm
+ // counting there's also an off the map step that I shouldn't be
+ // counting at the end. They cancel out.
+ let mut steps_moved = 0;
+
+ while !path_ended {
+ position = position.shift(&direction);
+ steps_moved += 1;
+
+ match char_at(&input, &position) {
+ '|' | '-' => {
+ //continue as is
+ },
+ ' ' => {
+ path_ended = true;
+ },
+ '+' => {
+ let left_option = char_at(&input, &position.shift(&direction.rotate_left()));
+ let right_option = char_at(&input, &position.shift(&direction.rotate_right()));
+ match (left_option, right_option) {
+ (' ', ' ') => {
+ path_ended = true;
+ },
+ (_, ' ') => {
+ direction = direction.rotate_left();
+ },
+ (' ', _) => {
+ direction = direction.rotate_right();
+ },
+ _ => {
+ panic!("Don't know where to go from {:?}", position);
+ }
+ }
+ },
+ token => {
+ tokens.push(token);
+ }
+ }
+
+ }
+
+ if args.part == 1 {
+ println!("{}", tokens.iter().collect::<String>());
+ } else {
+ println!("{}", steps_moved);
+ }
+}
+
+fn char_at(input: &Vec<Vec<char>>, position: &Point) -> char {
+ if position.y < 0 ||
+ position.x < 0 ||
+ position.y as usize >= input.len() ||
+ position.x as usize >= input[position.y as usize].len() {
+ ' '
+ } else {
+ input[position.y as usize][position.x as usize]
+ }
+
+}
diff --git a/src/lib.rs b/src/lib.rs
index 87d0c0b..452e67a 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -55,7 +55,7 @@ impl AdventArgs {
fn preprocess_file_lines(lines: Vec<String>) -> Vec<String> {
lines.iter()
.filter(|line| line.len() > 0)
- .map(|line| line.trim().to_string())
+ .map(|line| line.trim_right().to_string())
.collect()
}