From 403f2a2b43d25d30f9b0e41c548017650505f48c Mon Sep 17 00:00:00 2001 From: Justin Worthe Date: Fri, 2 Dec 2016 07:51:59 +0200 Subject: Started AOC --- aoc1/Cargo.lock | 4 + aoc1/Cargo.toml | 6 ++ aoc1/input.txt | 1 + aoc1/src/main.rs | 98 +++++++++++++++++++++ .../.fingerprint/aoc1-5796587de7530f99/bin-aoc1 | 1 + .../aoc1-5796587de7530f99/bin-aoc1.json | 1 + .../aoc1-5796587de7530f99/dep-bin-aoc1 | Bin 0 -> 212 bytes aoc1/target/debug/aoc1.exe | Bin 0 -> 2601718 bytes 8 files changed, 111 insertions(+) create mode 100644 aoc1/Cargo.lock create mode 100644 aoc1/Cargo.toml create mode 100644 aoc1/input.txt create mode 100644 aoc1/src/main.rs create mode 100644 aoc1/target/debug/.fingerprint/aoc1-5796587de7530f99/bin-aoc1 create mode 100644 aoc1/target/debug/.fingerprint/aoc1-5796587de7530f99/bin-aoc1.json create mode 100644 aoc1/target/debug/.fingerprint/aoc1-5796587de7530f99/dep-bin-aoc1 create mode 100644 aoc1/target/debug/aoc1.exe (limited to 'aoc1') diff --git a/aoc1/Cargo.lock b/aoc1/Cargo.lock new file mode 100644 index 0000000..b771521 --- /dev/null +++ b/aoc1/Cargo.lock @@ -0,0 +1,4 @@ +[root] +name = "aoc1" +version = "0.1.0" + diff --git a/aoc1/Cargo.toml b/aoc1/Cargo.toml new file mode 100644 index 0000000..096aad6 --- /dev/null +++ b/aoc1/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "aoc1" +version = "0.1.0" +authors = ["Justin Worthe "] + +[dependencies] diff --git a/aoc1/input.txt b/aoc1/input.txt new file mode 100644 index 0000000..456904d --- /dev/null +++ b/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/aoc1/src/main.rs b/aoc1/src/main.rs new file mode 100644 index 0000000..e9b4138 --- /dev/null +++ b/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::().parse::().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::().parse::().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 { + 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) +} diff --git a/aoc1/target/debug/.fingerprint/aoc1-5796587de7530f99/bin-aoc1 b/aoc1/target/debug/.fingerprint/aoc1-5796587de7530f99/bin-aoc1 new file mode 100644 index 0000000..1ef2d34 --- /dev/null +++ b/aoc1/target/debug/.fingerprint/aoc1-5796587de7530f99/bin-aoc1 @@ -0,0 +1 @@ +aa49b87236c3d407 \ No newline at end of file diff --git a/aoc1/target/debug/.fingerprint/aoc1-5796587de7530f99/bin-aoc1.json b/aoc1/target/debug/.fingerprint/aoc1-5796587de7530f99/bin-aoc1.json new file mode 100644 index 0000000..d370c06 --- /dev/null +++ b/aoc1/target/debug/.fingerprint/aoc1-5796587de7530f99/bin-aoc1.json @@ -0,0 +1 @@ +{"rustc":3005860601515633040,"target":7416218395859320575,"profile":13023446655302672390,"local":{"variant":"MtimeBased","fields":[[1480577027,916536700],[67,58,92,80,114,111,106,92,67,111,109,112,101,116,105,116,105,111,110,115,92,97,100,118,101,110,116,111,102,99,111,100,101,50,48,49,54,92,97,111,99,49,92,116,97,114,103,101,116,92,100,101,98,117,103,92,46,102,105,110,103,101,114,112,114,105,110,116,92,97,111,99,49,45,53,55,57,54,53,56,55,100,101,55,53,51,48,102,57,57,92,100,101,112,45,98,105,110,45,97,111,99,49]]},"features":"None","deps":[]} \ No newline at end of file diff --git a/aoc1/target/debug/.fingerprint/aoc1-5796587de7530f99/dep-bin-aoc1 b/aoc1/target/debug/.fingerprint/aoc1-5796587de7530f99/dep-bin-aoc1 new file mode 100644 index 0000000..f805f30 Binary files /dev/null and b/aoc1/target/debug/.fingerprint/aoc1-5796587de7530f99/dep-bin-aoc1 differ diff --git a/aoc1/target/debug/aoc1.exe b/aoc1/target/debug/aoc1.exe new file mode 100644 index 0000000..e594df4 Binary files /dev/null and b/aoc1/target/debug/aoc1.exe differ -- cgit v1.2.3