-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay13ClawContraption.kt
More file actions
63 lines (52 loc) · 2.07 KB
/
Day13ClawContraption.kt
File metadata and controls
63 lines (52 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package adventofcode.year2024
import adventofcode.Puzzle
import adventofcode.PuzzleInput
import adventofcode.common.spatial.Point2d
class Day13ClawContraption(
customInput: PuzzleInput? = null,
) : Puzzle(customInput) {
private val clawMachines by lazy {
input
.split("\n\n")
.map { machine ->
val (buttonAX, buttonAY) = INPUT_REGEX.find(machine.lines().first())!!.destructured
val (buttonBX, buttonBY) = INPUT_REGEX.find(machine.lines().drop(1).first())!!.destructured
val (prizeX, prizeY) = INPUT_REGEX.find(machine.lines().last())!!.destructured
ClawMachine(
Point2d(buttonAX.toInt(), buttonAY.toInt()),
Point2d(buttonBX.toInt(), buttonBY.toInt()),
Point2d(prizeX.toInt(), prizeY.toInt()),
)
}
}
override fun partOne() = clawMachines.sumOf { clawMachine -> clawMachine.countTokens() }
override fun partTwo() =
clawMachines
.map { (buttonA, buttonB, prize) -> ClawMachine(buttonA, buttonB, prize + PART_TWO_OFFSET) }
.sumOf { clawMachine -> clawMachine.countTokens() }
companion object {
private val INPUT_REGEX = """X[+|=](\d+), Y[+|=](\d+)""".toRegex()
private const val COST_A = 3
private const val COST_B = 1
private const val PART_TWO_OFFSET = 10000000000000
private data class ClawMachine(
val buttonA: Point2d,
val buttonB: Point2d,
val prize: Point2d,
) {
fun countTokens(): Long {
val (ax, ay) = buttonA
val (bx, by) = buttonB
val (px, py) = prize
val dividend = ax * by - bx * ay
val a = px * by - bx * py
val b = ax * py - px * ay
return if (setOf(a, b).all { it % dividend == 0L }) {
a / dividend * COST_A + b / dividend * COST_B
} else {
0
}
}
}
}
}