Skip to content

Commit a2edd49

Browse files
committed
Add day 18
1 parent 581fc6c commit a2edd49

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

2022/18/18.kt

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
data class Point(var x: Int, var y: Int, var z: Int) {
22
operator fun plus(o: Point): Point = Point(x+o.x, y+o.y, z+o.z)
33
fun adjacent(): List<Point> = dirs.map { this+it }.toList()
4+
fun min(): Int = minOf(x, y, z)
5+
fun max(): Int = maxOf(x, y, z)
46
}
57

68
val dirs = listOf(
@@ -12,26 +14,24 @@ fun main() {
1214
val input = generateSequence(::readlnOrNull)
1315
.map { it.split(",").map { it.toInt() } }
1416
.map { (x, y, z) -> Point(x, y, z) }.toList()
15-
val limitsX = input.minOf { it.x-1 }..input.maxOf { it.x+1 }
16-
val limitsY = input.minOf { it.y-1 }..input.maxOf { it.y+1 }
17-
val limitsZ = input.minOf { it.z-1 }..input.maxOf { it.z+1 }
17+
val bounds = input.minOf { it.min()-1 }..input.maxOf { it.max()+1 }
1818

1919
// Part 1
2020
input.sumOf { it.adjacent().count { !input.contains(it) } }.run(::println)
2121

2222
// Part 2
23-
val queue = ArrayDeque<Point>()
24-
queue.add(Point(limitsX.first, limitsY.first, limitsZ.first))
25-
var count = 0
23+
val queue = mutableListOf(Point(bounds.first, bounds.first, bounds.first))
2624
val visited: MutableSet<Point> = mutableSetOf()
25+
var count = 0
26+
2727
while (queue.isNotEmpty()) {
28-
val point = queue.removeFirst()
28+
val point = queue.removeAt(0)
2929
if (point in visited) continue
3030
visited.add(point)
3131
for (adj in point.adjacent()) {
3232
if (adj in input)
3333
count++
34-
else if (adj.x in limitsX && adj.y in limitsY && adj.z in limitsZ)
34+
else if (adj.x in bounds && adj.y in bounds && adj.z in bounds)
3535
queue.add(adj)
3636
}
3737
}

0 commit comments

Comments
 (0)