Skip to content

Commit

Permalink
Day 1: Historian Hysteria
Browse files Browse the repository at this point in the history
  • Loading branch information
ephemient committed Dec 1, 2024
1 parent 692d4fb commit 5f42996
Show file tree
Hide file tree
Showing 31 changed files with 958 additions and 3 deletions.
6 changes: 6 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# generated by Gradle Wrapper
kt/gradlew linguist-generated
kt/gradlew.bat linguist-generated

# generated by Kotlin/JS
kt/kotlin-js-store/yarn.lock
122 changes: 122 additions & 0 deletions .github/workflows/kt-bench.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
name: Kotlin benchmarks

on:
workflow_dispatch:

permissions:
contents: write

jobs:
get-inputs:
uses: ephemient/aoc2024/.github/workflows/get-inputs.yml@main
secrets:
SESSION: ${{ secrets.SESSION }}

jmh-visualizer:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
with:
repository: jzillmann/jmh-visualizer
- uses: actions/setup-node@v4
with:
node-version: 18
cache: npm
- run: npm install
- run: npm run providedZip
env:
NODE_OPTIONS: --openssl-legacy-provider
- uses: actions/upload-artifact@v4
with:
name: jmh-visualizer
path: jmh-visualizer.zip

assemble:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- uses: actions/setup-java@v4
with:
distribution: temurin
java-version: 17
- uses: gradle/actions/setup-gradle@v4
- run: ./gradlew assembleBenchmarks
working-directory: kt

build:
needs: [ assemble, get-inputs ]
runs-on: ubuntu-latest
strategy:
matrix:
target: [ jvm, js, linuxX64 ]

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: 17
- uses: gradle/actions/setup-gradle@v4
with:
cache-read-only: true
- run: ./gradlew ${{ matrix.target }}Benchmark
working-directory: kt
env:
AOC2024_DATADIR: ${{ github.workspace }}/inputs
- uses: actions/upload-artifact@v4
with:
name: ${{ matrix.target }}-benchmarks
path: kt/aoc2024-exe/build/reports/benchmarks

docs:
needs: [ jmh-visualizer, build ]
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
with:
ref: gh-docs
- uses: actions/download-artifact@v4
with:
name: jmh-visualizer
- uses: actions/download-artifact@v4
with:
name: jvm-benchmarks
path: benchmarks
- uses: actions/download-artifact@v4
with:
name: js-benchmarks
path: benchmarks
- uses: actions/download-artifact@v4
with:
name: linuxX64-benchmarks
path: benchmarks
- run: rm -rf jmh-visualizer
- run: unzip -d jmh-visualizer jmh-visualizer.zip
- name: Create provided.js
run: |
shopt -s failglob
names=() jsonargs=()
for file in benchmarks/main/*/*.json; do
name=${file##*/}
name=${name%.*}
names+=("$name")
jsonargs+=(--slurpfile "$name" "$file")
done
cat >jmh-visualizer/provided.js <<EOF
// provided.js - generated by ${{ github.repository }}/${{ github.workflow }}, $(date +'%Y-%m-%d %H:%M:%S.%N')
var providedBenchmarks = $(jq --null-input --compact-output '$ARGS.positional|unique' --args "${names[@]}");
var providedBenchmarkStore = $(jq --null-input '$ARGS.named|map_values(.[0]|sort_by(.benchmark|[split("[.]|(?<!\\d)(?!\\D)|(?<!\\D)(?!\\d)")|map(try tonumber catch .),.]))' "${jsonargs[@]}");
EOF
- uses: EndBug/add-and-commit@v9
with:
add: jmh-visualizer
message: 'kotlinx.benchmark ${{ github.sha }}'
105 changes: 105 additions & 0 deletions .github/workflows/kt.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
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:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- uses: actions/setup-java@v4
with:
distribution: temurin
java-version: 17
- uses: gradle/actions/setup-gradle@v4
- run: ./gradlew build distZip
working-directory: kt
- uses: actions/upload-artifact@v4
with:
name: aoc2024-exe
path: kt/aoc2024-exe/build/distributions/*.zip
- uses: actions/upload-artifact@v4
with:
name: aoc2024-kexe
path: |
kt/aoc2024-exe/build/bin/*/debugExecutable/*.kexe
kt/aoc2024-exe/build/bin/*/releaseExecutable/*.kexe
- uses: actions/upload-artifact@v4
with:
name: aoc2024-js
path: kt/build/js/packages/aoc2024-aoc2024-exe/kotlin/*
- uses: actions/upload-artifact@v4
with:
name: aoc2024-web
path: kt/web/build/dist/js/productionExecutable/*

run-jvm:
needs: [ get-inputs, build ]
runs-on: ubuntu-latest

steps:
- uses: actions/download-artifact@v4
with:
name: inputs
path: inputs
- uses: actions/download-artifact@v4
with:
name: aoc2024-exe
- run: unzip aoc2024-exe.zip
- uses: actions/setup-java@v4
with:
distribution: temurin
java-version: 17
- run: aoc2024-exe/bin/aoc2024-exe
env:
AOC2024_DATADIR: inputs

run-native:
needs: [ get-inputs, build ]
runs-on: ubuntu-latest

steps:
- uses: actions/download-artifact@v4
with:
name: inputs
path: inputs
- uses: actions/download-artifact@v4
with:
name: aoc2024-kexe
- run: chmod +x linuxX64/*/aoc2024-exe.kexe
- run: linuxX64/releaseExecutable/aoc2024-exe.kexe
env:
AOC2024_DATADIR: inputs

run-node:
needs: [ get-inputs, build ]
runs-on: ubuntu-latest

steps:
- uses: actions/download-artifact@v4
with:
name: inputs
path: inputs
- uses: actions/download-artifact@v4
with:
name: aoc2024-js
- uses: actions/setup-node@v4
with:
node-version: 22.0.0
- run: node aoc2024-aoc2024-exe.js
env:
AOC2024_DATADIR: inputs
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@

Development occurs in language-specific directories:

|[Haskell](hs) ![Haskell CI](https://github.com/ephemient/aoc2024/workflows/Haskell%20CI/badge.svg)|
|--:|
|[Day1.hs](hs/src/Day1.hs)|
|[Haskell](hs) ![Haskell CI](https://github.com/ephemient/aoc2024/workflows/Haskell%20CI/badge.svg)|[Kotlin](kt) ![Kotlin CI](https://github.com/ephemient/aoc2024/workflows/Kotlin%20CI/badge.svg)|
|--:|--:|
|[Day1.hs](hs/src/Day1.hs)|[Day1.kt](kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day1.kt)|
1 change: 1 addition & 0 deletions kt/.envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export AOC2024_DATADIR=$(realpath ..)
6 changes: 6 additions & 0 deletions kt/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.gradle/
.kotlin/
.idea/
build/
local.properties
*~
22 changes: 22 additions & 0 deletions kt/README.md
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
```
27 changes: 27 additions & 0 deletions kt/aoc2024-exe/build.gradle.kts
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)
}
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()
}
}
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
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()
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)
}
39 changes: 39 additions & 0 deletions kt/aoc2024-lib/build.gradle.kts
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()
}
Loading

0 comments on commit 5f42996

Please sign in to comment.