1
1
data class Point (var x : Int , var y : Int , var z : Int ) {
2
2
operator fun plus (o : Point ): Point = Point (x+ o.x, y+ o.y, z+ o.z)
3
3
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)
4
6
}
5
7
6
8
val dirs = listOf (
@@ -12,26 +14,24 @@ fun main() {
12
14
val input = generateSequence(::readlnOrNull)
13
15
.map { it.split(" ," ).map { it.toInt() } }
14
16
.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 }
18
18
19
19
// Part 1
20
20
input.sumOf { it.adjacent().count { ! input.contains(it) } }.run (::println)
21
21
22
22
// 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))
26
24
val visited: MutableSet <Point > = mutableSetOf ()
25
+ var count = 0
26
+
27
27
while (queue.isNotEmpty()) {
28
- val point = queue.removeFirst( )
28
+ val point = queue.removeAt( 0 )
29
29
if (point in visited) continue
30
30
visited.add(point)
31
31
for (adj in point.adjacent()) {
32
32
if (adj in input)
33
33
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 )
35
35
queue.add(adj)
36
36
}
37
37
}
0 commit comments