Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.yoavst.testing
- fun main(args: Array<String>) {
- val input = generateSequence { readLine()?.takeIf { it != "stop" }?.split(',')?.let { (y, x) -> x.toInt() to y.toInt() } }
- var data = List(dim * dim) { Pair(it % dim, it / dim) in input }
- var results = data.count { it } to 0
- for (gen in 1..50) {
- data = data.mapIndexed { i, _ -> moves.count { (dx, dy) -> data[fixed(i.x() + dx) + fixed(i.y() + dy) * dim] } in 3..5 }
- data.count { it }.let {
- if (it > results.first) results = it to gen
- println("gen $gen, alive: $it")
- }
- }
- println("best gen is ${results.second}")
- }
- const val dim = 10
- val moves = arrayOf(1 to 1, 1 to 0, 1 to -1, -1 to 0, -1 to 1, -1 to -1, 0 to 1, 0 to -1)
- inline fun fixed(num: Int) = if (num >= dim) 0 else if (num < 0) dim - 1 else num
- inline fun Int.x() = this % dim
- inline fun Int.y() = this / dim
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement