Day 13
This commit is contained in:
parent
5c0e8a2ce6
commit
ddaa007102
2 changed files with 1325 additions and 0 deletions
1279
input/day13
Normal file
1279
input/day13
Normal file
File diff suppressed because it is too large
Load diff
46
src/day13.scala
Normal file
46
src/day13.scala
Normal file
|
|
@ -0,0 +1,46 @@
|
||||||
|
package aoc
|
||||||
|
package day13
|
||||||
|
|
||||||
|
val dayNumber = "13"
|
||||||
|
|
||||||
|
@main def part1: Unit =
|
||||||
|
println(part1(loadInput(dayNumber)))
|
||||||
|
|
||||||
|
@main def part2: Unit =
|
||||||
|
println(part2(loadInput(dayNumber)))
|
||||||
|
|
||||||
|
def part1(input: String): String =
|
||||||
|
val machines = parseInput(input)
|
||||||
|
machines.flatMap(_.tokenCost).sum.toString
|
||||||
|
|
||||||
|
def part2(input: String): String =
|
||||||
|
val correction = 10000000000000L
|
||||||
|
val machines = parseInput(input)
|
||||||
|
.map(m => m.copy(x = m.x + correction, y = m.y + correction))
|
||||||
|
machines.flatMap(_.tokenCost).sum.toString
|
||||||
|
|
||||||
|
case class Machine(ax: Long, ay: Long, bx: Long, by: Long, x: Long, y: Long):
|
||||||
|
def tokenCost: Option[Long] =
|
||||||
|
// Calculations come from solving the given equations
|
||||||
|
// a * ax + b * bx == x
|
||||||
|
// a * ay + b * by == y
|
||||||
|
val b = (x * ay - y * ax) / (bx * ay - by * ax).toDouble
|
||||||
|
val a = (x - b * bx) / ax.toDouble
|
||||||
|
Option.when(a.isWhole && b.isWhole)(a.toLong * 3 + b.toLong)
|
||||||
|
|
||||||
|
def parseInput(input: String): List[Machine] =
|
||||||
|
object l:
|
||||||
|
def unapply(s: String): Option[Long] = s.toLongOption
|
||||||
|
input
|
||||||
|
.split("\n\n")
|
||||||
|
.map(_.split("\n").toList)
|
||||||
|
.flatMap {
|
||||||
|
case List(
|
||||||
|
s"Button A: X+${l(ax)}, Y+${l(ay)}",
|
||||||
|
s"Button B: X+${l(bx)}, Y+${l(by)}",
|
||||||
|
s"Prize: X=${l(x)}, Y=${l(y)}"
|
||||||
|
) =>
|
||||||
|
Some(Machine(ax, ay, bx, by, x, y))
|
||||||
|
case _ => None
|
||||||
|
}
|
||||||
|
.toList
|
||||||
Loading…
Add table
Add a link
Reference in a new issue