Advertisement
Guest User

Untitled

a guest
Feb 25th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.88 KB | None | 0 0
  1. package com.yoavst.testing
  2.  
  3. fun main(args: Array<String>) {
  4. val input = generateSequence { readLine()?.takeIf { it != "stop" }?.split(',')?.let { (y, x) -> x.toInt() to y.toInt() } }
  5. var data = List(dim * dim) { Pair(it % dim, it / dim) in input }
  6. var results = data.count { it } to 0
  7. for (gen in 1..50) {
  8. data = data.mapIndexed { i, _ -> moves.count { (dx, dy) -> data[fixed(i.x() + dx) + fixed(i.y() + dy) * dim] } in 3..5 }
  9. data.count { it }.let {
  10. if (it > results.first) results = it to gen
  11. println("gen $gen, alive: $it")
  12. }
  13. }
  14. println("best gen is ${results.second}")
  15. }
  16.  
  17. const val dim = 10
  18. 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)
  19. inline fun fixed(num: Int) = if (num >= dim) 0 else if (num < 0) dim - 1 else num
  20. inline fun Int.x() = this % dim
  21. inline fun Int.y() = this / dim
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement