From 4909b8b21da4d46e26beab15e213968075a2670e Mon Sep 17 00:00:00 2001 From: Tyler Beckman Date: Thu, 12 Dec 2024 23:24:19 -0700 Subject: [PATCH] Day 13 --- day13/part1.kts | 56 +++++++++++++++++++++++++++++++++++++++++++++++ day13/part2.kts | 58 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 114 insertions(+) create mode 100755 day13/part1.kts create mode 100755 day13/part2.kts diff --git a/day13/part1.kts b/day13/part1.kts new file mode 100755 index 0000000..ec84c97 --- /dev/null +++ b/day13/part1.kts @@ -0,0 +1,56 @@ +#!/usr/bin/env kotlin + +import java.io.File +import kotlin.math.floor + +// I LOVE MATH +typealias Matrix2 = Pair, Pair> +fun Matrix2.determinant(): Int = (this.first.first * this.second.second) - (this.first.second * this.second.first) + +typealias Point = Pair + +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") \ No newline at end of file diff --git a/day13/part2.kts b/day13/part2.kts new file mode 100755 index 0000000..5d556cf --- /dev/null +++ b/day13/part2.kts @@ -0,0 +1,58 @@ +#!/usr/bin/env kotlin + +import java.io.File +import kotlin.math.floor + +// I LOVE MATH +typealias Matrix2 = Pair, Pair> +fun Matrix2.determinant(): Long = (this.first.first * this.second.second) - (this.first.second * this.second.first) + +typealias Point = Pair + +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") \ No newline at end of file