Codegolf it a bit
This commit is contained in:
@@ -5,38 +5,28 @@ import os.ReadablePath
|
|||||||
|
|
||||||
object Day13 extends Solver(13):
|
object Day13 extends Solver(13):
|
||||||
def solve(machine: Vector[Long], error: Long = 0): Option[(Long, Long)] =
|
def solve(machine: Vector[Long], error: Long = 0): Option[(Long, Long)] =
|
||||||
val Vector(ax, ay, bx, by, px, py) = machine
|
val Vector(ax, ay, bx, by, px_, py_) = machine
|
||||||
val px_adj = px + error
|
val (px, py) = (px_ + error, py_ + error)
|
||||||
val py_adj = py + error
|
val j = (py * ax - px * ay) / (ax * by - bx * ay)
|
||||||
val j = (py_adj * ax - px_adj * ay) / (ax * by - bx * ay)
|
val i = (px * by - py * bx) / (ax * by - bx * ay)
|
||||||
val i = (px_adj * by - py_adj * bx) / (ax * by - bx * ay)
|
Option.when(i * ax + j * bx == px && i * ay + j * by == py)((i, j))
|
||||||
|
|
||||||
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) =
|
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) =
|
def inRange(i: Long, j: Long) =
|
||||||
(0 to 100 contains i) && (0 to 100 contains j)
|
(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 {
|
val (p1_time, p1) = timed {
|
||||||
machines
|
machines
|
||||||
.map(solve(_, 0L))
|
.map(solve(_))
|
||||||
.collect { case Some(i, j) if inRange(i, j) => i * 3 + j }
|
.collect { case Some(i, j) if inRange(i, j) => i * 3 + j }
|
||||||
.sum
|
.sum
|
||||||
}
|
}
|
||||||
|
|
||||||
|
val ERR = 10000000000000L
|
||||||
val (p2_time, p2) = timed {
|
val (p2_time, p2) = timed {
|
||||||
machines
|
machines.map(solve(_, ERR)).collect { case Some(i, j) => i * 3 + j }.sum
|
||||||
.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)))
|
||||||
Timings(pre_time, p1_time, p2_time),
|
|
||||||
Solution(Long.box(p1), Long.box(p2))
|
|
||||||
)
|
|
||||||
|
|||||||
Reference in New Issue
Block a user