Minor opt.

Change-Id: Id25e6987382144228afebe661d0b4bdd576b9d6d
This commit is contained in:
Chris Hodges 2023-12-22 10:58:11 +01:00
parent 946048f3f1
commit bfee59402e

View File

@ -183,7 +183,7 @@ fun main() {
} }
val disBricks = bricks.filterNot { it.onTopBricks.all { it.uponBricks.size != 1 } } val disBricks = bricks.filterNot { it.onTopBricks.all { it.uponBricks.size != 1 } }
val safeBricks = bricks.size - disBricks.size val safeBricks = bricks.size - disBricks.size
var sum = 0 var fallingSum = 0
for (brick in disBricks) { for (brick in disBricks) {
val fallingSet = HashSet<Brick>() val fallingSet = HashSet<Brick>()
val removeQueue = ArrayDeque<Brick>() val removeQueue = ArrayDeque<Brick>()
@ -192,11 +192,11 @@ fun main() {
while (removeQueue.isNotEmpty()) { while (removeQueue.isNotEmpty()) {
val falling = removeQueue.removeFirst() val falling = removeQueue.removeFirst()
fallingSet.add(falling) fallingSet.add(falling)
removeQueue.addAll(falling.onTopBricks.filter { it.uponBricks.subtract(fallingSet).isEmpty() }) removeQueue.addAll(falling.onTopBricks.filter { fallingSet.containsAll(it.uponBricks) })
} }
sum += fallingSet.size - 1 fallingSum += fallingSet.size - 1
} }
return safeBricks to sum return safeBricks to fallingSum
} }
// test if implementation meets criteria from the description, like: // test if implementation meets criteria from the description, like: