Skip to content

Instantly share code, notes, and snippets.

@ende76
Created December 21, 2021 14:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ende76/6a490b890f0891725dd423e1e27b7001 to your computer and use it in GitHub Desktop.
Save ende76/6a490b890f0891725dd423e1e27b7001 to your computer and use it in GitHub Desktop.
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