-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay21.kt
53 lines (40 loc) · 1.28 KB
/
Day21.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
import kotlin.math.abs
private fun readMap(): Pair<List<List<Boolean>>, Index2> {
val lines = mapLines { it.toList() }
var start = Index2(0, 0)
val map = lines.mapIndexed { i, list ->
list.mapIndexed { j, c ->
if (c == 'S') start = Index2(i, j)
c == '#'
}
}
return map to start
}
private fun getReachable(map: List<List<Boolean>>, start: Index2, steps: Int): Long {
val visited = mutableSetOf<Index2>()
val q = ArrayDeque<Index2>()
q += start
var even = 0
var odd = 0
var other = 0
while (q.isNotEmpty()) {
val index = q.removeFirst()
if (map.getOrNull(index) != false || index in visited) continue
visited += index
val distance = abs(index.i - map.size / 2) + abs(index.j - map.size / 2)
when {
distance > map.size / 2 -> other++
distance % 2 == 0 -> even++
else -> odd++
}
q += Direction.entries.map { index + it.index2 }
}
val x = steps.toLong() / map.size
return (x + 1) * (x + 1) * odd + x * x * even + x * (x + 1) * other
}
fun main() {
val (map, start) = readMap()
val first = getReachable(map, start, 64)
val second = getReachable(map, start, 26501365)
println("$first $second")
}