Skip to content

Instantly share code, notes, and snippets.

@mikezucc
Created May 22, 2017 01:28
Show Gist options
  • Save mikezucc/ebc83e2354dd1cf6bdca549e1c837fd4 to your computer and use it in GitHub Desktop.
Save mikezucc/ebc83e2354dd1cf6bdca549e1c837fd4 to your computer and use it in GitHub Desktop.
Conway game of life in swift with 10x10 doomed seed
#!/usr/bin/swift
// ubuntu peeps this ones for you
// run export PATH=~/swift3/usr/bin/:"${PATH}" in pwd first
import Foundation
// ********************* debug *********************
let showDebug = false
func printS(_ output: String) {
if showDebug { print(output) }
}
// ********************* debug *********************
// "onehundred" is coincidentally 10 letters long
let seed:[(Int, Int)] = [(1,2),(1,3),(1,4),(1,5),(2,3),(2,2),(2,1),(3,2),(3,4),(3,1),(4,4),(3,3),(7,3),(7,6)]
var earth:[[Int]] = {
var tray:[[Int]] = []
for idx in 0..."onehundred".characters.count {
var cubes:[Int] = []
for idy in 0..."onehundred".characters.count {
var initial = 0
for (inX, inY) in seed {
initial = (inX == idx && inY == idy ? 1 : 0) | initial
}
cubes += [initial]
}
tray += [cubes]
}
return tray
}()
func grid() -> [[Int]] {
var tray:[[Int]] = []
for _ in 0..."onehundred".characters.count {
var cubes:[Int] = []
for _ in 0..."onehundred".characters.count {
cubes += [0]
}
tray += [cubes]
}
return tray
}
// seed stage
func getPoint(_ earth: [[Int]], coordX: Int, coordY: Int) -> Int {
return (earth[coordX] as [Int])[coordY]
}
func setPoint(_ earth: [[Int]], coordX: Int, coordY: Int, newValue: Int) -> [[Int]] {
var perspective = earth
var list = perspective[coordX]
list[coordY] = newValue
perspective[coordX] = list
return perspective
}
func getChunkAsListLOL(_ earth: [[Int]], coordX: Int, coordY: Int) -> [Int] {
let startX = coordX-1 >= 0 ? coordX-1 : 0
let startY = coordY-1 >= 0 ? coordY-1 : 0
let endX = coordX+1 < earth.count ? coordX+1 : coordX
let endY = coordY+1 < earth[0].count ? coordY+1 : coordY
let cursorWalkX = endX-startX
let cursorWalkY = endY-startY
printS("chunking > looking at \(coordX),\(coordY), windStartX \(startX) - windStartY \(startY), windWalkX \(cursorWalkX), windWalkY \(cursorWalkY)")
var list:[Int] = []
for idx in 0...cursorWalkX {
for idy in 0...cursorWalkY {
let p = getPoint(earth, coordX: startX+idx, coordY: startY+idy)
list += [p]
}
}
return list
}
func lifeCycle(_ earth: [[Int]]) -> [[Int]] {
// define only qualifiers which should change state
// but should probably define smaller portion of truth cases
// qualifiers test sum of non-zero dist1 neighbors
let deadShouldBeAlive = [3]
let aliveShouldBeDead = [0,1,4,5,6,7,8,9,10]
var nextPhase:[[Int]] = grid()
for x in 0..<earth.count {
let tray = earth[x]
printS(tray.map({"\($0)"}).reduce("\tWorking on ", combine: +))
for y in 0..<tray.count {
let list = getChunkAsListLOL(earth, coordX: x, coordY: y)
printS(list.map({"\($0)"}).reduce("Radial \(x)x\(y) is ", combine: +))
let neighbors = list.reduce(0, combine: +)
let point = getPoint(earth, coordX: x, coordY: y)
var shouldChange = false
if point == 0 {
shouldChange = deadShouldBeAlive.contains(neighbors)
} else {
shouldChange = aliveShouldBeDead.contains(neighbors)
}
let possiblePointValue = (point == 0 ? 1 : 0)
printS("should change \(shouldChange ? "yes" : "no") to \(possiblePointValue)")
if shouldChange {
nextPhase = setPoint(nextPhase, coordX: x, coordY: y, newValue: possiblePointValue)
}
printS("\t***\tFinished \(x),\(y)")
}
printS("\t***\tFinished \(x)")
}
printS("finished lifecycle")
return nextPhase
}
func microscope(_ earth: [[Int]]) {
var buffer = ""
earth.forEach { (list) in
list.forEach { (point) in
buffer += "\(point == 1 ? "🙂" : "💀")"
buffer += " "
}
buffer += "\n"
}
print(buffer)
}
var alive = true //lol
while alive {
print("\n*** Hashing Life Cycle ***")
microscope(earth)
earth = lifeCycle(earth)
var volume = 0
earth.forEach { (list) in
volume += list.reduce(0, combine: +)
}
alive = volume != 0
if !alive {
print(" ____\n ,\' Y`.\n / \\\n \\ () () /\n `. /\\ ,\'\n8====| \"\" |====8\n `LLLU\'\n")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment