Cheat (by doing all the work in preprocessing)

This commit is contained in:
Christian
2024-12-10 07:32:46 +01:00
parent 9e2aca403f
commit ca5831b022

View File

@@ -4,41 +4,29 @@ import dev.ctsk.aoc._
import dev.ctsk.aoc.Direction._ import dev.ctsk.aoc.Direction._
object Day10 extends Solver(10): object Day10 extends Solver(10):
type Input = (Grid[Char], Seq[Point]) type Input = Vector[(Point, Point)]
def pre(input: os.ReadablePath): Input = def pre(input: os.ReadablePath): Input =
val grid = Grid(os.read.lines(input).map(_.toArray).toArray) val grid = Grid(os.read.lines(input).map(_.toArray).toArray)
val zero = grid.find(_ == '0').toSeq
(grid, zero)
def step( def step(seq: Input, height: Int): Input =
grid: Grid[Char], (for
start: Seq[(Point, Point)], (p, origin) <- seq
height: Int a <- Seq(Up, Down, Left, Right)
): Seq[(Point, Point)] = if grid(a(p)).exists(_.asDigit == height)
(for yield (a(p), origin)).toVector
(p, origin) <- start
a <- Seq(Up, Down, Left, Right)
if grid(a(p)).exists(_.asDigit == height)
yield (a(p), origin))
def part1(input: Input): Int = val zero = grid.find(_ == '0').toVector
val (grid, zero) = input (1 to 9).foldLeft(zero.zip(zero)) { case (acc, h) => step(acc, h) }
(1 to 9)
.foldLeft(zero.zip(zero)) { case (acc, h) => step(grid, acc, h) }
.distinct
.size
def part2(input: Input): Int = def part1(input: Input): Int = input.distinct.size
val (grid, zero) = input
(1 to 9) def part2(input: Input): Int = input.size
.foldLeft(zero.zip(zero)) { case (acc, h) => step(grid, acc, h) }
.size
def run(input: os.ReadablePath): (Timings, Solution) = def run(input: os.ReadablePath): (Timings, Solution) =
val (pre_time, (grid, zero)) = timed { pre(input) } val (pre_time, pre_input) = timed { pre(input) }
val (p1_time, p1_solution) = timed { part1((grid, zero)) } val (p1_time, p1_solution) = timed { part1(pre_input) }
val (p2_time, p2_solution) = timed { part2((grid, zero)) } val (p2_time, p2_solution) = timed { part2(pre_input) }
( (
Timings(pre_time, p1_time, p2_time), Timings(pre_time, p1_time, p2_time),