summaryrefslogtreecommitdiff
path: root/src/lib.rs
diff options
context:
space:
mode:
authorJustin Worthe <justin@worthe-it.co.za>2017-12-03 10:27:14 +0200
committerJustin Worthe <justin@worthe-it.co.za>2017-12-03 10:27:14 +0200
commit5d230e12f65b816881d084b8fbe2c52070105ceb (patch)
tree244c0e483cbcf42262ffb85bc468a4d4b1edc179 /src/lib.rs
parent33db46ef6dcdddeb8e9c64ca6f18a1cedfe9de36 (diff)
Day 3: Spirals...
Diffstat (limited to 'src/lib.rs')
-rw-r--r--src/lib.rs82
1 files changed, 82 insertions, 0 deletions
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<i32, std::num::ParseIntError> {
+ self.input[0].parse()
+ }
}
pub fn parse_space_separated_ints(line: &String) -> Result<Vec<i32>, std::num::ParseIntError> {
@@ -65,3 +69,81 @@ pub fn parse_space_separated_ints(line: &String) -> Result<Vec<i32>, std::num::P
.map(|x| x.parse::<i32>())
.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
+ }
+ }
+}