diff options
Diffstat (limited to '2016/aoc1')
-rw-r--r-- | 2016/aoc1/Cargo.lock | 4 | ||||
-rw-r--r-- | 2016/aoc1/Cargo.toml | 6 | ||||
-rw-r--r-- | 2016/aoc1/input.txt | 1 | ||||
-rw-r--r-- | 2016/aoc1/src/main.rs | 98 |
4 files changed, 109 insertions, 0 deletions
diff --git a/2016/aoc1/Cargo.lock b/2016/aoc1/Cargo.lock new file mode 100644 index 0000000..b771521 --- /dev/null +++ b/2016/aoc1/Cargo.lock @@ -0,0 +1,4 @@ +[root] +name = "aoc1" +version = "0.1.0" + diff --git a/2016/aoc1/Cargo.toml b/2016/aoc1/Cargo.toml new file mode 100644 index 0000000..096aad6 --- /dev/null +++ b/2016/aoc1/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "aoc1" +version = "0.1.0" +authors = ["Justin Worthe <justin.worthe@entelect.co.za>"] + +[dependencies] diff --git a/2016/aoc1/input.txt b/2016/aoc1/input.txt new file mode 100644 index 0000000..456904d --- /dev/null +++ b/2016/aoc1/input.txt @@ -0,0 +1 @@ +R4, R5, L5, L5, L3, R2, R1, R1, L5, R5, R2, L1, L3, L4, R3, L1, L1, R2, R3, R3, R1, L3, L5, R3, R1, L1, R1, R2, L1, L4, L5, R4, R2, L192, R5, L2, R53, R1, L5, R73, R5, L5, R186, L3, L2, R1, R3, L3, L3, R1, L4, L2, R3, L5, R4, R3, R1, L1, R5, R2, R1, R1, R1, R3, R2, L1, R5, R1, L5, R2, L2, L4, R3, L1, R4, L5, R4, R3, L5, L3, R4, R2, L5, L5, R2, R3, R5, R4, R2, R1, L1, L5, L2, L3, L4, L5, L4, L5, L1, R3, R4, R5, R3, L5, L4, L3, L1, L4, R2, R5, R5, R4, L2, L4, R3, R1, L2, R5, L5, R1, R1, L1, L5, L5, L2, L1, R5, R2, L4, L1, R4, R3, L3, R1, R5, L1, L4, R2, L3, R5, R3, R1, L3 diff --git a/2016/aoc1/src/main.rs b/2016/aoc1/src/main.rs new file mode 100644 index 0000000..e9b4138 --- /dev/null +++ b/2016/aoc1/src/main.rs @@ -0,0 +1,98 @@ +use std::io::Read; +use std::fs::File; + +#[derive(Clone, Copy, Debug)] +enum Direction { + Up, + Left, + Down, + Right +} + +impl Direction { + fn turn_right(self) -> Direction { + match self { + Direction::Up => Direction::Right, + Direction::Right => Direction::Down, + Direction::Down => Direction::Left, + Direction::Left => Direction::Up + } + } + fn turn_left(self) -> Direction { + //not an ambiturner + self.turn_right().turn_right().turn_right() + } + + fn as_vector(self, dist: i32) -> (i32,i32) { + match self { + Direction::Up => (0, -dist), + Direction::Right => (dist, 0), + Direction::Down => (0, dist), + Direction::Left => (-dist, 0) + } + } +} + +fn main() { + let content = read_file().expect("Failed to read file"); + let (_, dist_x, dist_y) = first_repeated_dest(content); + println!("Total: ({}, {})", dist_x, dist_y); + println!("Net: {}", dist_x+dist_y); +} + +fn net_distance(content: String) -> (Direction, i32, i32) { + content.trim().split(", ") + .map(|action| + ( + action.chars().nth(0).unwrap(), + action.chars().skip(1).collect::<String>().parse::<i32>().unwrap() + )) + .fold((Direction::Up, 0,0), |(facing, acc_x, acc_y), (dir, dist)| { + let new_facing = match dir { + 'R' => facing.turn_right(), + 'L' => facing.turn_left(), + _ => panic!("bad input") + }; + + let (new_x, new_y) = new_facing.as_vector(dist); + (new_facing, acc_x+new_x, acc_y+new_y) + }) +} + +fn first_repeated_dest(content: String) -> (Direction, i32, i32) { + let mut stops: Vec<(i32, i32)> = Vec::new(); + let actions = content.trim().split(", ") + .map(|action| + ( + action.chars().nth(0).unwrap(), + action.chars().skip(1).collect::<String>().parse::<i32>().unwrap() + )); + let mut current_facing = Direction::Up; + let (mut acc_x, mut acc_y) = (0, 0); + for (dir, dist) in actions { + current_facing = match dir { + 'R' => current_facing.turn_right(), + 'L' => current_facing.turn_left(), + _ => panic!("bad input") + }; + + for _ in 0..dist { + let (new_x, new_y) = current_facing.as_vector(1); + acc_x += new_x; + acc_y += new_y; + + if stops.iter().any(|&(x, y)| x==acc_x && y==acc_y) { + return (current_facing, acc_x, acc_y); + } + stops.push((acc_x, acc_y)); + } + } + (current_facing, acc_x, acc_y) +} + +fn read_file() -> Result<String, String> { + let mut file = try!(File::open("input.txt").map_err(|e| e.to_string())); + let mut content = String::new(); + try!(file.read_to_string(&mut content).map_err(|e| e.to_string())); + Ok(content) +} |