Skip to content

Commit

Permalink
Reorganize, remove parallization and re-enable multiplatform
Browse files Browse the repository at this point in the history
Not sure why JS and native were having so much trouble with the
coroutine launching but this now works just as fast without.
  • Loading branch information
ephemient committed Dec 27, 2023
1 parent 64bea34 commit 8e014bf
Show file tree
Hide file tree
Showing 11 changed files with 41 additions and 90 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,9 @@ class Day23Bench {
input = getDayInput(23)
}

@Suppress("EXPOSED_FUNCTION_RETURN_TYPE")
@Benchmark
fun part1() = runSuspend {
Day23(input).part1()
}
fun part1() = Day23(input).part1()

@Suppress("EXPOSED_FUNCTION_RETURN_TYPE")
@Benchmark
fun part2() = runSuspend {
Day23(input).part2()
}
fun part2() = Day23(input).part2()
}
6 changes: 0 additions & 6 deletions kt/aoc2023-lib/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@ plugins {
id("com.github.ephemient.aoc2023.kotlin.multiplatform.jvm.platform")
id("com.github.ephemient.aoc2023.kotlin.multiplatform.native.platforms")
id("com.github.ephemient.aoc2023.kotlin.multiplatform.js.platform")
alias(libs.plugins.kotlinx.atomicfu)
}

atomicfu {
jvmVariant = "VH"
}

kotlin {
Expand All @@ -20,7 +15,6 @@ kotlin {
sourceSets {
commonMain {
dependencies {
implementation(libs.kotlinx.atomicfu)
implementation(libs.kotlinx.coroutines)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
package com.github.ephemient.aoc2023

import kotlinx.atomicfu.atomic
import kotlinx.atomicfu.update
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.launch

class Day23(private val input: String) {
private val src: IntPair
private val dst: IntPair
Expand All @@ -21,50 +15,48 @@ class Day23(private val input: String) {
}
}

suspend fun part1(): Int = Solver(src, dst, mkGraph(true))()
fun part1(): Int = solve(mkGraph(true))

suspend fun part2(): Int = Solver(src, dst, mkGraph(false))()
fun part2(): Int = solve(mkGraph(false))

private class Solver(
private val src: IntPair,
private val dst: IntPair,
private val gr: Map<IntPair, Map<IntPair, Int>>,
) {
private val best = atomic(0)
private fun solve(gr: Map<IntPair, Map<IntPair, Int>>): Int = solve(gr, src, setOf(src), 0, 0)

@Suppress("CyclomaticComplexMethod", "ReturnCount")
fun CoroutineScope.go(src: IntPair, used: Set<IntPair>, distance: Int) {
val best = best.value
if (src == dst) {
if (distance > best) this@Solver.best.update { maxOf(it, distance) }
return
}

val stack = mutableListOf(src)
val visited = used.toMutableSet().apply { add(src) }
var potential = distance
while (true) {
val node = stack.removeLastOrNull() ?: break
var maxWeight = 0
for ((next, weight) in gr[node] ?: continue) {
if (next !in used) maxWeight = maxOf(maxWeight, weight)
if (!visited.add(next)) continue
stack.add(next)
}
potential += maxWeight
}
if (potential < best || dst !in visited) return

val used2 = used + src
for ((next, weight) in gr[src] ?: return) {
if (next !in used) launch { go(next, used2, distance + weight) }
private fun solve(
gr: Map<IntPair, Map<IntPair, Int>>,
pos: IntPair,
used: Set<IntPair>,
distance: Int,
best: Int,
): Int = if (pos == dst) {
maxOf(distance, best)
} else {
val reachable = mutableSetOf<IntPair>()
val potential = distance + scan(gr, pos, used, reachable)
if (potential <= best || dst !in reachable) {
best
} else {
gr[pos].orEmpty().entries.sortedByDescending { it.value }.fold(best) { acc, (next, weight) ->
if (next in used) acc else solve(gr, next, used + next, distance + weight, acc)
}
}
}

suspend operator fun invoke(): Int {
coroutineScope { go(src, emptySet(), 0) }
return best.value
}
private fun scan(
gr: Map<IntPair, Map<IntPair, Int>>,
pos: IntPair,
used: Set<IntPair>,
reachable: MutableSet<IntPair>,
): Int {
val next = gr[pos] ?: return 0
var greatest = 0
return next.entries.sumOf { (next, weight) ->
if (next in used) {
0
} else {
greatest = maxOf(greatest, weight)
if (reachable.add(next)) scan(gr, next, used, reachable) else 0
}
} + greatest
}

@Suppress("CyclomaticComplexMethod")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ val days: List<Day> = listOf(
Day(20, ::Day20, Day20::part1, Day20::part2),
Day(21, ::Day21, Day21::part1, Day21::part2),
Day(22, ::Day22, Day22::part1, Day22::part2),
Day(23, ::Day23, Day23::part1, Day23::part2, skipByDefault = !isJvm),
Day(23, ::Day23, Day23::part1, Day23::part2),
Day(24, ::Day24, Day24::part1, Day24::part2),
Day(25, ::Day25, Day25::part1),
)
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
package com.github.ephemient.aoc2023

import kotlinx.coroutines.test.runTest
import kotlin.test.Test
import kotlin.test.assertEquals

class Day23Test {
@Test
fun part1() = runTest {
fun part1() {
assertEquals(94, Day23(example).part1())
}

@Test
fun part2() = runTest {
fun part2() {
assertEquals(154, Day23(example).part2())
}

Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

3 changes: 0 additions & 3 deletions kt/gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ detekt = "1.23.4"
graal-sdk = "23.1.1"
junit-jupiter = "5.10.1"
kotlin = "1.9.22"
kotlinx-atomicfu = "0.23.1"
kotlinx-benchmark = "0.4.10"
kotlinx-coroutines = "1.8.0-RC2"
kotlinx-html = "0.10.1"
Expand All @@ -14,7 +13,6 @@ native-image-plugin = "0.9.28"
[plugins]
dependency-updates = { id = "com.github.ben-manes.versions", version.ref = "dependency-updates" }
kotlin-plugin-allopen = { id = "org.jetbrains.kotlin.plugin.allopen", version.ref = "kotlin" }
kotlinx-atomicfu = { id = "kotlinx-atomicfu", version.ref = "kotlinx-atomicfu" }
kotlinx-benchmark = { id = "org.jetbrains.kotlinx.benchmark", version.ref = "kotlinx-benchmark" }
native-image = { id = "org.graalvm.buildtools.native", version.ref = "native-image-plugin" }

Expand All @@ -26,7 +24,6 @@ junit-jupiter-api = { module = "org.junit.jupiter:junit-jupiter-api", version.re
junit-jupiter-engine = { module = "org.junit.jupiter:junit-jupiter-engine", version.ref = "junit-jupiter" }
kotlin-wrappers-browser = { module = "org.jetbrains.kotlin-wrappers:kotlin-browser", version = "1.0.0-pre.673" }
kotlin-wrappers-node = { module = "org.jetbrains.kotlin-wrappers:kotlin-node", version = "18.16.12-pre.673" }
kotlinx-atomicfu = { module = "org.jetbrains.kotlinx:atomicfu", version.ref = "kotlinx-atomicfu" }
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-coroutines-test = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-test", version.ref = "kotlinx-coroutines" }
Expand Down
10 changes: 0 additions & 10 deletions kt/settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,6 @@ pluginManagement {
mavenCentral()
gradlePluginPortal()
}

plugins {
resolutionStrategy {
eachPlugin {
if (requested.id.id == "kotlinx-atomicfu") {
useModule("org.jetbrains.kotlinx:atomicfu-gradle-plugin:${requested.version}")
}
}
}
}
}

gradle.afterProject {
Expand Down

0 comments on commit 8e014bf

Please sign in to comment.