User:Aadenboy/O(n) CGoL

From Esolang
Jump to navigation Jump to search

O(n) implementation of Conway's Game of Life. Worded off of my Lua implementation of such.

  1. All currently alive cells are stored in a hashmap B, where the key is the position of the cell.
  2. Create two new empty hashmaps, one hashmap H for the counting of dead cells and another N for the new state of the board.
  3. Loop through all currently alive cells in hashmap B.
    1. For each cell C, loop through all eight of its neighbors. Keep a count A of how many alive neighbors it has.
    2. If the neighbor is alive, increment A by one.
    3. If the neighbor is dead (or has no associated value in the hashmap B), increment its associated key in hashmap H by one, or set it to one if it does not exist.
    4. If the value in hashmap H is set to three, set the associated key in hashmap N to be alive.
    5. If the value in hashmap H is set to four (after being three), remove the associated key in hashmap N.
    6. After looping through all neighbors, set cell C's associated key in hashmap N to be alive if count A is two or three.
  4. Set hashmap B to hashmap N. Discard hashmap H.