Last active
July 11, 2018 19:26
-
-
Save acegreen/e16a2259a93dab880a7f to your computer and use it in GitHub Desktop.
Revisions
-
acegreen renamed this gist
Sep 20, 2016 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
acegreen revised this gist
Mar 9, 2016 . 1 changed file with 0 additions and 17 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -16,23 +16,6 @@ extension Array { } } // Groupon Interview Q2 /* -
acegreen revised this gist
Mar 9, 2016 . 1 changed file with 0 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,7 +1,5 @@ import UIKit import Foundation extension Array { -
acegreen revised this gist
Mar 9, 2016 . 1 changed file with 3 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -199,3 +199,6 @@ func game (array: [[Int]]) -> [[Int]] { print(game(gameGrid)) /* Output = [[0, 1, 0], [0, 0, 0], [0, 1, 0]] */ -
acegreen created this gist
Mar 9, 2016 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,201 @@ import UIKit import Foundation import XCPlayground import SwiftyJSON extension Array { func get(index: Int) -> Element? { if 0 <= index && index < count { return self[index] } else { return nil } } mutating func moveItem(fromIndex oldIndex: Index, toIndex newIndex: Index) { insert(removeAtIndex(oldIndex), atIndex: newIndex) } } extension String { subscript (i: Int) -> Character { return self[self.startIndex.advancedBy(i)] } subscript (i: Int) -> String { return String(self[i] as Character) } subscript (r: Range<Int>) -> String { let start = startIndex.advancedBy(r.startIndex) let end = start.advancedBy(r.endIndex - r.startIndex) return self[Range(start: start, end: end)] } } // Groupon Interview Q2 /* 1 1 1 0,0 0,1 0,2 0 0 0 1,0 1,1 1,2 1 1 1 2,0 2,1 2,2 0 1 0 0 0 0 0 1 0 1. Any live cell with fewer than two live neighbours dies, as if caused by under-population. 2. Any live cell with two or three live neighbours lives on to the next generation. 3. Any live cell with more than three live neighbours dies, as if by over-population. 4. Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction. */ var gameGrid = [[1,1,1], [0,0,0], [1,1,1]] func game (array: [[Int]]) -> [[Int]] { let arrayRowsIndex = array.count - 1 let arrayColumnIndex = array[0].count - 1 var output = array var liveNeighbours: Int = 0 var deadNeighbours: Int = 0 for (rowIndex, rowElement) in output.enumerate() { for (colIndex, colElement) in rowElement.enumerate() { print(rowIndex, colIndex) print(array[rowIndex][colIndex]) // above of element if (colIndex - 1) >= 0 { switch array[rowIndex][colIndex - 1] { case 1: liveNeighbours += 1 default: deadNeighbours += 1 } } // below of element if (colIndex + 1) <= arrayColumnIndex { switch array[rowIndex][colIndex + 1] { case 1: liveNeighbours += 1 default: deadNeighbours += 1 } } // left of element if (rowIndex - 1) >= 0 { switch array[rowIndex - 1][colIndex] { case 1: liveNeighbours += 1 default: deadNeighbours += 1 } } // right of element if (rowIndex + 1) <= arrayRowsIndex { switch array[rowIndex + 1][colIndex] { case 1: liveNeighbours += 1 default: deadNeighbours += 1 } } // below + right of element if (rowIndex + 1) <= arrayRowsIndex && (colIndex + 1) <= arrayColumnIndex { switch array[rowIndex + 1][colIndex + 1] { case 1: liveNeighbours += 1 default: deadNeighbours += 1 } } // above + left of element if (rowIndex - 1) >= 0 && (colIndex - 1) >= 0 { switch array[rowIndex - 1][colIndex - 1] { case 1: liveNeighbours += 1 default: deadNeighbours += 1 } } // below + left of element if (rowIndex + 1) <= arrayRowsIndex && (colIndex - 1) >= 0 { switch array[rowIndex + 1][colIndex - 1] { case 1: liveNeighbours += 1 default: deadNeighbours += 1 } } // above + right of element if (rowIndex - 1) >= 0 && (colIndex + 1) <= arrayColumnIndex { switch array[rowIndex - 1][colIndex + 1] { case 1: liveNeighbours += 1 default: deadNeighbours += 1 } } print("liveNeighbours", liveNeighbours) print("deadNeighbours", deadNeighbours) switch (liveNeighbours, deadNeighbours) { case (let liveNeighbours, let deadNeighbours) where liveNeighbours < 2: // fewer than 2 live neighbours dies output[rowIndex][colIndex] = 0 case (let liveNeighbours, let deadNeighbours) where liveNeighbours >= 2: // 2 or more neighbours lives on break case (let liveNeighbours, let deadNeighbours) where liveNeighbours > 3: // with more than 3 live neighbours dies output[rowIndex][colIndex] = 0 case (let liveNeighbours, let deadNeighbours) where liveNeighbours == 3: // exactly 3 live neighbours comes back to life output[rowIndex][colIndex] = 1 default: break } liveNeighbours = 0 deadNeighbours = 0 } } return output } print(game(gameGrid))