Advent of Code 2021 day 20
12/20/2021, 5:00 AMilya.gorbunov
12/20/2021, 5:31 AM#
in the beginning of the algorithm line and .
in the end as opposed to the example input?David Whittaker
12/20/2021, 5:55 AMMichael Böiers
12/20/2021, 6:27 AMDavid Whittaker
12/20/2021, 6:44 AMephemient
12/20/2021, 7:04 AMnkiesel
12/20/2021, 9:01 AMDan Fingal-Surma
12/20/2021, 9:09 AMnkiesel
12/20/2021, 9:27 AMDan Fingal-Surma
12/20/2021, 9:53 AMDan Fingal-Surma
12/20/2021, 9:55 AMMichael Böiers
12/20/2021, 10:28 AMJakub Gwóźdź
12/20/2021, 10:52 AM#
and .
to represent colors, when you can run Kotlin on JVM and use BufferedImage…
val light = Color.WHITE
val dark = Color.BLACK
fun color(c: Char) = when (c) {
'.' -> light
'#' -> dark
else -> error("wtf '$c'")
}
operator fun BufferedImage.get(row: Int, column: Int): Int {
val outside = getRGB(0, 0)
return (row - 1..row + 1).map { y ->
if (y !in 0 until height) listOf(outside, outside, outside)
else (column - 1..column + 1).map { x ->
if (x !in 0 until width) outside
else getRGB(x, y)
}
}
.flatten()
.fold(0) { acc, i -> acc * 2 + if (i == dark.rgb) 1 else 0 }
}
fun BufferedImage.enhanced(alg: List<Color>) =
BufferedImage(width, height, type).also { result ->
(0 until width).forEach { x ->
(0 until height).forEach { y -> result.setRGB(x, y, alg[this[y, x]].rgb) }
}
}
fun BufferedImage.count(): Int = (0 until width).sumOf { x ->
(0 until height).count { y -> getRGB(x, y) == dark.rgb }
}
val border = 50
fun renderAsBitmap(data: List<String>): BufferedImage {
val image = BufferedImage(
border + data[0].length + border,
border + data.size + border,
BufferedImage.TYPE_BYTE_INDEXED
)
(0 until image.width).forEach { x ->
(0 until image.height).forEach { y -> image.setRGB(x, y, light.rgb) }
}
data.forEachIndexed { row, line ->
line.forEachIndexed { col, c -> image.setRGB(col + border, row + border, color(c).rgb) }
}
return image
}
fun part1(input: List<String>): Int {
val alg = input.first().map { color(it) }
var image = renderAsBitmap(input.drop(2))
repeat(2) { image = image.enhanced(alg) }
return image.count()
}
fun part2(input: List<String>): Int {
val alg = input.first().map { color(it) }
AnimGif(File("src", "Day20.gif")).use { animGif ->
var image = renderAsBitmap(input.drop(2))
.also { animGif += it }
repeat(50) { r ->
image = image.enhanced(alg)
.also { if ((r % 2) == 1) animGif += it }
}
return image.count()
}
}
Michael de Kaste
12/20/2021, 12:39 PMMichael de Kaste
12/20/2021, 12:39 PMJakub Gwóźdź
12/20/2021, 12:41 PMhttps://github.com/jakubgwozdz/advent-of-code-2021-in-kotlin/raw/main/src/Day20.gif▾
Jintin
12/20/2021, 2:56 PMEdgars
12/20/2021, 6:04 PMDavid Whittaker
12/20/2021, 6:05 PMEdgars
12/20/2021, 6:08 PMnkiesel
12/20/2021, 6:24 PM./gradlew test --tests Day20
for my repo finishes in 1.2 secondsMarcin Wisniowski
12/20/2021, 10:26 PMphldavies
12/20/2021, 11:57 PMphldavies
12/20/2021, 11:57 PMphldavies
12/21/2021, 12:35 AMtoString()
for my image using the 2x2 block chars to make the output compact than I did on actually solving the problem 🙂