import java.io.File import kotlin.math.roundToLong fun main(args: Array){ val file = File("../puzzle_22.txt") // val file = File("test.txt") val lines = file.readLines() val coordMap = mutableMapOf, Boolean>() lines.forEachIndexed { y, string -> string.toCharArray().forEachIndexed { x, c -> coordMap[x.toLong() to (lines.size - 1 - y).toLong()] = c == '#'} } var x = Math.ceil(coordMap.keys.maxBy { it.first }!!.first.toDouble() / 2).roundToLong() var y = Math.ceil(coordMap.keys.maxBy { it.second }!!.second.toDouble() / 2).roundToLong() println(x to y) var direction = 1 var count = 0 var infectedCount = 0 while(count < 10000) { val isInfected = coordMap.computeIfAbsent(x to y) { false } if(isInfected) direction -= 1 else { direction += 1 infectedCount++ } direction = (direction + 4) % 4 coordMap[x to y] = !isInfected val (dx, dy) = dirToCoords(direction) x += dx y += dy count++ } println(infectedCount) }