-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
107 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
kt/aoc2024-exe/src/commonBench/kotlin/com/github/ephemient/aoc2024/exe/Day13Bench.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.github.ephemient.aoc2024.exe | ||
|
||
import com.github.ephemient.aoc2024.Day13 | ||
import kotlinx.benchmark.Benchmark | ||
import kotlinx.benchmark.Blackhole | ||
import kotlinx.benchmark.Scope | ||
import kotlinx.benchmark.Setup | ||
import kotlinx.benchmark.State | ||
|
||
@State(Scope.Benchmark) | ||
class Day13Bench { | ||
private lateinit var input: String | ||
|
||
@Setup | ||
fun setup() { | ||
input = getDayInput(13) | ||
} | ||
|
||
@Benchmark | ||
fun part1() = Day13(input).part1() | ||
|
||
@Benchmark | ||
fun part2() = Day13(input).part2() | ||
|
||
@Benchmark | ||
fun solve(bh: Blackhole) { | ||
val day13 = Day13(input) | ||
bh.consume(day13.part1()) | ||
bh.consume(day13.part2()) | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day13.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.github.ephemient.aoc2024 | ||
|
||
class Day13(input: String) { | ||
private val machines = pattern.findAll(input).map { matchResult -> | ||
val (ax, ay, bx, `by`, x, y) = matchResult.destructured | ||
Machine(ax.toLong(), ay.toLong(), bx.toLong(), `by`.toLong(), x.toLong(), y.toLong()) | ||
}.toList() | ||
|
||
fun part1() = machines.sumOf { it.solve() } | ||
|
||
fun part2() = machines.sumOf { it.copy(x = it.x + 10000000000000, y = it.y + 10000000000000).solve() } | ||
|
||
private data class Machine( | ||
val ax: Long, | ||
val ay: Long, | ||
val bx: Long, | ||
val `by`: Long, | ||
val x: Long, | ||
val y: Long, | ||
) { | ||
fun solve(): Long { | ||
val aNumerator = x * `by` - y * bx | ||
val aDenominator = ax * `by` - bx * ay | ||
val bNumerator = x * ay - y * ax | ||
val bDenominator = ay * bx - `by` * ax | ||
return if (aNumerator % aDenominator == 0L && bNumerator % bDenominator == 0L) { | ||
val a = aNumerator / aDenominator | ||
val b = bNumerator / bDenominator | ||
3 * a + b | ||
} else 0 | ||
} | ||
} | ||
|
||
companion object { | ||
private val pattern = | ||
""" | ||
Button A: X\+(\d+), Y\+(\d+) | ||
Button B: X\+(\d+), Y\+(\d+) | ||
Prize: X=(\d+), Y=(\d+) | ||
""".trimIndent().toRegex() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
kt/aoc2024-lib/src/commonTest/kotlin/com/github/ephemient/aoc2024/Day13Test.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.github.ephemient.aoc2024 | ||
|
||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
|
||
class Day13Test { | ||
@Test | ||
fun part1() { | ||
assertEquals(480, Day13(example).part1()) | ||
} | ||
|
||
companion object { | ||
val example = | ||
""" | ||
|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 | ||
|""".trimMargin() | ||
} | ||
} |