This commit is contained in:
Tyler Beckman 2024-12-12 23:24:19 -07:00
parent c2d7a4beec
commit 4909b8b21d
Signed by: Ty
GPG key ID: 2813440C772555A4
2 changed files with 114 additions and 0 deletions

56
day13/part1.kts Executable file
View file

@ -0,0 +1,56 @@
#!/usr/bin/env kotlin
import java.io.File
import kotlin.math.floor
// I LOVE MATH
typealias Matrix2 = Pair<Pair<Int, Int>, Pair<Int, Int>>
fun Matrix2.determinant(): Int = (this.first.first * this.second.second) - (this.first.second * this.second.first)
typealias Point = Pair<Int, Int>
data class Game(
val buttonA: Point,
val buttonB: Point,
val goal: Point
)
val games = File("input.txt").readLines().chunked(4).map { lines ->
Game(
buttonA = lines[0].substring(12).split(", Y+").let {
it[0].toInt() to it[1].toInt()
},
buttonB = lines[1].substring(12).split(", Y+").let {
it[0].toInt() to it[1].toInt()
},
goal = lines[2].substring(9).split(", Y=").let {
it[0].toInt() to it[1].toInt()
}
)
}
var tokens = 0
games.forEach { game ->
val aPresses = Matrix2(
game.goal.first to game.buttonB.first,
game.goal.second to game.buttonB.second
).determinant().toDouble() / Matrix2(
game.buttonA.first to game.buttonB.first,
game.buttonA.second to game.buttonB.second
).determinant()
val bPresses = Matrix2(
game.buttonA.first to game.goal.first,
game.buttonA.second to game.goal.second
).determinant().toDouble() / Matrix2(
game.buttonA.first to game.buttonB.first,
game.buttonA.second to game.buttonB.second
).determinant()
if (aPresses != floor(aPresses) || bPresses != floor(bPresses)) return@forEach
tokens += aPresses.toInt() * 3 + bPresses.toInt()
}
println("Result: $tokens")

58
day13/part2.kts Executable file
View file

@ -0,0 +1,58 @@
#!/usr/bin/env kotlin
import java.io.File
import kotlin.math.floor
// I LOVE MATH
typealias Matrix2 = Pair<Pair<Long, Long>, Pair<Long, Long>>
fun Matrix2.determinant(): Long = (this.first.first * this.second.second) - (this.first.second * this.second.first)
typealias Point = Pair<Long, Long>
data class Game(
val buttonA: Point,
val buttonB: Point,
val goal: Point
)
val games = File("input.txt").readLines().chunked(4).map { lines ->
Game(
buttonA = lines[0].substring(12).split(", Y+").let {
it[0].toLong() to it[1].toLong()
},
buttonB = lines[1].substring(12).split(", Y+").let {
it[0].toLong() to it[1].toLong()
},
goal = lines[2].substring(9).split(", Y=").let {
it[0].toLong() + 10000000000000 to it[1].toLong() + 10000000000000
}
)
}
var tokens = 0L
games.forEach { game ->
val aPressesNumer = Matrix2(
game.goal.first to game.buttonB.first,
game.goal.second to game.buttonB.second
).determinant()
val aPressesDenom = Matrix2(
game.buttonA.first to game.buttonB.first,
game.buttonA.second to game.buttonB.second
).determinant()
val bPressesNumer = Matrix2(
game.buttonA.first to game.goal.first,
game.buttonA.second to game.goal.second
).determinant()
val bPressesDenom = Matrix2(
game.buttonA.first to game.buttonB.first,
game.buttonA.second to game.buttonB.second
).determinant()
if (aPressesNumer % aPressesDenom == 0L && bPressesNumer % bPressesDenom == 0L) {
tokens += (aPressesNumer / aPressesDenom) * 3 + (bPressesNumer / bPressesDenom)
}
}
println("Result: $tokens")