Skip to content

Commit

Permalink
[DAY 20]
Browse files Browse the repository at this point in the history
  • Loading branch information
n3o59hf committed Dec 20, 2021
1 parent 89a190c commit 1295159
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/main/kotlin/lv/n3o/aoc2021/Task.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ abstract class Input {
}
}.toMap()

fun asBlocks(): List<Input> = input.replace("\r", "").split("\n\n").map { StringInput(it) }

fun asLinesPerBlock(trim: Boolean = true): List<List<String>> = input.replace("\r", "").split("\n\n").map {
it.split("\n").let { block ->
if (trim) block.map(String::trim) else block
Expand All @@ -43,6 +45,10 @@ class ClasspathInput(val name: String) : Input() {
}
}

class StringInput(val data: String) : Input() {
override fun readInput() = data
}

abstract class Task(val input: Input) {
var debugListener: DebugListener? = null

Expand Down
1 change: 1 addition & 0 deletions src/main/kotlin/lv/n3o/aoc2021/main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ val testCases: List<TestCase> = listOf(
ci(17, "5253", "1770"),
ci(18, "4480", "4676"),
ci(19, "308", "12124"),
ci(20, "5259", "15287"),
)

fun main() = runBlocking {
Expand Down
41 changes: 41 additions & 0 deletions src/main/kotlin/lv/n3o/aoc2021/tasks/T20.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package lv.n3o.aoc2021.tasks

import lv.n3o.aoc2021.Input
import lv.n3o.aoc2021.Task
import lv.n3o.aoc2021.coords.C2
import lv.n3o.aoc2021.get

class T20(input: Input) : Task(input) {
private val data = input.asBlocks()
private val decoder = data.first().raw().toList().map { it == '#' }
private val initialState = data.last().asCoordGrid().filterValues { it == '#' }.keys.let { pixels ->
val size = pixels.maxOf { it.x.coerceAtLeast(it.y) } + 1
Image(BooleanArray(size * size) { i -> pixels.contains(C2(i % size, i / size)) }, size)
}

inner class Image(val pixels: BooleanArray, val size: Int, val borderPixel: Boolean = false) {
val pixelsOn get() = if (borderPixel) Int.MAX_VALUE else pixels.count { it }
fun enhance() = Image(
BooleanArray((size + 2) * (size + 2)) { c ->
val x = (c % (size + 2)) - 1
val y = (c / (size + 2)) - 1
var index = 0
for (j in y + -1..y + 1) {
for (i in x - 1..x + 1) {
index = index.shl(1)
if (if (j < 0 || j >= size || i < 0 || i >= size) borderPixel else pixels[i + j * size]) {
index++
}
}
}
decoder[index]
}, size + 2, if (borderPixel) decoder[0b111111111] else decoder[0]
)
}

private val enhancements = generateSequence(initialState) { it.enhance() }

override fun a() = enhancements[2].pixelsOn

override fun b() = enhancements[50].pixelsOn
}
4 changes: 3 additions & 1 deletion src/main/kotlin/lv/n3o/aoc2021/utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -210,4 +210,6 @@ fun <T> prioritizingComparator(priority: List<T>, fallbackComparator: Comparator
bIndex < 0 -> -1
else -> aIndex - bIndex
}
}
}

operator fun <T> Sequence<T>.get(index: Int) = drop(index).first()
102 changes: 102 additions & 0 deletions src/main/resources/i20.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
##..##...#..#.#...#.....##....#.#..#.#.####...#####..#.######...#......#.##.#..######.########....#.#..##.####.##...##..#.########.#.##..........##.######.#......#..#...##..#..#.###.#.#..#..#...##.###.....#.#.###..##.####....##.#....#.#.###..###.....####..###..##.#.#..##....#.#....#####.##.....#.#.#..###..#.....####..##.#..#.###....#...##..###.#.###.##.####..#.##......##.##.#.##..##...##..######..####.#.##..###..###.###.##.##..###..#.......##.#######.#..##..##.###.#.#.#.####...####..#.#.#.......##.##.#.....

.#####..###.#.##.#..#.#.###..#......##.####.####.....##..#..#..#.#.##..##..##..#...##..#####....#.#.
..###..##..##.#.##.###..#.##..##.########..#...####.#..#.#.#...##.##.##.#....####...#......###.#.#..
###.###.###.#..#.#.#.##.#..#.######.##.#..##...#.#...##......#.###...###.#.#.#..##...######.###..#..
.######..##.#.##.####.....#..###.##..###....##..#.#.....#.##.....##.####.#...##..###...##...#.##.#.#
##...#.#.##.#.###...#.##.#..##.#.##.##.#####...#.##..#..###.#.##...##...#.###.###.#.#.##.##..#......
..##....#.##.#....#.####.#.#...####..##...#..#.##.##.#.###.#..##.#..##......#####.#.###..#.##..##..#
..##.....#..#.#####.....#..####...###.#..#.##.#....#.##.#...####.##.....##........#...###.#.#.##.#.#
##..##.........##.#..##..#.####.#.........#.##.#..####..####.....#....#.#..##.###...#..#.#...#.#..##
....###.###.#....##.#...#..####..##.###.###.#.####.##.##.#####..####.####.#...#.#.#.....#.##.##.###.
#...#.#.###...#...#.#..##..#.##.#.##.##...#.#.#..#.##..#.#.....####...##.#####....#.#..#...#...##.#.
#.#...###....##..##...###..#..#####.#.###...##......##..###.##.#.#...#...#.#...###..#.####..#.......
.###....##.##...###.###.#.#.#.##..#..#..###..#..#.#..#.#....##....#.#.#.#....#.#.#..#.#.#..##..#....
###..#..##.###.#.####..####........#.#.##..##..#...#...#...##.##.#.....#.#..#..#..#.#...##...###.###
..##.#......#...###...###########.###..#.#..###....###..##.###..#....#######.#..###.##..###......###
..###..#.#.#.#.....####.#.#..#..#.####..#..#.#.##.##########.##.#.#.#....########.##...#..#..#.#..##
###....###.#.#....###.##...#..##.#..##...####....####.####...##..##.....#####.#......###.##..#..#...
#.###.#.#..#...#..#..#...#...#......#..###.####...##.#.###.....###.###..#...#...####.##..#.###.#.#..
##.###.##..##..#..#...####......##.#.#.#.###.#....##..###.#.##.#....#####..##..........#.##.#..#.#.#
.#.#.#.#####..##...#####..#####..###.#######....#..#.####..#.#..##....##.#..#.....#..###.##.#...#.#.
.#...#..#.....##....#.#####.##...###..#####..#.#####.#.#..####...#..###...#.###..###....#..#.##.....
#..##.##...###.##.#.#....##.....#.#.####...##.#.#..###....##.#..#..#.####.#.#.##.##.#.####.##.###.#.
.#..#.###...#..###.....####..##.##.....##.....#......#######...#.###..#.##.##....##.##.####..#######
#..#.#####.#.#.#..#.#.##..##.###....#####.##.##..#####.#..###..#...#.##........####...##..###.#...##
#.##.#..#...########.....##.####.#...##.#.##...#..##.#.#..##.....###..##...#######....####..##.#....
####.....####.##.##.##.######...#.####.#.#.#.#..##.##.#..####.#.#..#.##...#.#...##########..#.##..##
.##.#######.###.####..#....##...###..#.#.#..##....#......#.#...#.#..###..###...##.#######.#.####..##
..#.#......#...#######.#..#...######.#.#.#.#...##.#....######.#..#####.##.......#.#####....#####...#
####.####.##..#..#..###..###....#...###..#..#..#.##.##..##.##..###.####.####.##..##...###..##..#.###
.##.#......###.####..###..#....#..##.#.#.#.##.#...#.##...##.....#.######..######.##.#...####..###...
##.####.#.#..##..#.########.....#.##..#..#....#.#.#..#..#.#...##..#.#...##...#####.#...####.#######.
##.#..#.###.....#.##....#.#.#...#####.#.#######..##.#..#####.####..#.#.##.#..#...##.##.######.##..##
##..#.##..###...###..###....###.#....#.##...#.#.###......####.###..#######.#.#........##...#.#.##..#
#..##.#..#..####....#.###...#.#...##.#..#.#.####.#..##.###.##.##..#.##..#.##.##.#..#.....##....#..#.
#######.#.#.###.#####.....#.#..###..####.#...#...###..####.#..#.#.#.#.#...##...#.####.#.#.....#.####
##.###...####..##.#..#.#.##.#....#.###.###.####..####..####....###.###.##..###.#.##..##.#..#######.#
...###...#....######....###..#..#..##..###...###..###.####.####....##.#..#.#.#.#..#####.##.#..##..#.
#.....########.####.#....#.#####.#.....###.#.##..##...####.#.#.#.##..##.##.#.##.#####..#..#.##.##.#.
#..#.#.#......#.#.####.#.###.#.#.##..##.##.#...#..#.##..#.##.#..##.#...####.#.####.##.#......##.#.#.
####.#..#.##.######....##.....#.######.....###...##.##.....##.#...##..........#.#.##.##..#.##..#####
...###..##..#.#..####.#...#..#..##.#..##.#..####...#..#..##.#...#.##...##...###..#.####.......#..##.
.#.###.....#.####..#..#.##....###.##..##.#####.#...###.##.##..##.####.#..###.#####.##....#.##..###..
.#..##....##..#.##.##..##.....#..##.#.#....#..##.#..#.###..#.......###.##.###....####...##.#####..##
.#.##.#.#....####..####.#......#.....#####.#.####...#.##.#...###.#.#..##.#.##.#.##..##.#######...#..
##..###.#..#.#..#...###.....#.#....##.##.#.##.##.#..######...#..#..#.......###.#.####...#.######...#
#...###..#......#..#...#....###.....##.##.##.##....#####.##....##.##.#....##..#...##..#.##..#.#.#..#
.....#.#.###.#.####..#....####.#####......##.##..####.#.#.#....#..#....######.#.###...###..###..#...
.#.#.#.#....#.#.....##..#...##.##.#..###.#.#.#.###......#.############.#............#..#..##.......#
#.##.#..####.##.##..##..#.####.#...##.##..#####.###..##..#.######.#.###....#.##..##..#....#..####...
#..###...###..#.##....#......##...###...##..#..#..#.#.#.#.##....#......##..##.#.#.####..##..#..#.##.
#.#.....##...#...#####...#.#####....#..###..#..#..####.##.#..##..#.###..#...#.#.#.##..##.#.....##.#.
..#.###..##.###..###.....#.###.#######.#.....#.#####.#...####.....#...#######..#.#.#.#..##.##.#.##..
####...#.#..#.##..#.##....#..####..#..####...##.#....###..#..#..#.#.###...#..#####...#.##.##.##..#..
.#.#..##.######..##.#.##...##...#.........#..#.###....#..#....####.##....#####..#....##.####..#.####
.#..##....#..##....#.###.######.#..#..#.###.####.#.#.#..####...###.##.##..#..#...#..#.###.##.###.###
......###.#..#..#..#.#.#.#...#.#..#...#....#.##..#.##.#.....##....####.#..##..##....#..#.#.##.#..##.
#..##.##..#######.#...##....#..###.#.##.###.##.###..#.###.#...#.#...##.#.###....###.#.##.#.#....#.##
#...####..######...####.#.##..###..#.#####.##...#.####.#.#....####.#..#.......###...##.#..#..##...##
##..#..##.#.###.#.#..#.#.###..#.......###..#.#.#.###........#...#.#..#.....##.####...#####.###....##
...#...#######.#..#########.#.#..#..###..#.#..#######.#.#....#.#.##.###.###.#######.####.....##.#.#.
..#...#####.#.##...##...#.#.##.#.###.#..###.#.#...##.#..#.#..###.#..#.#....#.###..###.#..##.....####
.#.###.#.#..#.####..#.###....##..#.#...##..#.###.#.#.##..#.#.#.....##.#...##.##..#.....#..##.#.###.#
#..#.###.##..#..###....#...#.#.###..#....####.##.##.#.#...#..#..####.##.##.##.###...####...####....#
.#.#...##.#.##.....#.#.##..#.#...#.#.#...##.....##.#...#####...##..##...#..##.##.##.#.##.##.....##.#
.######.###..#.#.######.###.#.#...#..#.#.#..#..##......####.##...#..#.#.#.###..####.##.######.######
.##.##...#......##.#....######.##.###.####...##...##.#.#.#.#.##...#######..#....##.#..#....###......
.#.##.###....#..#..#...###...#...#.#.....###.##.###.#........##.#.##.###..##.#####.##..##.##.#.#.#.#
..##..####.##.#..###.##.#..###.###.#####.####.#.##.####..#...#..#.###.#.#....#.##..##.#..##....##..#
.....#.#..##...#.#..##.#..#.#########..##.##.#...###..######...#.#.....#...###.##..##..#.#.......#.#
..#.#.#.#...###..####.###.##..#.##.###..#.#...##.....#.#..###.#..#.###..#.###..###.#.##...####.#....
#..#.###..#.#.##.#..##.###...##..............##.###.#.##...#.##..#...##........#.#.###.##....#.#.##.
##....####.##.#......###..#.....#.....#..####.....#..#.#.####...###...#...#.#...####..#..#..#..###..
.#....#..#####....#.#.#.#...#....##....##.#..###.#....#.#######.#.####.##...##..#....#......#.#.#.##
#.##....#####.#######...#.####.#..#..##..#.######..####..####..#..#...#.#.###.###....#..#....#.#####
###.#####...#.##.#.#####.#.#################.#..#..##.##...#.#...###..#.#.#.##.##..###.#..##.##..###
.#.##.##.###.#.##.#.###.##....#.....##..##...##.#########.##.##.#.###..##..#..#.#####..###.#...#..##
#.#....###..#.###..#...#..#.###.#.##..#..#.##...#...#..##..###..######....#.##.#..#.........##.#.##.
#.#.##.#.#####.#.#..#..###..#...###.###..##.##...##....#..##..##....#....#...##.#####..###..#..####.
#.#..##..##..##..##...####.#..#.####.##.#.##..#....###.#.####.####..#.###.###.#.#..#.#...#.#..#.....
#.#...####..#######.#.##..#.#......##.....######..#..##...##.#..#.#####.#..#..#....#.#..#####.#...##
.#.#.######.##....#..###.#...#.#....#.......#..#.#.#..#....##.#..###..##..#.#......####.#.#.##...##.
##..###..####...###.####...#.###....#.#######.##.####.#.....#.#.###.####.####...#.#.....#....##..###
..#.#.#.#.###...##.##..##..#.######..###..#...#......##.#.###....#.###...#.....#....####..###.##.#.#
.###..##..###.....##...#......####.##.#....###..#.##.#..#.##.##..#.....#.#.##..##.####..###.#...#..#
.#....####..##.#...###.#..#..###..##.#.#.....#...#.###..####..######.#.#...#...#.#...##.....###.....
.....#.#...#...#..#.###....##.##.#.#.#.#.##...##.#.#.....#.###.#.#.##.######.....#.###.###.#.##.###.
.####....#..###.#####.#.#..#.#.#..##.....##.#.#.#...#####..##.#.......###.###.#.##.#..##.#.###.#.#.#
##.#.#......#####..#.##.##..###..##..###.#..##..##.#.###.....#...####......#..###....#..##.#..#....#
#.#.#####...##.##.#...##.###..######.###.#.##.##..#..#...##..##.###...#.#..##.#########.#####.#.##.#
.#.##....#..#..#..#..##...#......#..#.#.##...##...#.###.##.......####...#.#..#.#...##.#..#.####..##.
.#.######.#.##...#####.###..#..##....##.#####.###..######.#...###.#.##..##....###..####.##.#.####.##
.#.#....#.##..##...#########...#..#.#.######.#.#.##.#....####.#...#.##..#..#.#.#..#.#.##..#.#...#.#.
.####...#...#..##.#.#.###.####...#.#...#####.....###.####.#.##..###..#.##.#...#.#####.####..##.##.#.
....#..#...#.###..##.##..#..####.#.#...#.#####..##...#####.#.#...###..#...#.#.###.#.#.##..###..###.#
#####.........##..###.#.###.#..........#.#.#..#.#..#.#.#######.#....##....#.###..#.#..##.#...##....#
.....###.##..#....#..#.#...#.##.#..#.#####.####.##.#.#.###........##..#.#..#.....###......##..##.#..
.###....#.#....#......##.##....##....#.#.##.######.#.##..##.###...#....#..#..#..###..##.#.#.##.#.#.#
##....#.##.#..#.###..##.##..#.#..###.##........##.#.#..#..####...####.#...#....#.#.##..#...#.####.#.
....###.##.#####.#....##.##.##.#..#.#..#.##.##.#.####.##..#.##.#...##..####..#..#.#.##.##...##.#....
..###........####.#....##.##..##..##.#....###..#.#.###......#.#####.###...##..##..#..#.#.###.#.#..#.
....#.###.##.....#..#.#.#.#####.###.#.##.#.##.......#.###.#.##..###........#.#.###.####.##.#....#.#.

0 comments on commit 1295159

Please sign in to comment.