diff --git a/src/main/kotlin/no/rodland/advent_2024/Day13.kt b/src/main/kotlin/no/rodland/advent_2024/Day13.kt index f56ab90b..b4466d6f 100644 --- a/src/main/kotlin/no/rodland/advent_2024/Day13.kt +++ b/src/main/kotlin/no/rodland/advent_2024/Day13.kt @@ -1,7 +1,6 @@ package no.rodland.advent_2024 import no.rodland.advent.Day -import no.rodland.advent.Pos // template generated: 13/12/2024 // Fredrik Rødland 2024 @@ -9,27 +8,35 @@ import no.rodland.advent.Pos class Day13(val input: List) : Day> { private val machines = input.parse() + private val machinesPart2 = machines.map { it.copy(p = P(10000000000000L + it.p.x, 10000000000000L + it.p.y)) } override fun partOne(): Long { - return 2 + return machines.filter { it.ok }.sumOf { it.ca * 3 + it.cb } } override fun partTwo(): Long { - return 2 + return machinesPart2.filter { it.ok }.sumOf { it.ca * 3 + it.cb } + } + + data class Machine(val a: P, val b: P, val p: P) { + val cb = (p.y * a.x - p.x * a.y) / (a.x * b.y - a.y * b.x) + val ca = (p.x - cb * b.x) / a.x + val ok = (cb * b.x + ca * a.x == p.x) && (cb * b.y + ca * a.y == p.y) } override fun List.parse(): List { return chunked(4).map { (a, b, p) -> - val ax = a.substringAfter("X+").substringBefore(",").toInt() - val ay = a.substringAfter("Y+").toInt() - val bx = b.substringAfter("X+").substringBefore(",").toInt() - val by = b.substringAfter("Y+").toInt() - val px = p.substringAfter("X=").substringBefore(",").toInt() - val py = p.substringAfter("Y=").toInt() - Machine(Pos(ax, ay), Pos(bx, by), Pos(px, py)) + val ax = a.substringAfter("X+").substringBefore(",").toLong() + val ay = a.substringAfter("Y+").toLong() + val bx = b.substringAfter("X+").substringBefore(",").toLong() + val by = b.substringAfter("Y+").toLong() + val px = p.substringAfter("X=").substringBefore(",").toLong() + val py = p.substringAfter("Y=").toLong() + Machine(P(ax, ay), P(bx, by), P(px, py)) } } - data class Machine(val a: Pos, val b: Pos, val prize: Pos) + data class P(val x: Long, val y: Long) + override val day = "13".toInt() } diff --git a/src/test/kotlin/no/rodland/advent_2024/Day13Test.kt b/src/test/kotlin/no/rodland/advent_2024/Day13Test.kt index fcab14c6..1f6cec87 100644 --- a/src/test/kotlin/no/rodland/advent_2024/Day13Test.kt +++ b/src/test/kotlin/no/rodland/advent_2024/Day13Test.kt @@ -15,10 +15,10 @@ internal class Day13Test { private val data13 = "2024/input_13.txt".readFile() private val test13 = "2024/input_13_test.txt".readFile() - private val resultTestOne = 2L - private val resultTestTwo = 2L - private val resultOne = 2L - private val resultTwo = 2L + private val resultTestOne = 480L + private val resultTestTwo = 875318608908L + private val resultOne = 36954L + private val resultTwo = 79352015273424L val test = defaultTestSuiteParseOnInit( Day13(data13),