Skip to content

Commit

Permalink
Simple web interface
Browse files Browse the repository at this point in the history
  • Loading branch information
ephemient committed Dec 2, 2023
1 parent c168fd9 commit 8c026d3
Show file tree
Hide file tree
Showing 8 changed files with 1,003 additions and 21 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/kt.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ jobs:
with:
name: aoc2023-js
path: kt/build/js/packages/aoc2023-aoc2023-exe/kotlin/*
- uses: actions/upload-artifact@v3
with:
name: aoc2023-web
path: kt/web/build/dist/js/productionExecutable/*

run-jvm:
needs: [ get-inputs, build ]
Expand Down
2 changes: 1 addition & 1 deletion kt/buildSrc/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ gradlePlugin {
}

dependencies {
implementation(kotlin("gradle-plugin", libs.versions.kotlin.asProvider().get()))
implementation(kotlin("gradle-plugin", libs.versions.kotlin.get()))
implementation(libs.detekt.plugin)
}
8 changes: 6 additions & 2 deletions kt/gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ detekt = "1.23.4"
graal-sdk = "23.1.1"
junit-jupiter = "5.10.1"
kotlin = "1.9.21"
kotlin-wrappers = "18.16.12-pre.652"
kotlinx-benchmark = "0.4.9"
kotlinx-coroutines = "1.7.3"
kotlinx-html = "0.9.1"
okio = "3.6.0"
native-image-plugin = "0.9.28"

Expand All @@ -21,8 +22,11 @@ detekt-plugin = { module = "io.gitlab.arturbosch.detekt:detekt-gradle-plugin", v
graal-sdk = { module = "org.graalvm.sdk:graal-sdk", version.ref = "graal-sdk" }
junit-jupiter-api = { module = "org.junit.jupiter:junit-jupiter-api", version.ref = "junit-jupiter" }
junit-jupiter-engine = { module = "org.junit.jupiter:junit-jupiter-engine", version.ref = "junit-jupiter" }
kotlin-wrappers-node = { module = "org.jetbrains.kotlin-wrappers:kotlin-node", version.ref = "kotlin-wrappers" }
kotlin-wrappers-browser = { module = "org.jetbrains.kotlin-wrappers:kotlin-browser", version = "1.0.0-pre.652" }
kotlin-wrappers-node = { module = "org.jetbrains.kotlin-wrappers:kotlin-node", version = "18.16.12-pre.652" }
kotlinx-benchmark = { module = "org.jetbrains.kotlinx:kotlinx-benchmark-runtime", version.ref = "kotlinx-benchmark" }
kotlinx-coroutines = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-core", version.ref = "kotlinx-coroutines" }
kotlinx-html = { module = "org.jetbrains.kotlinx:kotlinx-html", version.ref = "kotlinx-html" }
okio = { module = "com.squareup.okio:okio", version.ref = "okio" }

[bundles]
Expand Down
915 changes: 898 additions & 17 deletions kt/kotlin-js-store/yarn.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion kt/settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ dependencyResolutionManagement {
}

rootProject.name = "aoc2023"
include("aoc2023-exe", "aoc2023-lib")
include("aoc2023-exe", "aoc2023-lib", "web")
32 changes: 32 additions & 0 deletions kt/web/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
plugins {
kotlin("multiplatform")
id("com.github.ephemient.aoc2023.kotlin.multiplatform.js.platform")
}

kotlin {
js {
useCommonJs()
browser {
commonWebpackConfig {
outputFileName = "script.js"
}
}
binaries.executable()
}

sourceSets {
commonMain {
dependencies {
implementation(projects.aoc2023Lib)
implementation(libs.kotlinx.coroutines)
}
}

jsMain {
dependencies {
implementation(libs.kotlin.wrappers.browser)
implementation(libs.kotlinx.html)
}
}
}
}
46 changes: 46 additions & 0 deletions kt/web/src/jsMain/kotlin/com/github/ephemient/aoc2023/web/Main.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.github.ephemient.aoc2023.web

import com.github.ephemient.aoc2023.days
import kotlinx.browser.document
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.async
import kotlinx.coroutines.cancelChildren
import kotlinx.coroutines.launch
import kotlinx.dom.appendText
import kotlinx.dom.clear
import kotlinx.html.dom.append
import kotlinx.html.js.option
import org.w3c.dom.HTMLFormElement
import org.w3c.dom.HTMLPreElement
import org.w3c.dom.HTMLSelectElement
import org.w3c.dom.HTMLTextAreaElement

fun main() {
val daySelect = document.getElementById("day") as HTMLSelectElement
val input = document.getElementById("input") as HTMLTextAreaElement
val output = document.getElementById("output") as HTMLPreElement
val form = document.getElementById("container") as HTMLFormElement
daySelect.append {
for ((i, day) in days.withIndex()) {
option {
value = i.toString()
+"Day ${day.name}"
}
}
}
val job = SupervisorJob()
form.onsubmit = { event ->
val day = days[daySelect.value.toInt()]
job.cancelChildren()
output.clear()
GlobalScope.launch(job) {
val parts = day.solver(input.value).map { async { it() } }
for (part in parts) {
val result = part.await()
output.appendText("$result\n")
}
}
event.preventDefault()
}
}
15 changes: 15 additions & 0 deletions kt/web/src/jsMain/resources/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!DOCTYPE html>
<html>
<head>
<title>Advent of Code 2023 in Kotlin/JS by @ephemient</title>
</head>
<body>
<form id="container">
<select id="day"></select>
<textarea id="input"></textarea>
<pre id="output"></pre>
<input type="submit">
</form>
<script src="script.js"></script>
</body>
</html>

0 comments on commit 8c026d3

Please sign in to comment.