Skip to content

Commit faf5c3c

Browse files
committed
Fix 2023 Day 22 Part 2 solution
1 parent b57b33a commit faf5c3c

File tree

1 file changed

+13
-25
lines changed

1 file changed

+13
-25
lines changed

adventofcode2023/Day22.kt

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,12 @@
11
package adventofcode2023
22

33
import java.io.File
4+
import java.util.PriorityQueue
45

56
object Day22 {
67
private const val EMPTY = -1
78
private const val FLOOR = 0
89

9-
// private val bricks = ("1,0,1~1,2,1\n" +
10-
// "0,0,2~2,0,2\n" +
11-
// "0,2,3~2,2,3\n" +
12-
// "0,0,4~0,2,4\n" +
13-
// "2,0,5~2,2,5\n" +
14-
// "0,1,6~2,1,6\n" +
15-
// "1,1,8~1,1,9").split("\n")
1610
private val bricks = File("resources/adventofcode2023/Day22.txt")
1711
.readLines()
1812
.withIndex()
@@ -77,7 +71,7 @@ object Day22 {
7771
fun numberOfSupporting(): Int =
7872
verticalNeighborsIds(false).size
7973

80-
fun supports(): List<Brick> =
74+
fun supportedBricks(): List<Brick> =
8175
verticalNeighborsIds(true).map { bricks[it - 1] }
8276

8377
fun fall(): Boolean {
@@ -106,30 +100,24 @@ object Day22 {
106100

107101
private fun numberOfSafeToDisintegrate(): Int =
108102
bricks.count { brick ->
109-
brick.supports().all { it.numberOfSupporting() > 1 }
103+
brick.supportedBricks().all { it.numberOfSupporting() > 1 }
110104
}
111105

112-
// This function doesn't work for the bigger input somehow...
113-
// It works for the example input from the question
114106
private fun numberOfBrickThatWouldFall(brick: Brick): Int {
115-
var currentBricks = brick.supports()
116-
var lastIds = listOf(brick.id)
107+
val currentBricks = PriorityQueue<Brick> { brick1, brick2 -> brick1.end.z - brick2.end.z }
108+
val fallenIds = mutableListOf(brick.id)
117109
var fallenCount = 0
118110

111+
currentBricks.addAll(brick.supportedBricks())
112+
119113
while (currentBricks.isNotEmpty()) {
120-
val nextBricks = mutableListOf<Brick>()
121-
val currentIds = mutableListOf<Int>()
122-
123-
currentBricks.forEach { current ->
124-
if (current.verticalNeighborsIds(false).all { lastIds.contains(it) }) {
125-
nextBricks.addAll(current.supports().filterNot { nextBricks.contains(it) })
126-
currentIds.add(current.id)
127-
fallenCount++
128-
}
129-
}
114+
val current = currentBricks.remove()
130115

131-
currentBricks = nextBricks
132-
lastIds = currentIds
116+
if (current.verticalNeighborsIds(false).all { fallenIds.contains(it) }) {
117+
if (!fallenIds.contains(current.id)) fallenIds.add(current.id)
118+
currentBricks.addAll(current.supportedBricks().filterNot { currentBricks.contains(it) })
119+
fallenCount++
120+
}
133121
}
134122

135123
println("${brick.id}: $fallenCount")

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy