|
| 1 | +// Advent of Code 2024, Day 13. |
| 2 | +// By Sebastian Raaphorst, 2024. |
| 3 | + |
| 4 | +package day13 |
| 5 | + |
| 6 | +import common.aocreader.fetchAdventOfCodeInput |
| 7 | +import common.intpos2d.* |
| 8 | +import common.runner.timedFunction |
| 9 | +import java.math.BigInteger |
| 10 | + |
| 11 | +private typealias BigIntPos = Pair<BigInteger, BigInteger> |
| 12 | + |
| 13 | +data class Machine(val deltaA: IntPos2D, val deltaB: IntPos2D, val prizePos: IntPos2D) { |
| 14 | + /** |
| 15 | + * Let a be the number of times we hit button A, and b be the number of times we hit button B. |
| 16 | + * We want an integer solution to the equation: |
| 17 | + * [dAx dBx] (a) = (px) |
| 18 | + * [dAy dBy] (b) (py) |
| 19 | + */ |
| 20 | + fun calculateSolution(adjustment: BigIntPos): BigIntPos? { |
| 21 | + val determinant = deltaA.x() * deltaB.y() - deltaB.x() * deltaA.y() |
| 22 | + if (determinant == 0) return null |
| 23 | + val detBigInteger = determinant.toBigInteger() |
| 24 | + |
| 25 | + val adjPx = prizePos.x().toBigInteger() + adjustment.first |
| 26 | + val adjPy = prizePos.y().toBigInteger() + adjustment.second |
| 27 | + val abDet = BigIntPos( |
| 28 | + deltaB.y().toBigInteger() * adjPx - deltaB.x().toBigInteger() * adjPy, |
| 29 | + deltaA.x().toBigInteger() * adjPy - deltaA.y().toBigInteger() * adjPx) |
| 30 | + |
| 31 | + val detX = abDet.first % detBigInteger |
| 32 | + val detY = abDet.second % detBigInteger |
| 33 | + |
| 34 | + return if (detX == BigInteger.ZERO && detY == BigInteger.ZERO) |
| 35 | + BigIntPos(abDet.first / detBigInteger, abDet.second / detBigInteger) |
| 36 | + else null |
| 37 | + } |
| 38 | + |
| 39 | + companion object { |
| 40 | + val TokenCostA = 3.toBigInteger() |
| 41 | + val TokenCostB = BigInteger.ONE |
| 42 | + |
| 43 | + private val MachineRegex = """[XY][+=](\d+)""".toRegex() |
| 44 | + |
| 45 | + fun parse(input: String): Machine { |
| 46 | + val matches = MachineRegex.findAll(input).map { it.groupValues[1].toInt() }.toList() |
| 47 | + val (deltaA, deltaB, prizePos) = matches.chunked(2).map { IntPos2D(it[0], it[1]) } |
| 48 | + return Machine(deltaA, deltaB, prizePos) |
| 49 | + } |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +fun parse(input: String): List<Machine> = |
| 54 | + input.split("""[\r\n]{2}""".toRegex()).map { Machine.parse(it.trim()) } |
| 55 | + |
| 56 | +fun answer(input: String, adjustment: BigIntPos = BigIntPos(BigInteger.ZERO, BigInteger.ZERO)): BigInteger = |
| 57 | + parse(input) |
| 58 | + .mapNotNull { it.calculateSolution(adjustment) } |
| 59 | + .sumOf { sol -> sol.first * Machine.TokenCostA + sol.second * Machine.TokenCostB } |
| 60 | + |
| 61 | +fun answer1(input: String): BigInteger = |
| 62 | + answer(input) |
| 63 | + |
| 64 | +private val adjustment2 = BigInteger("10000000000000") |
| 65 | + |
| 66 | +fun answer2(input: String): BigInteger = |
| 67 | + answer(input, BigIntPos(adjustment2, adjustment2)) |
| 68 | + |
| 69 | +fun main() { |
| 70 | + val input = fetchAdventOfCodeInput(2024, 13) |
| 71 | + println("--- Day 13: Claw Contraption ---") |
| 72 | + timedFunction("Part 1") { answer1(input) } // 28262 |
| 73 | + timedFunction("Part 2") { answer2(input) } // 101406661266314 |
| 74 | +} |
0 commit comments