Day 13 part 1
This commit is contained in:
parent
b8411307bb
commit
37cd83d3ae
3 changed files with 1328 additions and 0 deletions
34
2024/src/main/kotlin/day13.kt
Normal file
34
2024/src/main/kotlin/day13.kt
Normal file
|
@ -0,0 +1,34 @@
|
||||||
|
val regex = "Button A: X\\+(\\d+), Y\\+(\\d+)\nButton B: X\\+(\\d+), Y\\+(\\d+)\nPrize: X=(\\d+), Y=(\\d+)".toRegex()
|
||||||
|
|
||||||
|
data class Machine(
|
||||||
|
val buttonA: Grid.Coordinate,
|
||||||
|
val buttonB: Grid.Coordinate,
|
||||||
|
val price: Grid.Coordinate
|
||||||
|
) {
|
||||||
|
|
||||||
|
fun solveA(b: Int) = (price.x - b * buttonB.x) / buttonA.x
|
||||||
|
fun solveB() = (price.y * buttonA.x - price.x * buttonA.y) / (buttonA.x * buttonB.y - buttonA.y * buttonB.x)
|
||||||
|
|
||||||
|
fun solve(): Pair<Int, Int>? {
|
||||||
|
val b = solveB()
|
||||||
|
val a = solveA(b)
|
||||||
|
|
||||||
|
return Pair(a, b).takeIf { (a * buttonA.x + b * buttonB.x) == price.x && (a * buttonA.y + b * buttonB.y) == price.y }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun main() {
|
||||||
|
val part1 = regex.findAll(readInputString("day13.txt"))
|
||||||
|
.map {
|
||||||
|
Machine(
|
||||||
|
buttonA = Grid.Coordinate(it.groupValues[1].toInt(), it.groupValues[2].toInt()),
|
||||||
|
buttonB = Grid.Coordinate(it.groupValues[3].toInt(), it.groupValues[4].toInt()),
|
||||||
|
price = Grid.Coordinate(it.groupValues[5].toInt(), it.groupValues[6].toInt()),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
.mapNotNull { it.solve() }
|
||||||
|
.sumOf { (a, b) -> 3 * a + b }
|
||||||
|
|
||||||
|
println("Part 1: $part1")
|
||||||
|
|
||||||
|
}
|
15
2024/src/main/resources/day13-sample.txt
Normal file
15
2024/src/main/resources/day13-sample.txt
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
Button A: X+94, Y+34
|
||||||
|
Button B: X+22, Y+67
|
||||||
|
Prize: X=8400, Y=5400
|
||||||
|
|
||||||
|
Button A: X+26, Y+66
|
||||||
|
Button B: X+67, Y+21
|
||||||
|
Prize: X=12748, Y=12176
|
||||||
|
|
||||||
|
Button A: X+17, Y+86
|
||||||
|
Button B: X+84, Y+37
|
||||||
|
Prize: X=7870, Y=6450
|
||||||
|
|
||||||
|
Button A: X+69, Y+23
|
||||||
|
Button B: X+27, Y+71
|
||||||
|
Prize: X=18641, Y=10279
|
1279
2024/src/main/resources/day13.txt
Normal file
1279
2024/src/main/resources/day13.txt
Normal file
File diff suppressed because it is too large
Load diff
Loading…
Add table
Reference in a new issue