-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathy24day12.kt
More file actions
115 lines (108 loc) · 4.81 KB
/
Copy pathy24day12.kt
File metadata and controls
115 lines (108 loc) · 4.81 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import kotlin.time.measureTimedValue
fun main() {
data class Pos(val i: Int, val j: Int)
operator fun Pos.plus(that: Pos) = Pos(i + that.i, j + that.j)
operator fun Pos.minus(that: Pos) = Pos(i - that.i, j - that.j)
fun Pos.adjacentPositions() =
sequenceOf(Pos(i - 1, j), Pos(i, j + 1), Pos(i + 1, j), Pos(i, j - 1))
fun Pos.adjacentDiagPositions() =
sequenceOf(Pos(i - 1, j - 1), Pos(i - 1, j + 1), Pos(i + 1, j - 1), Pos(i + 1, j + 1))
fun Pos.opposite(from: Pos) = this + (this - from)
fun Pos.turn(step: Int = 1) =
sequenceOf(this, Pos(j, -i), Pos(-i, -j), Pos(-j, i)).elementAt((step + 4) % 4)
fun Pos.turn(vec: Pos, step: Int = 1) = this + vec.turn(step)
fun Pos.turnFrom(from: Pos, step: Int = 1) = from.turn(from - this, step)
fun Pos.behindPositions(from: Pos) =
sequenceOf(from.turn(this - from, -1), from, from.turn(this - from, 1))
fun Pos.aheadPositions(from: Pos) = behindPositions(opposite(from))
fun List<String>.at(pos: Pos) = getOrNull(pos.i)?.getOrNull(pos.j)
fun <T> Array<Array<T>>.at(pos: Pos) = getOrNull(pos.i)?.getOrNull(pos.j)
data class Region(val type: Char, var area: Int = 0, var perimeter: Int = 0)
val map = generateSequence { readlnOrNull() }.toList()
fun regionScores(perimeterDelta: (pos: Pos, from: Pos, isSameRegion: Pos.() -> Boolean) -> Int): Int {
val regions = mutableListOf<Region>()
val regionMap = Array(map.size) { Array<Region?>(map[it].length) { null } }
fun visit(pos: Pos, from: Pos): Boolean {
if (regionMap[pos.i][pos.j] != null) return false
val region = regionMap[from.i][from.j] ?: Region(map.at(pos)!!).also { regions += it }
regionMap[pos.i][pos.j] = region.apply {
area += 1
perimeter += perimeterDelta(pos, from) { region === regionMap.at(this) }
}
return true
}
map.indices.forEach { i ->
map[i].indices.forEach { j ->
val queue = ArrayDeque<Pos>()
queue += Pos(i, j).also { visit(it, it) }
while (true) {
val pos = queue.removeFirstOrNull() ?: break
val cell = map.at(pos)!!
for (next in pos.adjacentPositions()) {
if (map.at(next) == cell && visit(next, pos)) queue += next
}
}
}
}
return regions.sumOf { it.area * it.perimeter }
}
measureTimedValue { // part 1
regionScores(perimeterDelta = { pos, _, isSameRegion ->
when (pos.adjacentPositions().filter { it.isSameRegion() }.count()) {
0 -> 4
1 -> 2
2 -> 0
3 -> -2
4 -> -4
else -> error("Unreachable")
}
})
}.also(::println)
measureTimedValue { // part 2
regionScores(perimeterDelta = { pos, from, isSameRegion ->
fun Pos.countBy(supplier: Pos.() -> Sequence<Pos>) =
supplier().filter { it.isSameRegion() }.count()
when (pos.countBy { adjacentPositions() }) {
0 -> 4
1 -> when (pos.countBy { behindPositions(from) }) {
1 -> 0
2 -> 2
3 -> 4
else -> error("Unreachable")
}
2 -> {
if (pos.opposite(from).isSameRegion()) {
when (pos.countBy { adjacentDiagPositions() }) {
0 -> -4
1 -> -2
2 -> 0
3 -> 2
4 -> 4
else -> error("Unreachable")
}
} else {
val (index, nonEmptyPos) = from.aheadPositions(from.opposite(pos)).withIndex().first { it.value.isSameRegion() }
val step = index - 1
when (pos.countBy { sequenceOf(turnFrom(nonEmptyPos, step), turnFrom(opposite(nonEmptyPos), step)) }) {
0 -> -2
1 -> 0
2 -> 2
else -> error("Unreachable")
}
}
}
3 -> {
val emptyPos = pos.adjacentPositions().first { !it.isSameRegion() }
when (pos.countBy { behindPositions(emptyPos) }) {
0 -> -4
1 -> -2
2 -> 0
else -> error("Unreachable")
}
}
4 -> -4
else -> error("Unreachable")
}
})
}.also(::println)
}