summaryrefslogtreecommitdiff
path: root/src
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
parent33db46ef6dcdddeb8e9c64ca6f18a1cedfe9de36 (diff)
Day 3: Spirals...
Diffstat (limited to 'src')
-rw-r--r--src/bin/day_3.rs62
-rw-r--r--src/lib.rs82
2 files changed, 144 insertions, 0 deletions
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<Point, i32> = 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(&current_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(&current.left()).cloned().unwrap_or(0) +
+ memory.get(&current.right()).cloned().unwrap_or(0) +
+ memory.get(&current.up()).cloned().unwrap_or(0) +
+ memory.get(&current.down()).cloned().unwrap_or(0) +
+ memory.get(&current.up().left()).cloned().unwrap_or(0) +
+ memory.get(&current.up().right()).cloned().unwrap_or(0) +
+ memory.get(&current.down().left()).cloned().unwrap_or(0) +
+ memory.get(&current.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<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
+ }
+ }
+}