From bd775b28dbd5b169b05df33b96ef35ac16ab3e3e Mon Sep 17 00:00:00 2001 From: Soren Date: Sun, 25 Nov 2018 21:18:20 -0800 Subject: [PATCH] Day 11 --- aoc/src/main/scala/aoc/y2017/Day03.scala | 8 +- aoc/src/main/scala/aoc/y2017/Day11.scala | 89 ++++++++++++++++++++++ aoc/src/main/scala/aoc/y2017/package.scala | 7 ++ 3 files changed, 98 insertions(+), 6 deletions(-) create mode 100644 aoc/src/main/scala/aoc/y2017/Day11.scala diff --git a/aoc/src/main/scala/aoc/y2017/Day03.scala b/aoc/src/main/scala/aoc/y2017/Day03.scala index b0cfc02..5917aaa 100644 --- a/aoc/src/main/scala/aoc/y2017/Day03.scala +++ b/aoc/src/main/scala/aoc/y2017/Day03.scala @@ -18,11 +18,6 @@ object Day03 extends Day { } override def part2(input: String): String = { - implicit class TupleInt2Ops(t: (Int, Int)) { - def +(o: (Int, Int)): (Int, Int) = (t, o) match { - case ((x1, y1), (x2, y2)) => (x1 + x2, y1 + y2) - } - } sealed trait Direction { def change: (Int, Int) } @@ -43,7 +38,8 @@ object Day03 extends Day { (-1, 1), (0, 1), (1, 1), - (-1, 0), /* */ (1, 0), + (-1, 0), + (1, 0), (-1, -1), (0, -1), (1, -1) diff --git a/aoc/src/main/scala/aoc/y2017/Day11.scala b/aoc/src/main/scala/aoc/y2017/Day11.scala new file mode 100644 index 0000000..306561a --- /dev/null +++ b/aoc/src/main/scala/aoc/y2017/Day11.scala @@ -0,0 +1,89 @@ +package aoc.y2017 + +import aoc.Day + +object Day11 extends Day { + + import fastparse._, NoWhitespace._ + + sealed trait Direction { + def points: (Int, Int) + } + case object North extends Direction { + override def points: (Int, Int) = (0, 2) + } + case object NorthWest extends Direction { + override def points: (Int, Int) = (-1, 1) + } + case object NorthEast extends Direction { + override def points: (Int, Int) = (1, 1) + } + case object South extends Direction { + override def points: (Int, Int) = (0, -2) + } + case object SouthWest extends Direction { + override def points: (Int, Int) = (-1, -1) + } + case object SouthEast extends Direction { + override def points: (Int, Int) = (1, -1) + } + + val strMap = Map( + "n" -> North, + "s" -> South, + "ne" -> NorthEast, + "nw" -> NorthWest, + "se" -> SouthEast, + "sw" -> SouthWest + ) + + def directionParser[_: P]: P[Direction] = P("ne" | "nw" | "se" | "sw" | "n" | "s").!.map(strMap) + def directionsParser[_: P]: P[List[Direction]] = P(directionParser.rep(1, ","./)).map(_.toList) + + implicit class DirectionListOps(l: List[Direction]) { + + def total: (Int, Int) = { + l.map(_.points).reduce(_ + _) + } + + } + + def getDirections(input: String): List[Direction] = { + val Parsed.Success(list, _) = parse(input, directionsParser(_)) + list + } + + def getDistance(directions: List[Direction]): Int = { + val (tx, ty) = directions.total + val dx = Math.abs(tx) + val dy = Math.abs(ty) + Math.abs(dy - dx) match { + case diag if dy > dx => + val remaining = dy - diag + (diag / 2) + remaining + case _ if dx >= dy => + dx + } + } + + override def part1(input: String): String = { + val directions = getDirections(input) + val td = getDistance(directions) + td.toString + } + + override def part2(input: String): String = { + val directions = getDirections(input) + def findFurthest(directions: List[Direction], step: Int = 1, max: Int = 0): Int = { + if(step > directions.length) { + max + } else { + val mds = directions.take(step) + val md = getDistance(mds) + findFurthest(directions, step + 1, Math.max(max, md)) + } + } + findFurthest(directions).toString + } + +} diff --git a/aoc/src/main/scala/aoc/y2017/package.scala b/aoc/src/main/scala/aoc/y2017/package.scala index e48361d..3ce0a11 100644 --- a/aoc/src/main/scala/aoc/y2017/package.scala +++ b/aoc/src/main/scala/aoc/y2017/package.scala @@ -15,6 +15,7 @@ package object y2017 extends Year { "8" -> Day08, "9" -> Day09, "10" -> Day10, + "11" -> Day11, "25" -> Day25 ) @@ -28,4 +29,10 @@ package object y2017 extends Year { list.drop(size - (i % size)) ++ list.take(size - (i % size)) } + implicit class TupleInt2Ops(t: (Int, Int)) { + def +(o: (Int, Int)): (Int, Int) = (t, o) match { + case ((x1, y1), (x2, y2)) => (x1 + x2, y1 + y2) + } + } + }