Skip to content

Instantly share code, notes, and snippets.

@nagat01
Created May 1, 2016 13:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nagat01/bd4897d3ca3814b6decdd2b0732eed57 to your computer and use it in GitHub Desktop.
Save nagat01/bd4897d3ca3814b6decdd2b0732eed57 to your computer and use it in GitHub Desktop.
let Size : Int = 40
var cells = [Array<Bool>]()
func min(a:Int, b:Int) -> Int {
if a > b {return b } else { return a }
}
func max(a:Int, b:Int) -> Int {
if a > b { return a } else { return b }
}
for y in 0 ... Size {
var xs = [Bool]()
for x in 0 ... Size {
var isAlive = false
switch y {
case 19: isAlive = x == 20
case 20: isAlive = 19 <= x && x <= 21
case 21: isAlive = x == 19
default: ()
}
xs.append(isAlive)
}
cells.append(xs)
}
for _ in 0 ... 100 {
var nexts = [Array<Bool>]()
for y in 0 ... Size {
var xs = [Bool]()
for x in 0 ... Size {
var neighbors = 0
for yy in max(0, y-1) ... min(y+1, Size) {
for xx in max(0, x-1) ... min(x+1, Size) {
if yy != y || xx != x {
if cells[yy][xx] {
neighbors += 1
}
}
}
}
var isAlive = false
if cells[y][x] {
isAlive = 2 <= neighbors && neighbors <= 3
} else {
isAlive = 3 == neighbors
}
xs.append(isAlive)
}
nexts.append(xs)
}
cells = nexts
}
for y in 0 ... Size {
var line = ""
for x in 0 ... Size {
line += (cells[y][x] ? "*" : " ")
}
print(line)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment