This commit is contained in:
Christian
2024-12-13 08:27:03 +01:00
parent b00c5c369e
commit c9d89a2b7e
4 changed files with 1325 additions and 1 deletions

View File

@@ -14,7 +14,8 @@ val solvers = Map[Int, Solver](
9 -> Day09, 9 -> Day09,
10 -> Day10, 10 -> Day10,
11 -> Day11, 11 -> Day11,
12 -> Day12 12 -> Day12,
13 -> Day13
) )
def runSolver(solver: Solver, input: os.Path): Unit = def runSolver(solver: Solver, input: os.Path): Unit =

View File

@@ -0,0 +1,42 @@
package dev.ctsk.aoc.days
import dev.ctsk.aoc.*
import os.ReadablePath
object Day13 extends Solver(13):
def solve(machine: Vector[Long], error: Long = 0): Option[(Long, Long)] =
val Vector(ax, ay, bx, by, px, py) = machine
val px_adj = px + error
val py_adj = py + error
val j = (py_adj * ax - px_adj * ay) / (ax * by - bx * ay)
val i = (px_adj * by - py_adj * bx) / (ax * by - bx * ay)
if i * ax + j * bx == px_adj && i * ay + j * by == py_adj
then Some((i, j))
else None
override def run(input: ReadablePath): (Timings, Solution) =
val data = os.read(input).mkString
val (pre_time, machines) = timed { data.split("\n\n").map(longs) }
def inRange(i: Long, j: Long) =
(0 to 100 contains i) && (0 to 100 contains j)
val (p1_time, p1) = timed {
machines
.map(solve(_, 0L))
.collect { case Some(i, j) if inRange(i, j) => i * 3 + j }
.sum
}
val (p2_time, p2) = timed {
machines
.map(solve(_, 10000000000000L))
.collect { case Some(i, j) => i * 3 + j }
.sum
}
(
Timings(pre_time, p1_time, p2_time),
Solution(Long.box(p1), Long.box(p2))
)

2
data/13.ans Normal file
View File

@@ -0,0 +1,2 @@
35574
80882098756071

1279
data/13.in Normal file

File diff suppressed because it is too large Load Diff