Commit b2832161 authored by David's avatar David

Part 2 tests pass, but doesn't get the right answer :(

parent ef4bc033
......@@ -4,4 +4,6 @@ object Main extends App {
val p3 = new Puzzle3
println(s"DISTANCE: ${p3.distance("input.txt")}")
println(s"Cheapest path: ${p3.cheapestRoute("input.txt")}")
}
......@@ -20,9 +20,6 @@ class Puzzle3 {
println()
val wire2Path = wirePath(wire2String.split(",").toList).reverse
println("WIRE 1: " + wire1Path)
println("WIRE 2: " + wire2Path)
//Find all the intersections
val points: List[Point] = wire1Path.iterator.flatMap { wire1 =>
//See if it intersects with any wire, and get that point
......@@ -34,9 +31,8 @@ class Puzzle3 {
//Don't count 0,0
val intersections = points.drop(1)
println("Intersecting points: " + intersections)
val distances = intersections.map(manhattanDistance(Point(0, 0), _));
val distances = intersections.map(manhattanDistance(Point(0, 0), _))
val smallest = distances.foldLeft(distances.head) { (acc, v) =>
if (v < acc) {
v
......@@ -51,6 +47,55 @@ class Puzzle3 {
}
}
def cheapestRoute(resource: String): Int = {
//load from resource
val source = Source.fromResource(resource)
try {
//Two lines
val lines = source.getLines().toList
val wire1String = lines.head
val wire2String = lines.tail.head
val wire1Path = wirePath(wire1String.split(",").toList).reverse
val wire2Path = wirePath(wire2String.split(",").toList).reverse
//Find all the intersections
val points: List[Point] = wire1Path.iterator.flatMap { wire1 =>
//See if it intersects with any wire, and get that point
wire2Path.map { wire2 =>
wire1.intersects2(wire2)
}.filter(_.isDefined)
.map(_.get)
}.toList
//Don't count 0,0
val intersections = points.drop(1)
val distances = intersections.map { point =>
pointPath(wire1Path, point) + pointPath(wire2Path, point)
}
val smallest = distances.foldLeft(distances.head) { (acc, v) =>
if (v < acc) {
v
} else {
acc
}
}
smallest
}
finally {
source.close()
}
}
private def pointPath(wire: List[Wire], dest: Point): Int = {
//Get all points
val path = wire.flatMap(_.points).distinct.map(Point(_)).takeWhile(_ != dest)
path.length
}
private def manhattanDistance(p: Point, q: Point): Int = {
Math.abs(p.x - q.x) + Math.abs(p.y - q.y)
}
......
......@@ -22,17 +22,29 @@ case class Wire(start: Point, end: Point) {
end.x to start.x
}
range.map(x =>
val path = range.map(x =>
(x, start.y)
).toList
)
val items = if (start.x < end.x) {
path
} else {
path.reverse
}
items.toList
} else {
val range = if (start.y < end.y) {
start.y to end.y
} else {
end.y to start.y
}
range.map(y =>
(start.x, y)).toList
val pre = range.map(y =>
(start.x, y))
val finished = if (start.y < end.y) {
pre
} else {
pre.reverse
}
finished.toList
}
}
......
......@@ -25,4 +25,18 @@ class Puzzle3Spec extends AnyFunSpec {
}
}
}
describe("Day 3 part 2") {
List(
("test1.txt", 30),
("test2.txt", 610),
("test3.txt", 410)
).foreach {
case (resource, expected) => {
it(s"for input ${resource}, expect $expected path") {
assert(p3.cheapestRoute(resource) == 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