import java.io.File import kotlin.math.roundToLong fun dirToCoords(index: Int) = listOf(1L to 0L, 0L to 1L, -1L to 0L, 0L to -1L)[index] fun main(args: Array){ val file = File("../puzzle_22.txt") // val file = File("test.txt") val lines = file.readLines() val coordMap = mutableMapOf, Int>() lines.forEachIndexed { y, string -> string.toCharArray().forEachIndexed { x, c -> coordMap[x.toLong() to (lines.size - 1 - y).toLong()] = if(c == '#') 2 else 0 } } 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 < 10000000) { val isInfected = coordMap.computeIfAbsent(x to y) { 0 } if(isInfected == 0) direction += 1 else if(isInfected == 1) {} else if(isInfected == 2) direction -= 1 else direction = (direction + 2) % 4 direction = (direction + 4) % 4 coordMap[x to y] = (isInfected + 1) % 4 if(coordMap[x to y] == 2) infectedCount++ val (dx, dy) = dirToCoords(direction) x += dx y += dy count++ } println(infectedCount) }