There was an error while loading. Please reload this page.
1 parent 57d058b commit 32a048eCopy full SHA for 32a048e
src/main/scala/Generation.scala
@@ -1,5 +1,23 @@
1
case class Generation(aliveCells: Set[Cell] = Set.empty) {
2
3
def next: Generation =
4
- ???
+ new Generation(stayingAlive ++ born)
5
+
6
+ def stayingAlive: Set[Cell] =
7
+ aliveCells.filter(canStayAlive)
8
9
+ def born: Set[Cell] =
10
+ aliveCells.flatMap(deadNeighbors).filter(canBeBorn)
11
12
+ def canStayAlive(cell: Cell): Boolean =
13
+ Set(2, 3).contains(aliveNeighbors(cell).size)
14
15
+ def canBeBorn(cell: Cell): Boolean =
16
+ aliveNeighbors(cell).size == 3
17
18
+ def aliveNeighbors(cell: Cell): Set[Cell] =
19
+ cell.neighbors.intersect(aliveCells)
20
21
+ def deadNeighbors(cell: Cell): Set[Cell] =
22
+ cell.neighbors.diff(aliveCells)
23
}
0 commit comments