Skip to content

Commit c9f14cf

Browse files
committed
Simpler way of iterating through powerset
1 parent 2f897f9 commit c9f14cf

File tree

1 file changed

+27
-42
lines changed
  • kt/src/commonMain/kotlin/com/github/ephemient/aoc2022

1 file changed

+27
-42
lines changed

kt/src/commonMain/kotlin/com/github/ephemient/aoc2022/Day16.kt

Lines changed: 27 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -86,51 +86,36 @@ class Day16(lines: List<String>) {
8686
}
8787
if (movesByTime.values.all { movesByAgent -> movesByAgent.all { it.isEmpty() } }) continue
8888
for ((d, movesByAgent) in movesByTime) {
89-
for (bitmask in 1 until 1.shl(agents)) {
90-
run { // https://youtrack.jetbrains.com/issue/KT-1436
91-
val indices = IntArray(agents) { i ->
92-
if (bitmask and 1.shl(i) != 0) {
93-
if (movesByAgent[i].isEmpty()) return@run
94-
0
89+
val indices = IntArray(agents) { -1 }
90+
while (
91+
indices.withIndex().any { (i, j) ->
92+
(if (j < movesByAgent[i].lastIndex) j + 1 else -1).also { indices[i] = it } >= 0
93+
}
94+
) {
95+
val allUnique: Boolean
96+
val valves = buildSet {
97+
allUnique = indices.withIndex().all { (i, j) -> j < 0 || add(movesByAgent[i][j]) }
98+
}
99+
if (allUnique) {
100+
val rooms = indices.mapIndexed { i, j ->
101+
if (j >= 0) {
102+
IndexedValue(0, movesByAgent[i][j])
95103
} else {
96-
-1
104+
val (age, room) = state.rooms[i]
105+
IndexedValue(age + d + 1, room)
97106
}
107+
}.sortedWith(compareBy({ it.value }, { it.index }))
108+
val rate = indices.withIndex().sumOf { (i, j) ->
109+
if (j >= 0) graph.getValue(movesByAgent[i][j]).index else 0
98110
}
99-
do {
100-
var allUnique = true
101-
val valves = buildSet {
102-
indices.forEachIndexed { i, j ->
103-
if (j >= 0) allUnique = add(movesByAgent[i][j]) && allUnique
104-
}
105-
}
106-
if (allUnique) {
107-
val rooms = indices.mapIndexed { i, j ->
108-
if (j >= 0) {
109-
IndexedValue(0, movesByAgent[i][j])
110-
} else {
111-
val (age, room) = state.rooms[i]
112-
IndexedValue(age + d + 1, room)
113-
}
114-
}.sortedWith(compareBy({ it.value }, { it.index }))
115-
val rate = indices.withIndex().sumOf { (i, j) ->
116-
if (j >= 0) graph.getValue(movesByAgent[i][j]).index else 0
117-
}
118-
val newState = State(
119-
rooms = rooms,
120-
valves = state.valves - valves,
121-
flow = state.flow + rate,
122-
total = state.total + state.flow * (d + 1),
123-
timeRemaining = state.timeRemaining - d - 1,
124-
)
125-
queue.add(IndexedValue(estimate + rate * newState.timeRemaining, newState))
126-
}
127-
val allIsAtEnd = indices.withIndex().all { (i, j) ->
128-
if (j < 0) return@all true
129-
val isAtEnd = j == movesByAgent[i].lastIndex
130-
indices[i] = if (isAtEnd) 0 else j + 1
131-
isAtEnd
132-
}
133-
} while (!allIsAtEnd)
111+
val newState = State(
112+
rooms = rooms,
113+
valves = state.valves - valves,
114+
flow = state.flow + rate,
115+
total = state.total + state.flow * (d + 1),
116+
timeRemaining = state.timeRemaining - d - 1,
117+
)
118+
queue.add(IndexedValue(estimate + rate * newState.timeRemaining, newState))
134119
}
135120
}
136121
}

0 commit comments

Comments
 (0)