From bfee59402eb33f54fef93e785cab3e3a669d0e7a Mon Sep 17 00:00:00 2001 From: Chris Hodges Date: Fri, 22 Dec 2023 10:58:11 +0100 Subject: [PATCH] Minor opt. Change-Id: Id25e6987382144228afebe661d0b4bdd576b9d6d --- src/aoc2023/Day22.kt | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/aoc2023/Day22.kt b/src/aoc2023/Day22.kt index 5acee1f..bb003ae 100644 --- a/src/aoc2023/Day22.kt +++ b/src/aoc2023/Day22.kt @@ -183,7 +183,7 @@ fun main() { } val disBricks = bricks.filterNot { it.onTopBricks.all { it.uponBricks.size != 1 } } val safeBricks = bricks.size - disBricks.size - var sum = 0 + var fallingSum = 0 for (brick in disBricks) { val fallingSet = HashSet() val removeQueue = ArrayDeque() @@ -192,11 +192,11 @@ fun main() { while (removeQueue.isNotEmpty()) { val falling = removeQueue.removeFirst() 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: