-
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
5 changed files
with
216 additions
and
1 deletion.
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
23 changes: 23 additions & 0 deletions
23
kt/aoc2023-exe/src/commonMain/kotlin/com/github/ephemient/aoc2023/exe/Day20Bench.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,23 @@ | ||
package com.github.ephemient.aoc2023.exe | ||
|
||
import com.github.ephemient.aoc2023.Day20 | ||
import kotlinx.benchmark.Benchmark | ||
import kotlinx.benchmark.Scope | ||
import kotlinx.benchmark.Setup | ||
import kotlinx.benchmark.State | ||
|
||
@State(Scope.Benchmark) | ||
class Day20Bench { | ||
private lateinit var input: String | ||
|
||
@Setup | ||
fun setup() { | ||
input = getDayInput(20) | ||
} | ||
|
||
@Benchmark | ||
fun part1() = Day20(input).part1() | ||
|
||
@Benchmark | ||
fun part2() = Day20(input).part2() | ||
} |
153 changes: 153 additions & 0 deletions
153
kt/aoc2023-lib/src/commonMain/kotlin/com/github/ephemient/aoc2023/Day20.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,153 @@ | ||
package com.github.ephemient.aoc2023 | ||
|
||
class Day20(input: String) { | ||
private val machines: Map<String, Pair<Set<String>, Type?>> | ||
private val dependencies: Map<String, Set<String>> | ||
|
||
init { | ||
val machines = mutableMapOf<String, Pair<Set<String>, Type?>>() | ||
val dependencies = mutableMapOf<String, MutableSet<String>>() | ||
|
||
for (line in input.lines()) { | ||
val (lhs, rhs) = line.split(" -> ", limit = 2).takeIf { it.size == 2 } ?: continue | ||
val (type, key) = when { | ||
lhs.startsWith('%') -> Type.FlipFlop to lhs.drop(1) | ||
lhs.startsWith('&') -> Type.Conjunction to lhs.drop(1) | ||
else -> null to lhs | ||
} | ||
val dsts = rhs.split(", ").toSet() | ||
machines[key] = dsts to type | ||
for (dst in dsts) dependencies.getOrPut(dst) { mutableSetOf() }.add(key) | ||
} | ||
|
||
this.machines = machines | ||
this.dependencies = dependencies | ||
} | ||
|
||
fun part1(): Int { | ||
val state = machines.mapValues { (key, value) -> | ||
value.second.createState(dependencies[key] ?: emptySet()) | ||
} | ||
var x = 0 | ||
var y = 0 | ||
repeat(1000) { | ||
val queue = ArrayDeque<Triple<String, String, Boolean>>() | ||
queue.add(Triple("button", "broadcaster", false)) | ||
@Suppress("LoopWithTooManyJumpStatements") | ||
while (true) { | ||
val (src, key, value) = queue.removeFirstOrNull() ?: break | ||
if (value) x++ else y++ | ||
val newValue = state[key]?.onPulse(src, value) ?: continue | ||
machines.getValue(key).first.mapTo(queue) { Triple(key, it, newValue) } | ||
} | ||
} | ||
return x * y | ||
} | ||
|
||
@Suppress("CyclomaticComplexMethod", "NestedBlockDepth", "ReturnCount") | ||
fun part2(): Long? { | ||
val conjunction = dependencies["rx"]?.singleOrNull() | ||
?.takeIf { machines[it]?.second == Type.Conjunction } | ||
?: return null | ||
val subsets = (dependencies[conjunction] ?: emptySet()).associateWith { dst -> | ||
buildSet { | ||
val stack = mutableListOf(dst) | ||
@Suppress("LoopWithTooManyJumpStatements") | ||
while (true) { | ||
val key = stack.removeLastOrNull() ?: break | ||
if (!add(key)) continue | ||
dependencies[key]?.let { stack.addAll(it) } | ||
} | ||
} | ||
} | ||
subsets.values.toList().let { values -> | ||
for (i in values.indices) { | ||
for (j in i + 1..values.lastIndex) { | ||
if (values[i].intersect(values[j]).singleOrNull() != "broadcaster") { | ||
return null | ||
} | ||
} | ||
} | ||
} | ||
return subsets.entries.fold(1) { acc: Long, (dst, subset) -> | ||
val state = machines.filterKeys { it in subset }.mapValues { (key, value) -> | ||
value.second.createState(dependencies[key] ?: emptySet()) | ||
} | ||
val seen = mutableMapOf<Map<String, Boolean?>, Int>() | ||
var hadOutput = false | ||
while (true) { | ||
var hasOutput = false | ||
val queue = ArrayDeque<Triple<String, String, Boolean>>() | ||
queue.add(Triple("button", "broadcaster", false)) | ||
@Suppress("LoopWithTooManyJumpStatements") | ||
while (true) { | ||
val (src, key, value) = queue.removeFirstOrNull() ?: break | ||
val newValue = state[key]?.onPulse(src, value) ?: continue | ||
if (newValue && key == dst) hasOutput = true | ||
machines.getValue(key).first | ||
.filter { it in subset } | ||
.mapTo(queue) { Triple(key, it, newValue) } | ||
} | ||
val snapshot = state.mapValues { it.value.value } | ||
if (snapshot in seen) { | ||
if (!hadOutput) return null | ||
val i = seen.getValue(snapshot) | ||
if (i != 0) return null | ||
break | ||
} | ||
seen[snapshot] = seen.size | ||
if (hadOutput) return null | ||
hadOutput = hasOutput | ||
} | ||
lcm(acc, seen.size.toLong()) | ||
} | ||
} | ||
|
||
private sealed interface State { | ||
object Default : State { | ||
override val value: Boolean? | ||
get() = null | ||
|
||
override fun onPulse(key: String, value: Boolean): Boolean = value | ||
} | ||
|
||
class FlipFlop : State { | ||
override var value = false | ||
private set | ||
|
||
override fun onPulse(key: String, value: Boolean): Boolean? = if (value) { | ||
null | ||
} else { | ||
!this.value | ||
}?.also { this.value = it } | ||
} | ||
|
||
class Conjunction(keys: Set<String>) : State { | ||
private val state = keys.associateWithTo(mutableMapOf()) { false } | ||
|
||
override val value: Boolean | ||
get() = !state.values.all { it } | ||
|
||
override fun onPulse(key: String, value: Boolean): Boolean { | ||
state[key] = value | ||
return this.value | ||
} | ||
} | ||
|
||
val value: Boolean? | ||
|
||
fun onPulse(key: String, value: Boolean): Boolean? | ||
} | ||
|
||
private enum class Type { | ||
FlipFlop, Conjunction, | ||
} | ||
|
||
companion object { | ||
private fun Type?.createState(keys: Set<String>): State = when (this) { | ||
null -> State.Default | ||
Type.FlipFlop -> State.FlipFlop() | ||
Type.Conjunction -> State.Conjunction(keys) | ||
} | ||
} | ||
} |
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
38 changes: 38 additions & 0 deletions
38
kt/aoc2023-lib/src/commonTest/kotlin/com/github/ephemient/aoc2023/Day20Test.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,38 @@ | ||
package com.github.ephemient.aoc2023 | ||
|
||
import kotlin.test.Ignore | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
|
||
class Day20Test { | ||
@Test | ||
fun part1() { | ||
assertEquals(32000000, Day20(example1).part1()) | ||
assertEquals(11687500, Day20(example2).part1()) | ||
} | ||
|
||
@Test | ||
@Ignore | ||
fun part2() { | ||
assertEquals(0, Day20("").part2()) | ||
} | ||
|
||
companion object { | ||
private val example1 = | ||
""" | ||
|broadcaster -> a, b, c | ||
|%a -> b | ||
|%b -> c | ||
|%c -> inv | ||
|&inv -> a | ||
|""".trimMargin() | ||
private val example2 = | ||
""" | ||
|broadcaster -> a | ||
|%a -> inv, con | ||
|&inv -> b | ||
|%b -> con | ||
|&con -> output | ||
|""".trimMargin() | ||
} | ||
} |