Skip to content

Commit

Permalink
2024: day 13 part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
plagov committed Dec 20, 2024
1 parent fef8580 commit 777535d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 11 deletions.
35 changes: 24 additions & 11 deletions src/main/kotlin/io/plagov/advent/aoc2024/Day13.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,36 @@ class Day13 {
private val buttonACost = 3
private val buttonBCost = 1

fun partOne(input: String): Int {
val blocks = input.split("\n\n")
fun partOne(input: String): Long {
val totalCost = input.split("\n\n")
.map { block -> parseBlock(block) }
.map { clawMachine -> calculate(clawMachine) }
.sumOf { cost -> cost ?: 0 }

return blocks
return totalCost
}

private fun calculate(clawMachine: ClawMachine): Int? {
fun partTwo(input: String): Long {
val prizeLocationOffset = 10000000000000L
val totalCost = input.split("\n\n")
.map { block -> parseBlock(block) }
.map { clawMachine -> calculate(clawMachine, prizeLocationOffset) }
.sumOf { cost -> cost ?: 0 }

return totalCost
}

private fun calculate(clawMachine: ClawMachine, prizeLocationOffset: Long = 0): Long? {
val (buttonA, buttonB, prize) = clawMachine

val pressACunt = (prize.x * buttonB.y - prize.y * buttonB.x) / (buttonA.x * buttonB.y - buttonA.y * buttonB.x)
val pressBCount = (buttonA.x * prize.y - buttonA.y * prize.x) / (buttonA.x * buttonB.y - buttonA.y * buttonB.x)
val prizeX = prize.x + prizeLocationOffset
val prizeY = prize.y + prizeLocationOffset

val pressACunt = (prizeX * buttonB.y - prizeY * buttonB.x) / (buttonA.x * buttonB.y - buttonA.y * buttonB.x)
val pressBCount = (buttonA.x * prizeY - buttonA.y * prizeX) / (buttonA.x * buttonB.y - buttonA.y * buttonB.x)

if (pressACunt * buttonA.x + pressBCount * buttonB.x == prize.x &&
pressACunt * buttonA.y + pressBCount * buttonB.y == prize.y) {
if (pressACunt * buttonA.x + pressBCount * buttonB.x == prizeX &&
pressACunt * buttonA.y + pressBCount * buttonB.y == prizeY) {
val totalCost = pressACunt * buttonACost + pressBCount * buttonBCost

return totalCost
Expand All @@ -45,15 +58,15 @@ class Day13 {
)

val prize = PrizeLocation(
x = match.groups["pricex"]?.value?.toInt()!!,
y = match.groups["pricey"]?.value?.toInt()!!
x = match.groups["pricex"]?.value?.toLong()!!,
y = match.groups["pricey"]?.value?.toLong()!!
)

return ClawMachine(buttonA, buttonB, prize)

}

data class Button(val x: Int, val y: Int)
data class PrizeLocation(val x: Int, val y: Int)
data class PrizeLocation(val x: Long, val y: Long)
data class ClawMachine(val buttonA: Button, val buttonB: Button, val prize: PrizeLocation)
}
5 changes: 5 additions & 0 deletions src/test/kotlin/io/plagov/advent/aoc2024/Day13Test.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,9 @@ class Day13Test {
fun partOne() {
Assertions.assertEquals(28753, day13.partOne(input))
}

@Test
fun partTwo() {
Assertions.assertEquals(102718967795500, day13.partTwo(input))
}
}

0 comments on commit 777535d

Please sign in to comment.