-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay18.kt
56 lines (50 loc) · 1.73 KB
/
Day18.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import java.util.LinkedList
import java.util.Queue
fun main() {
fun List<Int>.neighbors() = listOf(-1, 1).flatMap {
listOf(
listOf(get(0) + it, get(1), get(2)),
listOf(get(0), get(1) + it, get(2)),
listOf(get(0), get(1), get(2) + it)
)
}
fun part1(input: List<String>): Int {
val cubes = input.map { line -> line.split(",").map { it.toInt() } }
return cubes.sumOf { cube -> cube.neighbors().count { it !in cubes } }
}
fun part2(input: List<String>): Int {
val cubes = input.map { line -> line.split(",").map { it.toInt() } }
val inside = mutableSetOf<List<Int>>()
val outside = mutableSetOf<List<Int>>()
return cubes.sumOf {
it.neighbors().count { cube ->
if (cube in outside)
return@count true
if (cube in inside)
return@count false
val seen = mutableSetOf<List<Int>>()
val q: Queue<List<Int>> = LinkedList()
q.add(cube)
while (q.isNotEmpty()) {
val c = q.poll()
if (c in cubes || c in seen)
continue
seen += c
if (seen.size > 2000) {
outside += seen
return@count true
}
q.addAll(c.neighbors())
}
inside += seen
false
}
}
}
val testInput = readInputLines("Day18_test")
check(part1(testInput) == 64)
check(part2(testInput) == 58)
val input = readInputLines("Day18")
println(part1(input))
println(part2(input))
}