Day 20 faster?

This commit is contained in:
Christian
2024-12-20 16:38:26 +01:00
parent 4e20f541f9
commit cdf14fdada

View File

@@ -5,12 +5,6 @@ import dev.ctsk.aoc.*
import collection.parallel.CollectionConverters.*
object Day20 extends Solver(20):
private def manhattanBall(dist: Int, center: Point): Array[(Point, Int)] =
(for
x <- -dist to dist
y <- -(dist - x.abs) to (dist - x.abs)
yield (Point(center.x + x, center.y + y), x.abs + y.abs)).toArray
override def run(input: os.ReadablePath): (Timings, Solution) =
val grid = Grid(os.read.lines(input).map(_.toArray).toArray)
val start = grid.findFirst(_ == 'S').get
@@ -24,18 +18,18 @@ object Day20 extends Solver(20):
.concat(Seq(start))
.toArray
val distance = trace.zipWithIndex.toMap
def cheats(radius: Int): Int =
trace.zipWithIndex.par
.map((from, fromDist) => {
(for
(to, dist) <- manhattanBall(radius, from)
toDist <- distance.get(to)
cheat = fromDist - toDist
if cheat >= 100 + dist
yield ()).length
})
trace.indices.par
.map(a =>
(a + 1 until trace.length)
.count(b =>
val (from, fromDist) = (trace(a), trace.length - a)
val (to, toDist) = (trace(b), trace.length - b)
val dist = (from - to).manhattan
dist <= radius && (fromDist - toDist >= 100 + dist)
)
)
.sum
val p1 = timed { cheats(2) }