Skip to content

Commit

Permalink
Merge pull request #194 from ephemient/kt/day18
Browse files Browse the repository at this point in the history
  • Loading branch information
ephemient authored Jan 4, 2025
2 parents cf64665 + 08d78e4 commit 5d230fc
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 16 deletions.
1 change: 0 additions & 1 deletion kt/aoc2024-exe/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import org.jetbrains.kotlin.gradle.plugin.KotlinSourceSetTree
plugins {
alias(libs.plugins.kotlin.multiplatform)
alias(libs.plugins.kotlin.plugin.allopen)
alias(libs.plugins.atomicfu)
alias(libs.plugins.detekt)
alias(libs.plugins.kotlinx.benchmark)
distribution
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,29 @@ import kotlin.coroutines.Continuation
import kotlin.coroutines.CoroutineContext
import kotlin.coroutines.EmptyCoroutineContext
import kotlin.coroutines.startCoroutine
import kotlinx.atomicfu.atomic
import kotlinx.coroutines.CompletableDeferred
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Deferred
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.InternalForInheritanceCoroutinesApi
import kotlinx.coroutines.Job
import kotlinx.coroutines.completeWith

actual fun <T> runBlockingBenchmark(block: suspend CoroutineScope.() -> T): T {
val continuation = ContinuationImpl<T>(Dispatchers.Unconfined)
block.startCoroutine(CoroutineScope(continuation.context), continuation)
return continuation.getCompleted()
val deferred = CompletableContinuation<T>(Dispatchers.Unconfined)
block.startCoroutine(CoroutineScope(deferred.context), deferred)
@OptIn(ExperimentalCoroutinesApi::class)
return deferred.getCompleted()
}

private class ContinuationImpl<T>(context: CoroutineContext = EmptyCoroutineContext) : Continuation<T> {
private val result = atomic<Result<T>?>(null)
@OptIn(InternalForInheritanceCoroutinesApi::class)
private class CompletableContinuation<T> private constructor(
context: CoroutineContext,
private val deferred: CompletableDeferred<T>,
) : Continuation<T>, Deferred<T> by deferred {
constructor(context: CoroutineContext = EmptyCoroutineContext) : this(context, CompletableDeferred(context[Job]))

override val context: CoroutineContext = context + Job(context[Job])

override fun resumeWith(result: Result<T>) {
check(this.result.compareAndSet(null, result)) { "Already resumed, but proposed with update $result" }
}

fun getCompleted(): T = checkNotNull(result.value) { "The job has not completed yet" }.getOrThrow()
override val context: CoroutineContext = context + deferred
override fun resumeWith(result: Result<T>) = check(deferred.completeWith(result)) { "Already resumed" }
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.ephemient.aoc2024

import kotlin.math.abs
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.asFlow
import kotlinx.coroutines.flow.flatMapMerge
import kotlinx.coroutines.flow.flow
Expand All @@ -26,6 +27,7 @@ class Day20(input: String) {

suspend fun part2(time: Int = 100) = solve(20, time)

@OptIn(ExperimentalCoroutinesApi::class)
private suspend fun solve(cheats: Int, time: Int) = path.indices.asFlow().flatMapMerge { i ->
flow {
var count = 0
Expand Down
2 changes: 0 additions & 2 deletions kt/gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
[versions]
asm = "9.7.1"
atomicfu = "0.26.1"
detekt = "1.23.7"
junit-jupiter = "5.11.4"
kotlin = "2.1.0"
Expand All @@ -12,7 +11,6 @@ native-image = "0.10.4"
okio = "3.9.1"

[plugins]
atomicfu = { id = "org.jetbrains.kotlinx.atomicfu", version.ref = "atomicfu" }
detekt = { id = "io.gitlab.arturbosch.detekt", version.ref = "detekt" }
kotlin-jvm = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" }
kotlin-multiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin" }
Expand Down

0 comments on commit 5d230fc

Please sign in to comment.