From 5d230e12f65b816881d084b8fbe2c52070105ceb Mon Sep 17 00:00:00 2001 From: Justin Worthe Date: Sun, 3 Dec 2017 10:27:14 +0200 Subject: Day 3: Spirals... --- src/bin/day_3.rs | 62 ++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 144 insertions(+) create mode 100644 src/bin/day_3.rs (limited to 'src') diff --git a/src/bin/day_3.rs b/src/bin/day_3.rs new file mode 100644 index 0000000..69ded88 --- /dev/null +++ b/src/bin/day_3.rs @@ -0,0 +1,62 @@ +extern crate advent_of_code_2017; +use advent_of_code_2017::*; + +use std::collections::HashMap; + +fn main() { + use Direction::*; + + let args = AdventArgs::init(); + let input = args.one_number_input().unwrap(); + + let mut memory: HashMap = HashMap::new(); + let mut last_allocated = 1; + + let mut current = Point { + x: 0, + y: 0 + }; + memory.insert(current, last_allocated); + + let mut steps_per_direction = 1; + let mut steps_to_next_turn = 1; + let mut turns_to_spiral_increase = 2; + + let mut current_index = 1; + let mut current_direction = Right; + + while (args.part == 1 && current_index != input) || (args.part == 2 && last_allocated < input) { + current = current.shift(¤t_direction); + current_index += 1; + + steps_to_next_turn -= 1; + if steps_to_next_turn == 0 { + current_direction = current_direction.rotate_left(); + turns_to_spiral_increase -= 1; + if turns_to_spiral_increase == 0 { + steps_per_direction += 1; + turns_to_spiral_increase = 2; + } + + steps_to_next_turn = steps_per_direction; + } + + if args.part == 2 { + last_allocated = memory.get(¤t.left()).cloned().unwrap_or(0) + + memory.get(¤t.right()).cloned().unwrap_or(0) + + memory.get(¤t.up()).cloned().unwrap_or(0) + + memory.get(¤t.down()).cloned().unwrap_or(0) + + memory.get(¤t.up().left()).cloned().unwrap_or(0) + + memory.get(¤t.up().right()).cloned().unwrap_or(0) + + memory.get(¤t.down().left()).cloned().unwrap_or(0) + + memory.get(¤t.down().right()).cloned().unwrap_or(0); + + memory.insert(current, last_allocated); + } + } + + println!("{:?}", current); + println!("Distance: {}", current.x.abs() + current.y.abs()); + println!("Last Allocated Value: {}", last_allocated); + +} diff --git a/src/lib.rs b/src/lib.rs index 5d3721f..caa8881 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -58,6 +58,10 @@ impl AdventArgs { .map(|line| line.trim().to_string()) .collect() } + + pub fn one_number_input(&self) -> Result { + self.input[0].parse() + } } pub fn parse_space_separated_ints(line: &String) -> Result, std::num::ParseIntError> { @@ -65,3 +69,81 @@ pub fn parse_space_separated_ints(line: &String) -> Result, std::num::P .map(|x| x.parse::()) .collect() } + + +#[derive(Hash, Eq, PartialEq, Debug, Clone, Copy)] +pub struct Point { + pub x: i32, + pub y: i32 +} + +impl Point { + pub fn up(&self) -> Point { + Point { + y: self.y-1, + ..*self + } + } + + pub fn down(&self) -> Point { + Point { + y: self.y+1, + ..*self + } + } + + pub fn left(&self) -> Point { + Point { + x: self.x-1, + ..*self + } + } + + pub fn right(&self) -> Point { + Point { + x: self.x+1, + ..*self + } + } + + pub fn shift(&self, dir: &Direction) -> Point { + use Direction::*; + + match *dir { + Right => self.right(), + Left => self.left(), + Up => self.up(), + Down => self.down() + } + } +} + +#[derive(Debug)] +pub enum Direction { + Left, + Up, + Down, + Right +} + +impl Direction { + pub fn rotate_left(&self) -> Direction { + use Direction::*; + match *self { + Right => Up, + Up => Left, + Left => Down, + Down => Right + } + } + + pub fn rotate_right(&self) -> Direction { + use Direction::*; + match *self { + Right => Down, + Up => Right, + Left => Up, + Down => Left + } + } +} -- cgit v1.2.3