From 9f0b9b4ef8d7c148dc4c8e989c2751f15bcb91b4 Mon Sep 17 00:00:00 2001 From: Bert Peters Date: Thu, 1 Dec 2016 13:18:17 +0100 Subject: [PATCH] Add solution for Day 01 of 2016. --- 2016/day-01/input.txt | 1 + 2016/day-01/sample.txt | 1 + 2016/day-01/solution.rs | 86 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 88 insertions(+) create mode 100644 2016/day-01/input.txt create mode 100644 2016/day-01/sample.txt create mode 100644 2016/day-01/solution.rs diff --git a/2016/day-01/input.txt b/2016/day-01/input.txt new file mode 100644 index 0000000..2288cc5 --- /dev/null +++ b/2016/day-01/input.txt @@ -0,0 +1 @@ +R4, R3, L3, L2, L1, R1, L1, R2, R3, L5, L5, R4, L4, R2, R4, L3, R3, L3, R3, R4, R2, L1, R2, L3, L2, L1, R3, R5, L1, L4, R2, L4, R3, R1, R2, L5, R2, L189, R5, L5, R52, R3, L1, R4, R5, R1, R4, L1, L3, R2, L2, L3, R4, R3, L2, L5, R4, R5, L2, R2, L1, L3, R3, L4, R4, R5, L1, L1, R3, L5, L2, R76, R2, R2, L1, L3, R189, L3, L4, L1, L3, R5, R4, L1, R1, L1, L1, R2, L4, R2, L5, L5, L5, R2, L4, L5, R4, R4, R5, L5, R3, L1, L3, L1, L1, L3, L4, R5, L3, R5, R3, R3, L5, L5, R3, R4, L3, R3, R1, R3, R2, R2, L1, R1, L3, L3, L3, L1, R2, L1, R4, R4, L1, L1, R3, R3, R4, R1, L5, L2, R2, R3, R2, L3, R4, L5, R1, R4, R5, R4, L4, R1, L3, R1, R3, L2, L3, R1, L2, R3, L3, L1, L3, R4, L4, L5, R3, R5, R4, R1, L2, R3, R5, L5, L4, L1, L1 diff --git a/2016/day-01/sample.txt b/2016/day-01/sample.txt new file mode 100644 index 0000000..1d6615f --- /dev/null +++ b/2016/day-01/sample.txt @@ -0,0 +1 @@ +R8, R4, R4, R8 diff --git a/2016/day-01/solution.rs b/2016/day-01/solution.rs new file mode 100644 index 0000000..60285f9 --- /dev/null +++ b/2016/day-01/solution.rs @@ -0,0 +1,86 @@ +use std::collections::HashSet; +use std::error::Error; +use std::env; +use std::fs::File; +use std::io::prelude::*; +use std::path::Path; +use std::str; + +fn dist(pos: (i32, i32)) -> i32 { + let (x, y) = pos; + + return x.abs() + y.abs() +} + +fn main() { + let args: Vec = env::args().collect(); + let path = Path::new(&args[1]); + let display = path.display(); + + // Open the path in read-only mode, returns `io::Result` + let mut file = match File::open(&path) { + // The `description` method of `io::Error` returns a string that + // describes the error + Err(why) => panic!("couldn't open {}: {}", display, + why.description()), + Ok(file) => file, + }; + + let mut content = String::new(); + match file.read_to_string(&mut content) { + Err(why) => panic!("couldn't open {}: {}", display, + why.description()), + Ok(_) => {}, + }; + + let mut posx = 0; + let mut posy = 0; + let mut dir = 0; + let mut found = false; + + let mut positions = HashSet::new(); + positions.insert((posx, posy)); + + for instruction in content.split(", ") { + let turn = &instruction[..1]; + let steps_opt: Option = instruction[1..].trim().parse().ok(); + let steps = match steps_opt { + Some(num) => num, + None => panic!("Could note parse number of steps"), + }; + if turn == "R" { + dir += 1; + } else { + dir -= 1; + } + dir = (dir + 4) % 4; + + let backwards = if dir & 2 == 2 { -1 } else { 1 }; + if dir & 1 == 1 { + // Move in y direction + for y in 1..(steps + 1) { + if positions.contains(&(posx, posy + y * backwards)) && !found { + found = true; + println!("Arrived at the same position, dist {}", dist((posx, posy + y * backwards))); + } else { + positions.insert((posx, posy + y * backwards)); + } + } + posy += steps * backwards; + } else { + // Move in x direction + for x in 1..(steps + 1) { + if positions.contains(&(posx + x * backwards, posy)) && !found { + println!("Arrived at the same position, dist {}", dist((posx + x * backwards, posy))); + found = true; + } else { + positions.insert((posx + x * backwards, posy)); + } + } + posx += steps * backwards; + } + } + + println!("Total distance is {}", dist((posx, posy))) + +}