-
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.
Merge pull request #138 from ephemient/kt/day21
- Loading branch information
Showing
8 changed files
with
230 additions
and
2 deletions.
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/Day21Bench.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.Day21 | ||
import kotlinx.benchmark.Benchmark | ||
import kotlinx.benchmark.Blackhole | ||
import kotlinx.benchmark.Scope | ||
import kotlinx.benchmark.Setup | ||
import kotlinx.benchmark.State | ||
|
||
@State(Scope.Benchmark) | ||
class Day21Bench { | ||
private lateinit var input: String | ||
|
||
@Setup | ||
fun setup() { | ||
input = getDayInput(21) | ||
} | ||
|
||
@Benchmark | ||
fun part1() = Day21(input).part1() | ||
|
||
@Benchmark | ||
fun part2() = Day21(input).part2() | ||
|
||
@Benchmark | ||
fun solve(bh: Blackhole) { | ||
val day21 = Day21(input) | ||
bh.consume(day21.part1()) | ||
bh.consume(day21.part2()) | ||
} | ||
} |
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
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
22 changes: 22 additions & 0 deletions
22
kt/aoc2024-lib/src/commonTest/kotlin/com/github/ephemient/aoc2024/Day21Test.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,22 @@ | ||
package com.github.ephemient.aoc2024 | ||
|
||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
|
||
class Day21Test { | ||
@Test | ||
fun part1() { | ||
assertEquals(126384, Day21(example).part1()) | ||
} | ||
|
||
companion object { | ||
private val example = | ||
""" | ||
|029A | ||
|980A | ||
|179A | ||
|456A | ||
|379A | ||
|""".trimMargin() | ||
} | ||
} |
141 changes: 141 additions & 0 deletions
141
kt/aoc2024-lib/src/jvmCodegen/kotlin/com/github/ephemient/aoc2024/codegen/Day21.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,141 @@ | ||
package com.github.ephemient.aoc2024.codegen | ||
|
||
import com.squareup.kotlinpoet.ClassName | ||
import com.squareup.kotlinpoet.CodeBlock | ||
import com.squareup.kotlinpoet.FileSpec | ||
import com.squareup.kotlinpoet.FunSpec | ||
import com.squareup.kotlinpoet.KModifier | ||
import com.squareup.kotlinpoet.LIST | ||
import com.squareup.kotlinpoet.LONG | ||
import com.squareup.kotlinpoet.LONG_ARRAY | ||
import com.squareup.kotlinpoet.MemberName | ||
import com.squareup.kotlinpoet.MemberName.Companion.member | ||
import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.parameterizedBy | ||
import com.squareup.kotlinpoet.PropertySpec | ||
import com.squareup.kotlinpoet.STRING | ||
import com.squareup.kotlinpoet.TypeSpec | ||
import com.squareup.kotlinpoet.joinToCode | ||
import java.nio.file.Path | ||
import kotlin.math.abs | ||
|
||
fun day21(outputDir: Path, className: ClassName = ClassName("com.github.ephemient.aoc2024", "Day21")) { | ||
FileSpec.builder(className) | ||
.addType( | ||
TypeSpec.classBuilder(className) | ||
.primaryConstructor( | ||
FunSpec.constructorBuilder() | ||
.addParameter("input", STRING) | ||
.build() | ||
) | ||
.addProperty( | ||
PropertySpec.builder("lines", LIST.parameterizedBy(STRING)) | ||
.addModifiers(KModifier.PRIVATE) | ||
.initializer("%N.%M()", "input", MemberName("kotlin.text", "lines")) | ||
.build() | ||
) | ||
.addFunctions( | ||
arrayOf(IndexedValue(2, "part1"), IndexedValue(25, "part2")).map { (depth, name) -> | ||
FunSpec.builder(name) | ||
.returns(LONG) | ||
.beginControlFlow("return %N.%M", "lines", MemberName("kotlin.collections", "sumOf")) | ||
.addStatement("var %N = 10", "i") | ||
.addStatement("var %N = 0", "numeric") | ||
.addStatement("var %N = 0L", "length") | ||
.beginControlFlow("for (%N in %N)", "char", "it") | ||
.beginControlFlow("val %N = when (%N)", "j", "char") | ||
.beginControlFlow("in '0'..'9' ->") | ||
.addStatement("%1N = 10 * %1N + (%2N - '0')", "numeric", "char") | ||
.addStatement("%N - '0'", "char") | ||
.endControlFlow() | ||
.addStatement("else -> 10") | ||
.endControlFlow() | ||
.addStatement( | ||
"%N += %M[11 * %N + %N]", | ||
"length", | ||
className.nestedClass("Companion").member(name), | ||
"i", | ||
"j", | ||
) | ||
.addStatement("%N = %N", "i", "j") | ||
.endControlFlow() | ||
.addStatement("%N * %N", "numeric", "length") | ||
.endControlFlow() | ||
.build() | ||
} | ||
) | ||
.addType( | ||
TypeSpec.companionObjectBuilder() | ||
.addProperty( | ||
PropertySpec.builder("part1", LONG_ARRAY) | ||
.addModifiers(KModifier.PRIVATE) | ||
.initializer( | ||
"%M(%L)", | ||
MemberName("kotlin", "longArrayOf"), | ||
lut(2).joinToCode(",♢") { CodeBlock.of("%L", it) }, | ||
) | ||
.build() | ||
) | ||
.addProperty( | ||
PropertySpec.builder("part2", LONG_ARRAY) | ||
.addModifiers(KModifier.PRIVATE) | ||
.initializer( | ||
"%M(%L)", | ||
MemberName("kotlin", "longArrayOf"), | ||
lut(25).joinToCode(",♢") { CodeBlock.of("%L", it) }, | ||
) | ||
.build() | ||
) | ||
.build() | ||
) | ||
.build() | ||
) | ||
.build() | ||
.writeTo(outputDir) | ||
} | ||
|
||
private val keypad = intArrayOf( | ||
3 * 1 + 1, // 0 | ||
3 * 2 + 0, // 1 | ||
3 * 2 + 1, // 2 | ||
3 * 2 + 2, // 3 | ||
3 * 3 + 0, // 4 | ||
3 * 3 + 1, // 5 | ||
3 * 3 + 2, // 6 | ||
3 * 4 + 0, // 7 | ||
3 * 4 + 1, // 8 | ||
3 * 4 + 2, // 9 | ||
3 * 1 + 2, // A | ||
) | ||
private const val BLANK = 3 * 1 + 0 | ||
private const val UP = 3 * 1 + 1 | ||
private const val A = 3 * 1 + 2 | ||
private const val LEFT = 3 * 0 + 0 | ||
private const val DOWN = 3 * 0 + 1 | ||
private const val RIGHT = 3 * 0 + 2 | ||
|
||
private fun lut(depth: Int): List<Long> { | ||
var lut = Array(15 * 15) { | ||
val p1 = it / 15 | ||
val p2 = it % 15 | ||
if (p1 == BLANK || p2 == BLANK) null else abs(p2 / 3 - p1 / 3) + abs(p2 % 3 - p1 % 3) + 1L | ||
} | ||
fun best(p1: Int, p2: Int, pos: Int): Long? { | ||
if (p1 == BLANK || p2 == BLANK) return null | ||
if (p1 == p2) return lut[15 * pos + A] | ||
val v = when { | ||
p1 / 3 < p2 / 3 -> lut[15 * pos + UP]?.let { best(p1 + 3, p2, UP)?.let(it::plus) } | ||
p1 / 3 > p2 / 3 -> lut[15 * pos + DOWN]?.let { best(p1 - 3, p2, DOWN)?.let(it::plus) } | ||
else -> null | ||
} | ||
val h = when { | ||
p1 % 3 < p2 % 3 -> lut[15 * pos + RIGHT]?.let { best(p1 + 1, p2, RIGHT)?.let(it::plus) } | ||
p1 % 3 > p2 % 3 -> lut[15 * pos + LEFT]?.let { best(p1 - 1, p2, LEFT)?.let(it::plus) } | ||
else -> null | ||
} | ||
return if (v != null && h != null) minOf(v, h) else v ?: h | ||
} | ||
repeat(depth) { lut = Array(15 * 15) { best(it / 15, it % 15, A) } } | ||
return List(11 * 11) { lut[15 * keypad[it / 11] + keypad[it % 11]]!! }.also { | ||
println(it.chunked(11).joinToString(",\n", "Day21[$depth] = [\n", "]") { it.joinToString() }) | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
kt/aoc2024-lib/src/jvmCodegen/kotlin/com/github/ephemient/aoc2024/codegen/Main.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,16 @@ | ||
@file:JvmName("Main") | ||
|
||
package com.github.ephemient.aoc2024.codegen | ||
|
||
import java.nio.file.Files | ||
import kotlin.io.path.ExperimentalPathApi | ||
import kotlin.io.path.Path | ||
import kotlin.io.path.deleteRecursively | ||
|
||
@OptIn(ExperimentalPathApi::class) | ||
fun main(argv: Array<String>) { | ||
val outputDir = Path(argv.single()) | ||
outputDir.deleteRecursively() | ||
Files.createDirectory(outputDir) | ||
day21(outputDir) | ||
} |
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