Skip to content

Commit

Permalink
Day 13 part 2.
Browse files Browse the repository at this point in the history
  • Loading branch information
sraaphorst committed Dec 13, 2024
1 parent 0019ba8 commit cb12df5
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 22 deletions.
45 changes: 30 additions & 15 deletions src/main/kotlin/day13/day13.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ package day13
import common.aocreader.fetchAdventOfCodeInput
import common.intpos2d.*
import common.runner.timedFunction
import java.math.BigInteger

private typealias BigIntPos = Pair<BigInteger, BigInteger>

data class Machine(val deltaA: IntPos2D, val deltaB: IntPos2D, val prizePos: IntPos2D) {
/**
Expand All @@ -14,21 +17,28 @@ data class Machine(val deltaA: IntPos2D, val deltaB: IntPos2D, val prizePos: Int
* [dAx dBx] (a) = (px)
* [dAy dBy] (b) (py)
*/
fun calculateSolution(): IntPos2D? {
fun calculateSolution(adjustment: BigIntPos): BigIntPos? {
val determinant = deltaA.x() * deltaB.y() - deltaB.x() * deltaA.y()
if (determinant == 0) return null
val detBigInteger = determinant.toBigInteger()

val abDet = IntPos2D(
deltaB.y() * prizePos.x() - deltaB.x() * prizePos.y(),
deltaA.x() * prizePos.y() - deltaA.y() * prizePos.x())

return if (abDet % determinant != Zero2D) null else abDet / determinant
}
val adjPx = prizePos.x().toBigInteger() + adjustment.first
val adjPy = prizePos.y().toBigInteger() + adjustment.second
val abDet = BigIntPos(
deltaB.y().toBigInteger() * adjPx - deltaB.x().toBigInteger() * adjPy,
deltaA.x().toBigInteger() * adjPy - deltaA.y().toBigInteger() * adjPx)

val detX = abDet.first % detBigInteger
val detY = abDet.second % detBigInteger

return if (detX == BigInteger.ZERO && detY == BigInteger.ZERO)
BigIntPos(abDet.first / detBigInteger, abDet.second / detBigInteger)
else null
}

companion object {
val TokenCost = IntPos2D(3, 1)
val TokenCostA = 3.toBigInteger()
val TokenCostB = BigInteger.ONE

private val MachineRegex = """[XY][+=](\d+)""".toRegex()

Expand All @@ -43,17 +53,22 @@ data class Machine(val deltaA: IntPos2D, val deltaB: IntPos2D, val prizePos: Int
fun parse(input: String): List<Machine> =
input.split("""[\r\n]{2}""".toRegex()).map { Machine.parse(it.trim()) }

fun answer1(input: String): Int =
fun answer(input: String, adjustment: BigIntPos = BigIntPos(BigInteger.ZERO, BigInteger.ZERO)): BigInteger =
parse(input)
.mapNotNull(Machine::calculateSolution)
.sumOf { it dot Machine.TokenCost }
.mapNotNull { it.calculateSolution(adjustment) }
.sumOf { sol -> sol.first * Machine.TokenCostA + sol.second * Machine.TokenCostB }

//fun answer2(input: String): Int =
// parse(input).let(::findRegions).let(::regionCosts2)
fun answer1(input: String): BigInteger =
answer(input)

private val adjustment2 = BigInteger("10000000000000")

fun answer2(input: String): BigInteger =
answer(input, BigIntPos(adjustment2, adjustment2))

fun main() {
val input = fetchAdventOfCodeInput(2024, 13)
println("--- Day 13: Claw Contraption ---")
timedFunction("Part 1") { answer1(input) } // 28262
// timedFunction("Part 2") { answer2(input) } // 953738
}
timedFunction("Part 2") { answer2(input) } // 101406661266314
}
8 changes: 1 addition & 7 deletions src/test/kotlin/day13/day13.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,6 @@ class Day13 {

@Test
fun `Problem 1 example`() {
assertEquals(480, answer1(input1))
assertEquals(480.toBigInteger(), answer1(input1))
}

// @Test
// fun `Problem2 example`() {
// assertEquals(80, answer2(input1))
// assertEquals(1206, answer2(input2))
// }
}

0 comments on commit cb12df5

Please sign in to comment.