changed zone filling with flooding

This commit is contained in:
Louis Heredero 2023-12-10 18:51:01 +01:00
parent 8d55a3c7b3
commit 651418bea3

View File

@ -66,23 +66,24 @@ object Puzzle2 {
println("Painted path neighbours") println("Painted path neighbours")
var newZones: Array[Array[Int]] = Array.ofDim(height, width) var newZones: Array[Array[Int]] = Array.ofDim(height, width)
var filled: Boolean = false var changed: Boolean = true
var exteriorZone: Int = 0 var exteriorZone: Int = 0
do { do {
filled = true changed = false
newZones = copyZones() newZones = copyZones()
for (y: Int <- 0 until height) { for (y: Int <- 0 until height) {
for (x: Int <- 0 until width) { for (x: Int <- 0 until width) {
if (zones(y)(x) == 0 && !path.contains((x, y))) { if (zones(y)(x) != 0) {
filled = false if (floodTile(x, y, newZones, path)) {
fillTile(x, y, newZones) changed = true
}
} }
} }
} }
zones = newZones zones = newZones
} while (!filled) } while (changed)
println("Filled zones") println("Flooded zones")
for (y: Int <- 0 until height) { for (y: Int <- 0 until height) {
for (x: Int <- 0 until width) { for (x: Int <- 0 until width) {
@ -106,20 +107,19 @@ object Puzzle2 {
return area return area
} }
def fillTile(x: Int, y: Int, newZones: Array[Array[Int]]): Unit = { def floodTile(x: Int, y: Int, newZones: Array[Array[Int]], path: ArrayBuffer[(Int, Int)]): Boolean = {
var zone: Int = 0 var changed: Boolean = false
for ((dx: Int, dy: Int) <- Walker.OFFSETS) { for ((dx: Int, dy: Int) <- Walker.OFFSETS) {
val x2: Int = x + dx val x2: Int = x + dx
val y2: Int = y + dy val y2: Int = y + dy
if (0 <= x2 && x2 < width && 0 <= y2 && y2 < height) { if (0 <= x2 && x2 < width && 0 <= y2 && y2 < height) {
if (zones(y2)(x2) != 0) { if (zones(y2)(x2) == 0 && !path.contains((x2, y2))) {
zone = zones(y2)(x2) newZones(y2)(x2) = zones(y)(x)
changed = true
} }
} }
} }
return changed
newZones(y)(x) = zone
} }
def copyZones(): Array[Array[Int]] = { def copyZones(): Array[Array[Int]] = {