Skip to content

Commit bf90fb1

Browse files
committed
feat(day15): part 2
1 parent 2ed5f97 commit bf90fb1

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

day15.sc

+22-6
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,37 @@ case class Point(x: Int, y: Int) {
99
copy(y = y - 1),
1010
copy(y = y + 1)
1111
)
12+
1213
def manhattanDistance(to: Point): Int = (x - to.x).abs + (y - to.y).abs
1314
}
1415

1516
val risk: Map[Point, Int] = input.zipWithIndex.flatMap { case (row, y) =>
1617
row.zipWithIndex.map { case (risk, x) => Point(x, y) -> risk.toString.toInt }
1718
}.toMap
1819

19-
val start = Point(0, 0)
20-
val end = Point(input.head.length - 1, input.length - 1)
21-
22-
class ChitonGrid() extends Grid[Point] {
20+
class ChitonGrid extends Grid[Point] {
2321
override def heuristicDistance(from: Point, to: Point) = from.manhattanDistance(to)
24-
override def getNeighbours(state: Point) = state.neighbors.filter(risk.keySet.contains)
22+
23+
override def getNeighbours(state: Point) = state.neighbors.filter(risk.keySet)
24+
2525
override def moveCost(from: Point, to: Point) = risk(to)
2626
}
2727

28-
aStarSearch(start, end, new ChitonGrid())
28+
val part1 = aStarSearch(Point(0, 0), Point(input.head.length - 1, input.length - 1), new ChitonGrid)
29+
30+
val width = input.head.length
31+
val height = input.length
32+
val riskPart2: Map[Point, Int] = (for (x <- input.head.indices;
33+
y <- input.indices;
34+
gridX <- 0 until 5;
35+
gridY <- 0 until 5) yield
36+
Point(x + gridX * width, y + gridY * width) -> ((risk(Point(x, y)) + gridX + gridY - 1) % 9 + 1)
37+
).toMap
38+
39+
class ExtendedChitonGrid extends ChitonGrid {
40+
override def getNeighbours(state: Point) = state.neighbors.filter(riskPart2.keySet)
41+
42+
override def moveCost(from: Point, to: Point) = riskPart2(to)
43+
}
2944

45+
val part2 = aStarSearch(Point(0, 0), Point(width * 5 - 1, height * 5 - 1), new ExtendedChitonGrid)

0 commit comments

Comments
 (0)