diff --git a/aoc/src/dev/ctsk/aoc/days/Day13.scala b/aoc/src/dev/ctsk/aoc/days/Day13.scala index 4eca64d..79acad3 100644 --- a/aoc/src/dev/ctsk/aoc/days/Day13.scala +++ b/aoc/src/dev/ctsk/aoc/days/Day13.scala @@ -5,38 +5,28 @@ 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 + val Vector(ax, ay, bx, by, px_, py_) = machine + val (px, py) = (px_ + error, py_ + error) + val j = (py * ax - px * ay) / (ax * by - bx * ay) + val i = (px * by - py * bx) / (ax * by - bx * ay) + Option.when(i * ax + j * bx == px && i * ay + j * by == py)((i, j)) 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 (pre_time, machines) = timed { os.read(input).split("\n\n").map(longs) } + val (p1_time, p1) = timed { machines - .map(solve(_, 0L)) + .map(solve(_)) .collect { case Some(i, j) if inRange(i, j) => i * 3 + j } .sum } + val ERR = 10000000000000L val (p2_time, p2) = timed { - machines - .map(solve(_, 10000000000000L)) - .collect { case Some(i, j) => i * 3 + j } - .sum + machines.map(solve(_, ERR)).collect { case Some(i, j) => i * 3 + j }.sum } - ( - Timings(pre_time, p1_time, p2_time), - Solution(Long.box(p1), Long.box(p2)) - ) + (Timings(pre_time, p1_time, p2_time), Solution(Long.box(p1), Long.box(p2)))