-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day20.kt
72 lines (59 loc) · 2.82 KB
/
Day20.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package aoc.puzzles
import aoc.Point
import aoc.toInt
import java.util.*
/**
* @author Kris | 20/12/2021
*/
object Day20 : Puzzle<TrenchMap, Int>(20) {
private const val LIT_BIT = '#'
private const val PADDING = 1
private val NEIGHBOUR_RANGE = -1..1
private val NEIGHBOUR_POINTS = NEIGHBOUR_RANGE.flatMap { x -> NEIGHBOUR_RANGE.map { y -> Point(x, y) } }
override fun List<String>.parse(): TrenchMap = drop(2).toTrenchMap(first().toImageEnhancement())
private fun String.toImageEnhancement(): ImageEnhancement = ImageEnhancement(length).apply {
forEachIndexed { index, c -> if (c == LIT_BIT) set(index) }
}
private fun List<String>.toTrenchMap(imageEnhancement: BitSet): TrenchMap {
require(all { it.length == size }) { "Grid has uneven sides." }
val grid = Grid(size * size)
forEachIndexed { x, line ->
line.forEachIndexed { y, char ->
if (char == LIT_BIT) grid[x, y, size] = true
}
}
return TrenchMap(imageEnhancement, size, grid)
}
private fun TrenchMap.enhance(litBorder: Boolean): TrenchMap {
val nextSize = size + PADDING * 2
val nextGrid = BitSet(nextSize)
for (x in 0 until nextSize) {
for (y in 0 until nextSize) {
nextGrid[x, y, nextSize] = imageEnhancement.get(getPixelValue(Point(x - PADDING, y - PADDING), litBorder))
}
}
return TrenchMap(imageEnhancement, nextSize, nextGrid)
}
private operator fun Grid.get(x: Int, y: Int, size: Int, litBorder: Boolean): Boolean =
if (x < 0 || x >= size || y < 0 || y >= size) litBorder else this[x, y, size]
private fun TrenchMap.getPixelValue(center: Point, litBorder: Boolean): Int = NEIGHBOUR_POINTS.fold(0) { acc, offset ->
acc shl 1 or grid.pointToBit(center.merge(offset), size, litBorder)
}
private operator fun Grid.get(x: Int, y: Int, size: Int) = get(x * size + y)
private operator fun Grid.set(x: Int, y: Int, size: Int, value: Boolean) = set(x * size + y, value)
private fun Grid.pointToBit(point: Point, size: Int, invert: Boolean): Int = get(point.x, point.y, size, invert).toInt
private fun TrenchMap.getLitPixelCount(enhancements: Int): Int {
val sequence = generateSequence(false to this) { (invert, map) ->
!invert to map.enhance(map.litBorder && invert)
}
val (_, map) = sequence.drop(enhancements).first()
return map.grid.cardinality()
}
override fun TrenchMap.solvePartOne(): Int = getLitPixelCount(2)
override fun TrenchMap.solvePartTwo(): Int = getLitPixelCount(50)
}
private typealias ImageEnhancement = BitSet
private typealias Grid = BitSet
data class TrenchMap(val imageEnhancement: ImageEnhancement, val size: Int, val grid: Grid) {
val litBorder get() = imageEnhancement.get(0)
}