Created
December 21, 2021 14:54
-
-
Save ende76/6a490b890f0891725dd423e1e27b7001 to your computer and use it in GitHub Desktop.
Kotlin solution for https://adventofcode.com/2021/day/20
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private fun readChar(): Char = readString()[0] | |
private fun readChars(): CharArray = readString().toCharArray() | |
private fun readInt(): Int = readLine()!!.toInt() | |
private fun readInts(): List<Int> = readLine()!!.trim().split(" ").map(String::toInt) | |
private fun readLong(): Long = readLine()!!.toLong() | |
private fun readLongs(): List<Long> = readLine()!!.trim().split(" ").map(String::toLong) | |
private fun readString(): String = readLine()!!.trim() | |
private fun readStrings(): List<String> = readLine()!!.trim().split(" ") | |
fun rowIndex(i: Int, rows: Int, cols: Int) = i / cols | |
fun colIndex(i: Int, rows: Int, cols: Int) = i % cols | |
fun index(r: Int, c: Int, rows: Int, cols: Int) = r * cols + c | |
fun process(image: ByteArray, rules: ByteArray, rows: Int, cols: Int, outsidePixelValue: Byte) : ByteArray { | |
val output = ByteArray((rows + 2) * (cols + 2)) | |
for (r in -1 .. rows) { | |
for (c in -1 .. cols) { | |
var ruleIndex = 0 | |
for (rc in r - 1 .. r + 1) { | |
for (cc in c - 1 .. c + 1) { | |
ruleIndex = (ruleIndex shl 1) + if (rc !in 0 until rows || cc !in 0 until cols) outsidePixelValue else image[index(rc, cc, rows, cols)] | |
} | |
} | |
output[index(r + 1, c + 1, rows + 2, cols + 2)] = rules[ruleIndex] | |
} | |
} | |
return output | |
} | |
fun main() { | |
val iterations = 50 | |
val byteOne = 1.toByte() | |
val rules = readString().map { if (it == '.') 0 else byteOne }.toByteArray() | |
var input = mutableListOf<String>() | |
readString() // empty line | |
var nextLine = readString() | |
nextLine@ while (nextLine.isNotEmpty()) { | |
input.add(nextLine) | |
nextLine = readString() | |
} | |
var rows = input.size | |
var cols = input[0].length | |
var image = ByteArray(rows * cols) { if (input[rowIndex(it, rows, cols)][colIndex(it, rows, cols)] == '.') 0 else 1 } | |
for (iteration in 0 until iterations) { | |
image = process(image, rules, rows, cols, (rules[0].toInt() and (iteration % 2)).toByte()) | |
rows += 2 | |
cols += 2 | |
} | |
for (r in 0 until rows) { | |
println(image.slice(r * cols until (r + 1) * cols).map { if (it == byteOne) '#' else '.' }.joinToString("")) | |
} | |
println(image.filter { it == byteOne }.size) | |
} | |
/* | |
..#.#..#####.#.#.#.###.##.....###.##.#..###.####..#####..#....#..#..##..###..######.###...####..#..#####..##..#.#####...##.#.#..#.##..#.#......#.###.######.###.####...#.##.##..#..#..#####.....#.#....###..#.##......#.....#..#..#..##..#...##.######.####.####.#.#...#.......#..#.#.#...####.##.#......#..#...##.#.##..#...##.#.##..###.#......#.#.......#.#.#.####.###.##...#.....####.#..#..#.##.#....##..#.####....##...##..#...#......#.#.......#.......##..####..#...#.#.#...##..#.#..###..#####........#..####......#..# | |
#..#. | |
#.... | |
##..# | |
..#.. | |
..### | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment