Day 20 faster?
This commit is contained in:
@@ -5,12 +5,6 @@ import dev.ctsk.aoc.*
|
|||||||
import collection.parallel.CollectionConverters.*
|
import collection.parallel.CollectionConverters.*
|
||||||
|
|
||||||
object Day20 extends Solver(20):
|
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) =
|
override def run(input: os.ReadablePath): (Timings, Solution) =
|
||||||
val grid = Grid(os.read.lines(input).map(_.toArray).toArray)
|
val grid = Grid(os.read.lines(input).map(_.toArray).toArray)
|
||||||
val start = grid.findFirst(_ == 'S').get
|
val start = grid.findFirst(_ == 'S').get
|
||||||
@@ -24,20 +18,20 @@ object Day20 extends Solver(20):
|
|||||||
.concat(Seq(start))
|
.concat(Seq(start))
|
||||||
.toArray
|
.toArray
|
||||||
|
|
||||||
val distance = trace.zipWithIndex.toMap
|
|
||||||
|
|
||||||
def cheats(radius: Int): Int =
|
def cheats(radius: Int): Int =
|
||||||
trace.zipWithIndex.par
|
trace.indices.par
|
||||||
.map((from, fromDist) => {
|
.map(a =>
|
||||||
(for
|
(a + 1 until trace.length)
|
||||||
(to, dist) <- manhattanBall(radius, from)
|
.count(b =>
|
||||||
toDist <- distance.get(to)
|
val (from, fromDist) = (trace(a), trace.length - a)
|
||||||
cheat = fromDist - toDist
|
val (to, toDist) = (trace(b), trace.length - b)
|
||||||
if cheat >= 100 + dist
|
val dist = (from - to).manhattan
|
||||||
yield ()).length
|
|
||||||
})
|
|
||||||
.sum
|
|
||||||
|
|
||||||
|
dist <= radius && (fromDist - toDist >= 100 + dist)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
.sum
|
||||||
|
|
||||||
val p1 = timed { cheats(2) }
|
val p1 = timed { cheats(2) }
|
||||||
val p2 = timed { cheats(20) }
|
val p2 = timed { cheats(20) }
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user