-
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
30 changed files
with
767 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# generated by Gradle Wrapper | ||
kt/gradlew linguist-generated | ||
kt/gradlew.bat linguist-generated |
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,39 @@ | ||
name: Kotlin CI | ||
|
||
on: | ||
push: | ||
branches: [ main ] | ||
paths: [ kt/** ] | ||
pull_request: | ||
branches: [ main ] | ||
paths: [ kt/** ] | ||
|
||
workflow_dispatch: | ||
|
||
jobs: | ||
get-inputs: | ||
uses: ephemient/aoc2024/.github/workflows/get-inputs.yml@main | ||
secrets: | ||
SESSION: ${{ secrets.SESSION }} | ||
|
||
build: | ||
needs: [ get-inputs ] | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/checkout@v4 | ||
- uses: actions/download-artifact@v4 | ||
with: | ||
name: inputs | ||
path: inputs | ||
- uses: actions/setup-java@v4 | ||
with: | ||
distribution: temurin | ||
java-version: 21 | ||
- uses: gradle/actions/setup-gradle@v4 | ||
- run: ./gradlew check | ||
working-directory: kt | ||
- run: ./gradlew :aoc2024-exe:runJvm | ||
working-directory: kt | ||
env: | ||
AOC2024_DATADIR: ${{ github.workspace }}/inputs |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export AOC2024_DATADIR=$(realpath ..) |
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,6 @@ | ||
.gradle/ | ||
.kotlin/ | ||
.idea/ | ||
build/ | ||
local.properties | ||
*~ |
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 @@ | ||
# [Advent of Code 2024](https://adventofcode.com/2024) | ||
### my answers in [Kotlin](https://www.kotlinlang.org/) ![Kotlin CI](https://github.com/ephemient/aoc2024/workflows/Kotlin%20CI/badge.svg) | ||
|
||
This project builds with [Gradle](https://gradle.org/). | ||
|
||
Run the test suite: | ||
|
||
```sh | ||
./gradlew :aoc2024-lib:allTests | ||
``` | ||
|
||
Print solutions for the inputs provided in local data files: | ||
|
||
```sh | ||
./gradlew :aoc2024-exe:jvmRun | ||
``` | ||
|
||
Run all checks, including [Detekt](https://detekt.github.io/) static code analysis: | ||
|
||
```sh | ||
./gradlew check | ||
``` |
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,27 @@ | ||
import io.gitlab.arturbosch.detekt.Detekt | ||
|
||
plugins { | ||
alias(libs.plugins.kotlin.multiplatform) | ||
alias(libs.plugins.detekt) | ||
} | ||
|
||
kotlin { | ||
jvm { | ||
mainRun { | ||
mainClass = "com.github.ephemient.aoc2024.exe.Main" | ||
} | ||
} | ||
|
||
sourceSets { | ||
commonMain { | ||
dependencies { | ||
implementation(projects.aoc2024Lib) | ||
implementation(libs.kotlinx.coroutines) | ||
} | ||
} | ||
} | ||
} | ||
|
||
dependencies { | ||
detektPlugins(libs.bundles.detekt.plugins) | ||
} |
12 changes: 12 additions & 0 deletions
12
kt/aoc2024-exe/src/commonMain/kotlin/com/github/ephemient/aoc2024/exe/CommonMain.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,12 @@ | ||
package com.github.ephemient.aoc2024.exe | ||
|
||
import com.github.ephemient.aoc2024.days | ||
|
||
internal suspend fun mainImpl(args: Array<out String>) { | ||
for (day in days) { | ||
if ((args.isNotEmpty() || day.skipByDefault) && day.name !in args) continue | ||
println("Day ${day.name}") | ||
for (part in day.solver(getDayInput(day.day))) println(part()) | ||
println() | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
kt/aoc2024-exe/src/commonMain/kotlin/com/github/ephemient/aoc2024/exe/IO.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,3 @@ | ||
package com.github.ephemient.aoc2024.exe | ||
|
||
internal expect fun getDayInput(day: Int): String |
6 changes: 6 additions & 0 deletions
6
kt/aoc2024-exe/src/jvmMain/kotlin/com/github/ephemient/aoc2024/exe/IO.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,6 @@ | ||
package com.github.ephemient.aoc2024.exe | ||
|
||
import java.io.File | ||
|
||
internal actual fun getDayInput(day: Int): String = | ||
File(System.getenv("AOC2024_DATADIR")?.ifEmpty { null } ?: ".", "day$day.txt").readText() |
11 changes: 11 additions & 0 deletions
11
kt/aoc2024-exe/src/jvmMain/kotlin/com/github/ephemient/aoc2024/exe/JvmMain.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,11 @@ | ||
@file:JvmName("Main") | ||
|
||
package com.github.ephemient.aoc2024.exe | ||
|
||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.withContext | ||
|
||
@Suppress("InjectDispatcher") | ||
suspend fun main(vararg args: String): Unit = withContext(Dispatchers.Default) { | ||
mainImpl(args) | ||
} |
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,39 @@ | ||
plugins { | ||
alias(libs.plugins.kotlin.multiplatform) | ||
alias(libs.plugins.detekt) | ||
} | ||
|
||
kotlin { | ||
jvm() | ||
|
||
sourceSets { | ||
commonMain { | ||
dependencies { | ||
implementation(libs.kotlinx.coroutines) | ||
} | ||
} | ||
|
||
commonTest { | ||
dependencies { | ||
implementation(kotlin("test")) | ||
implementation(libs.kotlinx.coroutines.test) | ||
} | ||
} | ||
|
||
jvmTest { | ||
dependencies { | ||
implementation(kotlin("test-junit5")) | ||
implementation(libs.junit.jupiter.api) | ||
runtimeOnly(libs.junit.jupiter.engine) | ||
} | ||
} | ||
} | ||
} | ||
|
||
dependencies { | ||
detektPlugins(libs.bundles.detekt.plugins) | ||
} | ||
|
||
tasks.withType<Test>().configureEach { | ||
useJUnitPlatform() | ||
} |
45 changes: 45 additions & 0 deletions
45
kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/CommonPriorityQueue.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,45 @@ | ||
package com.github.ephemient.aoc2024 | ||
|
||
internal class CommonPriorityQueue<E : Any>(private val comparator: Comparator<in E>) : PriorityQueue<E> { | ||
private val storage = mutableListOf<E>() | ||
|
||
override fun isEmpty(): Boolean = storage.isEmpty() | ||
|
||
override fun add(element: E): Boolean { | ||
storage.add(element) | ||
var i = storage.lastIndex | ||
while (i > 0) { | ||
val j = (i - 1) / 2 | ||
val a = storage[j] | ||
val b = storage[i] | ||
if (comparator.compare(a, b) <= 0) break | ||
storage[i] = a | ||
storage[j] = b | ||
i = j | ||
} | ||
return true | ||
} | ||
|
||
@Suppress("NestedBlockDepth") | ||
@Throws(NoSuchElementException::class) | ||
override fun remove(): E { | ||
val first = storage.first() | ||
val last = storage.removeLast() | ||
if (storage.isNotEmpty()) { | ||
storage[0] = last | ||
var i = 0 | ||
while (2 * i + 2 < storage.size) { | ||
val j = if (comparator.compare(storage[2 * i + 1], storage[2 * i + 2]) < 0) 2 * i + 1 else 2 * i + 2 | ||
if (comparator.compare(storage[i], storage[j]) <= 0) break | ||
storage[i] = storage[j].also { storage[j] = storage[i] } | ||
i = j | ||
} | ||
if (2 * i + 1 == storage.lastIndex && comparator.compare(storage[i], storage.last()) > 0) { | ||
storage[i] = storage.last().also { storage[storage.lastIndex] = storage[i] } | ||
} | ||
} | ||
return first | ||
} | ||
|
||
override fun iterator(): Iterator<E> = storage.iterator() | ||
} |
33 changes: 33 additions & 0 deletions
33
kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day1.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,33 @@ | ||
package com.github.ephemient.aoc2024 | ||
|
||
import kotlin.math.abs | ||
|
||
class Day1(input: String) { | ||
private val left: List<Int> | ||
private val right: List<Int> | ||
|
||
init { | ||
val left = mutableListOf<Int>() | ||
val right = mutableListOf<Int>() | ||
for (line in input.lineSequence()) { | ||
val parts = line.trim().split(splitter, limit = 2) | ||
val x = parts.getOrNull(0)?.toIntOrNull() ?: continue | ||
val y = parts.getOrNull(1)?.toIntOrNull() ?: continue | ||
left.add(x) | ||
right.add(y) | ||
} | ||
this.left = left.toList() | ||
this.right = right.toList() | ||
} | ||
|
||
fun part1() = left.sorted().zip(right.sorted(), Int::minus).sumOf(::abs) | ||
|
||
fun part2(): Int { | ||
val right = right.groupingBy { it }.eachCount() | ||
return left.sumOf { it * right.getOrElse(it) { 0 } } | ||
} | ||
|
||
companion object { | ||
private val splitter = """\s+""".toRegex() | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Days.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,27 @@ | ||
package com.github.ephemient.aoc2024 | ||
|
||
val days: List<Day> = listOf( | ||
Day(1, ::Day1, Day1::part1, Day1::part2), | ||
) | ||
|
||
data class Day( | ||
val day: Int, | ||
val parts: Int, | ||
val solver: (String) -> List<suspend () -> Any?>, | ||
val name: String = day.toString(), | ||
val skipByDefault: Boolean = false, | ||
) | ||
|
||
fun <T> Day( | ||
day: Int, | ||
create: (String) -> T, | ||
vararg parts: suspend (T) -> Any?, | ||
name: String = day.toString(), | ||
skipByDefault: Boolean = false, | ||
): Day = Day( | ||
day = day, | ||
parts = parts.size, | ||
solver = { with(create(it)) { parts.map { suspend { it.invoke(this) } } } }, | ||
name = name, | ||
skipByDefault = skipByDefault, | ||
) |
7 changes: 7 additions & 0 deletions
7
kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/IntPair.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,7 @@ | ||
package com.github.ephemient.aoc2024 | ||
|
||
data class IntPair(val first: Int, val second: Int) { | ||
override fun toString(): String = "($first, $second)" | ||
} | ||
|
||
infix fun Int.to(other: Int): IntPair = IntPair(this, other) |
10 changes: 10 additions & 0 deletions
10
kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Math.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,10 @@ | ||
package com.github.ephemient.aoc2024 | ||
|
||
fun gcd(x: Long, y: Long): Long { | ||
var a = x | ||
var b = y | ||
while (b != 0L) a = b.also { b = a.mod(b) } | ||
return a | ||
} | ||
|
||
fun lcm(x: Long, y: Long): Long = x / gcd(x, y) * y |
13 changes: 13 additions & 0 deletions
13
kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Parallel.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,13 @@ | ||
package com.github.ephemient.aoc2024 | ||
|
||
import kotlinx.coroutines.flow.channelFlow | ||
import kotlinx.coroutines.flow.fold | ||
import kotlinx.coroutines.launch | ||
|
||
suspend fun <T> Iterable<T>.parSum(block: (T) -> Long): Long = channelFlow { | ||
for (value in this@parSum) { | ||
launch { | ||
send(block(value)) | ||
} | ||
} | ||
}.fold(0, Long::plus) |
12 changes: 12 additions & 0 deletions
12
kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/PriorityQueue.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,12 @@ | ||
package com.github.ephemient.aoc2024 | ||
|
||
interface PriorityQueue<E : Any> : Iterable<E> { | ||
fun isEmpty(): Boolean | ||
|
||
fun add(element: E): Boolean | ||
|
||
@Throws(NoSuchElementException::class) | ||
fun remove(): E | ||
} | ||
|
||
expect fun <E : Any> PriorityQueue(comparator: Comparator<E>): PriorityQueue<E> |
11 changes: 11 additions & 0 deletions
11
kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Transpose.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,11 @@ | ||
package com.github.ephemient.aoc2024 | ||
|
||
fun Iterable<String>.transpose(): List<String> = buildList { | ||
val strings = this@transpose.filterTo(mutableListOf()) { it.isNotEmpty() } | ||
var i = 0 | ||
while (strings.isNotEmpty()) { | ||
add(buildString(strings.size) { for (string in strings) append(string[i]) }) | ||
i++ | ||
strings.removeAll { it.length == i } | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
kt/aoc2024-lib/src/commonTest/kotlin/com/github/ephemient/aoc2024/Day1Test.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,28 @@ | ||
package com.github.ephemient.aoc2024 | ||
|
||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
|
||
class Day1Test { | ||
@Test | ||
fun part1() { | ||
assertEquals(11, Day1(example).part1()) | ||
} | ||
|
||
@Test | ||
fun part2() { | ||
assertEquals(31, Day1(example).part2()) | ||
} | ||
|
||
companion object { | ||
private val example = | ||
""" | ||
|3 4 | ||
|4 3 | ||
|2 5 | ||
|1 3 | ||
|3 9 | ||
|3 3 | ||
|""".trimMargin() | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
kt/aoc2024-lib/src/jvmMain/kotlin/com/github/ephemient/aoc2024/JvmPriorityQueue.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,8 @@ | ||
package com.github.ephemient.aoc2024 | ||
|
||
internal class JvmPriorityQueue<E : Any>(comparator: Comparator<E>) : | ||
PriorityQueue<E>, | ||
java.util.PriorityQueue<E>(comparator) | ||
|
||
actual fun <E : Any> PriorityQueue(comparator: Comparator<E>): PriorityQueue<E> = | ||
JvmPriorityQueue(comparator) |
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,5 @@ | ||
plugins { | ||
alias(libs.plugins.kotlin.multiplatform) apply false | ||
} | ||
|
||
group = "com.github.ephemient.aoc2024" |
Oops, something went wrong.