Advertisement
spdqbr

DivoomOfLife

May 8th, 2024 (edited)
1,056
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 9.17 KB | Software | 0 0
  1. import random
  2. import time
  3. import copy
  4. from pixoo import Pixoo, SimulatorConfig
  5.  
  6. pixoo_ip = "192.168.0.116"
  7. grid_size = 64
  8. hist_length = 64*4*2+5
  9. idle_count = 40
  10.  
  11. def randomize():
  12.     return [ [ random.randint(0, 1) == 1 for x in range(0,grid_size) ] for y in range(0,grid_size) ]
  13.  
  14. def loadstr(string):
  15.     out = [ [ 0 for x in range(0,grid_size) ] for y in range(0,grid_size) ]
  16.     lines=string.split("\n")
  17.     for row in range(grid_size):
  18.         for col in range(grid_size):
  19.             if row >= len(lines) or col >= len(lines[row]):
  20.                 out[row][col] = 0
  21.             else:
  22.                 #print(f'{row}, {col} :: {len(lines)} :: {lines[row][col]}')
  23.                 out[row][col] = lines[row][col] == "1"
  24.     return out
  25.  
  26. pixoo = Pixoo(pixoo_ip)
  27. #pixoo = Pixoo(pixoo_ip, simulated=True, simulation_config=SimulatorConfig(4))
  28.  
  29. green = [ 0, 255, 0]
  30. red = [ 255, 0, 0 ]
  31. black = [ 0 ,  0, 0]
  32.  
  33. def draw_grid(grid, color=green, factor = 1.0, push = True):
  34.     for col in range(0, len(grid)):
  35.         for row in range(0, len(grid[col])):
  36.             if grid[row][col]:
  37.                 pixoo.draw_pixel([col, row], [int(x * factor) for x in color])
  38.             else:
  39.                 pixoo.draw_pixel([col, row], black)
  40.     if push: pixoo.push()
  41.  
  42. def step(current_grid, wrap = False):
  43.     """
  44.    Any live cell with two or three live neighbours survives.
  45.    Any dead cell with three live neighbours becomes a live cell.
  46.    All other live cells die in the next generation. Similarly, all other dead cells stay dead.
  47.    """
  48.     new_grid = [ [0 for x in range(0, grid_size) ] for y in range(0, grid_size) ]
  49.     for col in range(0, len(current_grid)):
  50.         for row in range(0, len(current_grid[col])):
  51.             neighbor_count = 0
  52.             for dr in [-1, 0, 1]:
  53.                 for dc in [-1, 0, 1]:
  54.                     if wrap:
  55.                         row_check = (( dr + row) + 64 ) % 64
  56.                         col_check = (( dc + col) + 64 ) % 64
  57.                         if (dr != 0) or (dc != 0):
  58.                             if current_grid[row_check][col_check]:
  59.                                 neighbor_count = neighbor_count + 1
  60.                     else:
  61.                         if 0 <= (col + dc) < len(current_grid):
  62.                             if 0 <= (row + dr) < len(current_grid):
  63.                                 if (dr != 0) or (dc != 0):
  64.                                     if current_grid[row+dr][col+dc]:
  65.                                         neighbor_count = neighbor_count + 1
  66.             if current_grid[row][col] and (neighbor_count == 2 or neighbor_count == 3):
  67.                     new_grid[row][col] = True
  68.             elif not current_grid[row][col] and neighbor_count == 3:
  69.                     new_grid[row][col] = True
  70.             else:
  71.                 new_grid[row][col] = False
  72.     return new_grid
  73.  
  74. max_grid = loadstr("""1.111..11..11...11.111.1.111...1111.11111....111....1.1.1.1.1.11
  75. .1.11111......1....1.1.1.111..1...11..111...111.....1..111111111
  76. 1......1.1....11.1.111111.1.1....1.1..11..1.1111....1.1..1.1111.
  77. .......1....111.1.1.11..1..11111.1.111..1......1..11.1......111.
  78. ..111.11.1..11....1...1.1..111..11...11111...1111...11..1.1.1...
  79. ..11..1..11..11.1....111.......11.1...1.1..11.....1.1..1..11....
  80. 1111.1.1..11111.111111.1.1.111111...11.111....11..11....1...1.11
  81. 1111...1.11.111.1.1..1111111.1.1111.111..1...1111....1.11.1.111.
  82. .1.1.1111..11.1.111..11.1.1...1111....11.1...1.1....11.1.1..11.1
  83. 11...111.11......11...1.11.11.1.111..1.1...11..1.1.1.1111.1.1111
  84. ..1111.1.1.1.1111.1.1.1.1...11....1....1.11....1.1.1...1.1.1....
  85. 111.1.1111..1..1..11.11.1....1....1.1.1.11.11111111..1.11.....1.
  86. 1.1..1..11.11111.11111....1.1..1.1.1111.1..11.....11.1111.1.11.1
  87. 1..1.111.111..111..1.11.1.1...1..1.1111..11...1...1.111.....11.1
  88. ....11.1..11..1.111.111..11...11...11.1..1.1...11111...111.....1
  89. ...1.1..1.11.1....1..1111....11.11....1..1111..11....111...1....
  90. 1...1..1111...1...111111..1..1...11......1..111..1.11.1...1.1111
  91. .1..1...111..1.1.111...1..111.11.1........1..111..1111.11111..1.
  92. 111.1......1....111..1.111.11...111111.1.1..1.1.1.1.1...1111.1..
  93. 1.........111.1..1...1.11....111.1.1..1..1..1.1.1.111..1..1..111
  94. ..111.1..1..1.1.1.11...1111111....1.111..1..1111111.111.11.111.1
  95. 11..1..1.1.11.1.....111..1.1.11....1.11..11111..11.11.111....1.1
  96. .1....11.11111.11.111...11.1..11.1...1.111111.1....111...1....11
  97. .1...11.111.1...1.1..1....1.11...11.......1.1..111.1...1..11..1.
  98. ..111.1..111.1.1..11.11.....1..1.1111.1...1...11..11.11..1.1..1.
  99. 1.111...1.1.11.11.1...1.11..11...1.1.111..111...1...111..11.1..1
  100. ..11.111..1.111..1.111.11.1..11111.1..1.1...11.1.11.1.11..111..1
  101. .111..1..11..1.11...111...11..1.11...11....111.1.1..1.1..1.1.11.
  102. 1...1.1111.1111..111..1..111.1.1..111..1....11...1.11..11..11..1
  103. ..111..11...1.1111.11.1111.....1111.11111.111.1..1.1111.1111.1..
  104. .1..1.11.1.111.11..11111..111.1...11..1..1111....1111..11..11...
  105. 1.1.1.....1..1.11111.111..11111111.1.....1..11......1.1..1.11111
  106. ...1...1.111.1.11..1..1111.1..1.111........1..1.1.1.1...1.1..1..
  107. ..1..1..11...1..11...11.111111.111.1..11.111.1...11.....1.1..1.1
  108. 1111..1.....1.11.1.1..1..1......111......1...1.11.11.111..1..11.
  109. 1111.1..1.....11.1....111.1.1.1.11..1111.1..111..1..1.1...1.....
  110. ....11..1..1.11.111.11....111111..1111....1.111.1..1...1.11.11.1
  111. .1...11..1.1....11...1.11.1..111.1..1.1.1.1.1.111..1.1111.1.11.1
  112. 11...1.111.1...11.11.11.1.1.11.....1...1..11...1...1...11.11111.
  113. ..1..11..1111.1..1..1...1....1..1111....1111.1..1.11.111.111.11.
  114. .1.....1...1111.1.111....1111.1.1.111.....1...11..1.111..11..11.
  115. 11..11..11.1.11.1111.....1.111.1..11....1.11.11111.1.111.1...1.1
  116. .1.1.111.111.1.1...1111.1..11.111..1111111111.1.1..111.111..1.11
  117. 1.1..1111.1..1111...11111.111.111111..111.111.111...11...1.1...1
  118. ....1.1.111.11..111.1111.1..11...1111..111111.1..1..111..1.1....
  119. 11111....1.11111..111.11..1..1.1.1..11...1.1...1.1111.1.1.111.1.
  120. ..11111111.1....1.1.....111...11...1.1..1.1.111..1.111..11..1.11
  121. 1.1...1..11.1....1.1.1.11..1.1.1.11..1....111..1.1.1.1..111.1..1
  122. 1...11..1111.1.1..1....1.111..11..1...1.....1.1.1.1..1.11..1.1..
  123. 11.11111.11...111...1111.1.1.1...1.1..1..1.1.11...11.1..1.11.111
  124. .1..1...1.11.111.1..1....11.1.111111..1..1111..11111111.1.11.111
  125. .111..1.1..11.11.1...1.1..11.1.11..1111.1.11.111111...11..1.1..1
  126. 11111.11..111..11...11.1..1..1111..1.1......11.1.1....1.1.1.1.1.
  127. 11.11.111.1.11...1.1.1.11.1..11..1.111...1...1.1.1...1..1.111.11
  128. .....1.1.1111..11..1..111.1.1..1111...1...1.11.1.1.111.1...1.111
  129. 11.11...11...1.11....1..1.11....1111..1...11.1.1..11....11...1..
  130. 1.11.11.1111...11..11.1..11.1...11.1...111111.1..1...1..1111.1..
  131. 11.11.11.1...1..1111111....1.11111.1..1....1...1.....1.1.1..111.
  132. 1111.11.1.11..........1.1.....11.....1.1........11.111.11.111..1
  133. ..111..1...111.11....11111..11..1..1111...111..1.1..111111..111.
  134. ......11....1.11.111.1..11..11..1..111..11.11.11111...1111..1...
  135. 1..1..11..111.1..1.1...1.111....11...1.1..1.11..1.1.11..1..111.1
  136. 11.1.1.1.1.111111..1.1.1.11.1.1..111..1.11..111..11..111..1111..
  137. 1...1....111111.1.1..1.11....1111.1...1.11.111.111.1.111......11""")
  138.  
  139. def compare(a, b):
  140.     if len(a) != len(b): return False
  141.     for row in range(len(a)):
  142.         if len(a[row]) != len(b[row]): return False
  143.         for col in range(len(a[row])):
  144.             if a[row][col] != b[row][col]: return False
  145.     return True
  146.  
  147. def scoreboard(max, current, color):
  148.     if current > max:
  149.         newmax = current
  150.         record = "NEW RECORD!"
  151.     else:
  152.         newmax = max
  153.         record = ""
  154.  
  155.     max_str = str(newmax)
  156.     cur_str = str(current)
  157.  
  158.     c = f"LIFESPAN: %{len(max_str)}s" % cur_str
  159.     m = f"MAX     : %{len(max_str)}s" % max_str
  160.     c_len = len(c) *4
  161.     m_len = len(m) *4
  162.  
  163.     pixoo.draw_text_at_location_rgb(record, 32 - int(len(record) * 4 / 2), 10, 0, 0, color)
  164.     pixoo.draw_text_at_location_rgb(c, 32 - int(c_len / 2), 26, color, 0, 0)
  165.     pixoo.draw_text_at_location_rgb(m, 32 - int(m_len / 2), 33, color, 0, 0)
  166.     pixoo.push()
  167.     return newmax
  168.  
  169. def dumpgrid(grid):
  170.     rows = []
  171.     for row in grid:
  172.         foo = [ "1" if x else "." for x in row ]
  173.         rows.append("".join(foo))
  174.     print("\n".join(rows))
  175.  
  176. if __name__ == "__main__":
  177.     history = [ [[]] for x in range(hist_length) ]
  178.  
  179.     max = 0
  180.  
  181.     init_grid = randomize()
  182.     init_grid = max_grid
  183.     current_grid = copy.deepcopy(init_grid)
  184.  
  185.     count = 0
  186.     while(True):
  187.         draw_grid(current_grid)
  188.         current_grid = step(current_grid, True)
  189.         match = False
  190.         for grid in history:
  191.             if compare(current_grid, grid):
  192.                 match = True
  193.                 break
  194.         if match:
  195.             for i in range(idle_count):
  196.                 current_grid = step(current_grid)
  197.                 draw_grid(current_grid, green, 1 - (float(i) / idle_count), False)
  198.                 scoreboard(max, count, int(255 * float(i) / idle_count))
  199.             if count > max :
  200.                 max = count
  201.                 print(f"{count}:")
  202.                 dumpgrid(init_grid)
  203.             time.sleep(10)
  204.             current_grid = randomize()
  205.             init_grid = copy.deepcopy(current_grid)
  206.             count = 0
  207.         else:
  208.             history[count % hist_length] = copy.deepcopy(current_grid)
  209.         count = count + 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement