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/lib.rs | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) (limited to 'src/lib.rs') 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