fun bigIntKnotHash(input: String) = BigInteger(knotHash(input), 16) fun main(args: Array) { println((0 until 128).map { bigIntKnotHash("$input-$it").bitCount() }.sum()) val grid = Grid((0 until 128).flatMap { x -> val int = bigIntKnotHash("$input-$x") (0 until 128).map { y -> Coord(x, y) to int.testBit(y) } }.toMap()) println((0 until 128).sumBy { x -> (0 until 128).count { y -> clearConnected(Coord(x, y), grid) } }) } fun clearConnected(start: Coord, grid: Grid): Boolean { if (!grid[start]) return false grid[start] = false listOf(-1 to 0, 1 to 0, 0 to 1, 0 to -1) .map { Coord(start.x + it.first, start.y + it.second) } .filter { grid[it] } .forEach { clearConnected(it, grid) } return true }