Day 13
This commit is contained in:
@@ -14,7 +14,8 @@ val solvers = Map[Int, Solver](
|
||||
9 -> Day09,
|
||||
10 -> Day10,
|
||||
11 -> Day11,
|
||||
12 -> Day12
|
||||
12 -> Day12,
|
||||
13 -> Day13
|
||||
)
|
||||
|
||||
def runSolver(solver: Solver, input: os.Path): Unit =
|
||||
|
||||
42
aoc/src/dev/ctsk/aoc/days/Day13.scala
Normal file
42
aoc/src/dev/ctsk/aoc/days/Day13.scala
Normal 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
2
data/13.ans
Normal file
@@ -0,0 +1,2 @@
|
||||
35574
|
||||
80882098756071
|
||||
1279
data/13.in
Normal file
1279
data/13.in
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user