Skip to content

Commit b57b33a

Browse files
committed
Add broken 2023 Day 22 Part 2 solution
1 parent 1498494 commit b57b33a

File tree

1 file changed

+34
-3
lines changed

1 file changed

+34
-3
lines changed

adventofcode2023/Day22.kt

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ object Day22 {
1313
// "2,0,5~2,2,5\n" +
1414
// "0,1,6~2,1,6\n" +
1515
// "1,1,8~1,1,9").split("\n")
16-
1716
private val bricks = File("resources/adventofcode2023/Day22.txt")
1817
.readLines()
1918
.withIndex()
@@ -110,13 +109,45 @@ object Day22 {
110109
brick.supports().all { it.numberOfSupporting() > 1 }
111110
}
112111

113-
fun part1() {
112+
// This function doesn't work for the bigger input somehow...
113+
// It works for the example input from the question
114+
private fun numberOfBrickThatWouldFall(brick: Brick): Int {
115+
var currentBricks = brick.supports()
116+
var lastIds = listOf(brick.id)
117+
var fallenCount = 0
118+
119+
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+
}
130+
131+
currentBricks = nextBricks
132+
lastIds = currentIds
133+
}
134+
135+
println("${brick.id}: $fallenCount")
136+
return fallenCount
137+
}
138+
139+
fun initialize() {
114140
initializeBricksMap()
115141
makeBricksFall()
116-
println(numberOfSafeToDisintegrate())
117142
}
143+
144+
fun part1() = println(numberOfSafeToDisintegrate())
145+
146+
fun part2() = println(bricks.sumOf { numberOfBrickThatWouldFall(it) })
118147
}
119148

120149
fun main() {
150+
Day22.initialize()
121151
Day22.part1()
152+
Day22.part2()
122153
}

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