Skip to content

Commit cb12df5

Browse files
committed
Day 13 part 2.
1 parent 0019ba8 commit cb12df5

File tree

2 files changed

+31
-22
lines changed

2 files changed

+31
-22
lines changed

src/main/kotlin/day13/day13.kt

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ package day13
66
import common.aocreader.fetchAdventOfCodeInput
77
import common.intpos2d.*
88
import common.runner.timedFunction
9+
import java.math.BigInteger
10+
11+
private typealias BigIntPos = Pair<BigInteger, BigInteger>
912

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

21-
val abDet = IntPos2D(
22-
deltaB.y() * prizePos.x() - deltaB.x() * prizePos.y(),
23-
deltaA.x() * prizePos.y() - deltaA.y() * prizePos.x())
24-
25-
return if (abDet % determinant != Zero2D) null else abDet / determinant
26-
}
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)
2730

31+
val detX = abDet.first % detBigInteger
32+
val detY = abDet.second % detBigInteger
2833

34+
return if (detX == BigInteger.ZERO && detY == BigInteger.ZERO)
35+
BigIntPos(abDet.first / detBigInteger, abDet.second / detBigInteger)
36+
else null
37+
}
2938

3039
companion object {
31-
val TokenCost = IntPos2D(3, 1)
40+
val TokenCostA = 3.toBigInteger()
41+
val TokenCostB = BigInteger.ONE
3242

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

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

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

51-
//fun answer2(input: String): Int =
52-
// parse(input).let(::findRegions).let(::regionCosts2)
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))
5368

5469
fun main() {
5570
val input = fetchAdventOfCodeInput(2024, 13)
5671
println("--- Day 13: Claw Contraption ---")
5772
timedFunction("Part 1") { answer1(input) } // 28262
58-
// timedFunction("Part 2") { answer2(input) } // 953738
59-
}
73+
timedFunction("Part 2") { answer2(input) } // 101406661266314
74+
}

src/test/kotlin/day13/day13.kt

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,6 @@ class Day13 {
3030

3131
@Test
3232
fun `Problem 1 example`() {
33-
assertEquals(480, answer1(input1))
33+
assertEquals(480.toBigInteger(), answer1(input1))
3434
}
35-
36-
// @Test
37-
// fun `Problem2 example`() {
38-
// assertEquals(80, answer2(input1))
39-
// assertEquals(1206, answer2(input2))
40-
// }
4135
}

0 commit comments

Comments
 (0)