Commit ea4543c5 authored by David's avatar David

Hey it works, and it's simple and fast

parent b2832161
......@@ -3,7 +3,20 @@ package is.kow.adventofcode._2019
object Main extends App {
val p3 = new Puzzle3
val p3Start = System.currentTimeMillis()
println(s"DISTANCE: ${p3.distance("input.txt")}")
println(s"Cheapest path: ${p3.cheapestRoute("input.txt")}")
val p3End = System.currentTimeMillis()
val p32 = new Puzzle3Try2
val p32Start = System.currentTimeMillis()
val distance = Puzzle3Loader.process("input.txt", p32.distanceFromCenter)
val cheapest = Puzzle3Loader.process("input.txt", p32.shortestPath)
val p32End = System.currentTimeMillis()
println(s"Distance: $distance -- Cheapest: $cheapest")
println(s"Original: ${(p3End - p3Start) / 1000}")
println(s"new: ${(p32End - p32Start) / 1000}")
}
......@@ -17,7 +17,6 @@ class Puzzle3 {
val wire1Path = wirePath(wire1String.split(",").toList).reverse
println()
val wire2Path = wirePath(wire2String.split(",").toList).reverse
//Find all the intersections
......@@ -120,7 +119,6 @@ class Puzzle3 {
case right(dist) =>
Wire(startPoint, startPoint.copy(x = startPoint.x + dist.toInt))
}
println(s"${ops.head}: ${wire}")
//Got a wire, accumulate it, and get point
mapPath(wire :: acc, wire.end, ops.tail)
}
......
package is.kow.adventofcode._2019
import scala.io.Source
object Puzzle3Loader {
def process(resource: String, operation: (String, String) => Int): Int = {
val source = Source.fromResource(resource)
try {
val lines = source.getLines().toList
val wire1 = lines.head
val wire2 = lines.tail.head
operation(wire1, wire2)
} finally {
source.close()
}
}
}
package is.kow.adventofcode._2019
class Puzzle3Try2 {
def distanceFromCenter(wire1: String, wire2: String): Int = {
val wire1Points = wireStringToPoints(wire1)
val wire2Points = wireStringToPoints(wire2)
val intersections = wire1Points.intersect(wire2Points)
intersections.foldLeft(Int.MaxValue) { (acc, point) =>
val dist = manhattanDistance(Point(0, 0), point)
if (dist < acc) {
dist
} else {
acc
}
}
}
private def manhattanDistance(p: Point, q: Point): Int = {
Math.abs(p.x - q.x) + Math.abs(p.y - q.y)
}
def shortestPath(wire1: String, wire2: String): Int = {
val wire1Points = wireStringToPoints(wire1)
val wire2Points = wireStringToPoints(wire2)
val intersections = wire1Points.intersect(wire2Points)
intersections.foldLeft(Int.MaxValue) { (acc, point) =>
val dist = wire1Points.indexOf(point) + wire2Points.indexOf(point) + 2
if (dist < acc) dist else acc
}
}
private def wireStringToPoints(wire: String): List[Point] = {
val up = raw"U([0-9]+)".r
val down = raw"D([0-9]+)".r
val left = raw"L([0-9]+)".r
val right = raw"R([0-9]+)".r
wire.split(",").foldLeft(List.empty[Point]) { (acc, op) =>
val lastPoint = if (acc.isEmpty) {
Point(0, 0)
} else {
acc.last
}
val newLine = op match {
case up(dist) => (1 to dist.toInt).map(y => lastPoint.copy(y = lastPoint.y + y))
case down(dist) => (1 to dist.toInt).map(y => lastPoint.copy(y = lastPoint.y - y))
case right(dist) => (1 to dist.toInt).map(x => lastPoint.copy(x = lastPoint.x + x))
case left(dist) => (1 to dist.toInt).map(x => lastPoint.copy(x = lastPoint.x - x))
}
acc ++ newLine
}
}
}
......@@ -10,6 +10,7 @@ case class Wires(wire1: List[String], wire2: List[String], distance: Int)
class Puzzle3Spec extends AnyFunSpec {
def p3 = new Puzzle3
def p32 = new Puzzle3Try2
describe("Day 3 Part 1") {
List(
......@@ -22,6 +23,9 @@ class Puzzle3Spec extends AnyFunSpec {
assert(p3.distance(resource) == expected)
}
it(s"for input $resource expects $expected for version 2") {
assert(Puzzle3Loader.process(resource, p32.distanceFromCenter) == expected)
}
}
}
}
......@@ -36,6 +40,10 @@ class Puzzle3Spec extends AnyFunSpec {
it(s"for input ${resource}, expect $expected path") {
assert(p3.cheapestRoute(resource) == expected)
}
it(s"for input $resource expects $expected for version 2") {
assert(Puzzle3Loader.process(resource, p32.shortestPath) == expected)
}
}
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment