Advertisement
yeeeeeeeeeeeee

gfdage

May 5th, 2025
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.90 KB | None | 0 0
  1. local mon = peripheral.find("monitor")
  2. mon.setTextScale(0.5)
  3.  
  4. -- Combine grids
  5. local grid1 = {
  6. [10] = {[10] = true,[11] = true},[11] = {[10] = true,[11] = true},[13] = {[10] = true,[11] = true,[12] = true},
  7. [50] = {[10] = true,[11] = true},[51] = {[10] = true,[11] = true},[53] = {[11] = true},[54] = {[11] = true},[55] = {[11] = true}
  8. }
  9.  
  10. local grid2 = {
  11. [10] = { [10] = true, [11] = true },[11] = { [10] = true, [11] = true },
  12. [18] = { [11] = true, [12] = true },[19] = { [10] = true, [12] = true },[20] = { [10] = true, [11] = true },
  13. [26] = { [12] = true, [13] = true, [14] = true },[27] = { [12] = true },[28] = { [13] = true },
  14. [32] = { [10] = true, [9] = true },[33] = { [10] = true, [8] = true },[34] = { [8] = true, [9] = true, [20] = true, [21] = true },
  15. [35] = { [20] = true, [22] = true },[36] = { [20] = true },[44] = { [8] = true, [9] = true },
  16. [45] = { [8] = true, [9] = true, [15] = true, [16] = true, [17] = true },[46] = { [15] = true},[47] = { [16] = true},
  17. }
  18.  
  19. local function mergeGrids(a, b)
  20. local result = {}
  21. for x, col in pairs(a) do
  22. result[x] = result[x] or {}
  23. for y in pairs(col) do result[x][y] = true end
  24. end
  25. for x, col in pairs(b) do
  26. result[x] = result[x] or {}
  27. for y in pairs(col) do result[x][y] = true end
  28. end
  29. return result
  30. end
  31.  
  32. local initialGrid = mergeGrids(grid1, grid2)
  33. local grid = {}
  34. local dead_cells = {}
  35. local new_cells = {}
  36.  
  37. local function draw(redraw)
  38. if redraw then
  39. mon.setBackgroundColor(colors.black)
  40. mon.clear()
  41. for x in pairs(grid) do
  42. for y in pairs(grid[x]) do
  43. mon.setCursorPos(x, y)
  44. mon.setBackgroundColor(colors.white)
  45. mon.write(" ")
  46. end
  47. end
  48. else
  49. for x in pairs(dead_cells) do
  50. for y in pairs(dead_cells[x]) do
  51. mon.setCursorPos(x, y)
  52. mon.setBackgroundColor(colors.black)
  53. mon.write(" ")
  54. end
  55. end
  56. for x in pairs(new_cells) do
  57. for y in pairs(new_cells[x]) do
  58. mon.setCursorPos(x, y)
  59. mon.setBackgroundColor(colors.white)
  60. mon.write(" ")
  61. end
  62. end
  63. end
  64. mon.setBackgroundColor(colors.black)
  65. end
  66.  
  67. local function nextGeneration()
  68. dead_cells = {}
  69. new_cells = {}
  70. local toDelete = {}
  71.  
  72. for x in pairs(grid) do
  73. local y_elements = 0
  74. for y in pairs(grid[x]) do
  75. y_elements = y_elements + 1
  76.  
  77. local alive_neighbors = calculateCell(x, y)
  78. if not (alive_neighbors == 2 or alive_neighbors == 3) then
  79. dead_cells[x] = dead_cells[x] or {}
  80. dead_cells[x][y] = true
  81. end
  82.  
  83. local neighbors = {
  84. { x+1, y }, { x-1, y }, { x, y+1 }, { x, y-1 },
  85. { x+1, y+1 }, { x+1, y-1 }, { x-1, y+1 }, { x-1, y-1 }
  86. }
  87. for _, cell in ipairs(neighbors) do
  88. local cx, cy = cell[1], cell[2]
  89. if not cellAlive(cx, cy) and calculateCell(cx, cy) == 3 then
  90. new_cells[cx] = new_cells[cx] or {}
  91. new_cells[cx][cy] = true
  92. end
  93. end
  94. end
  95. if y_elements == 0 then table.insert(toDelete, x) end
  96. end
  97.  
  98. for x in pairs(dead_cells) do
  99. for y in pairs(dead_cells[x]) do
  100. grid[x][y] = nil
  101. end
  102. end
  103.  
  104. for _, x in ipairs(toDelete) do
  105. if grid[x] and next(grid[x]) == nil then grid[x] = nil end
  106. end
  107.  
  108. for x in pairs(new_cells) do
  109. grid[x] = grid[x] or {}
  110. for y in pairs(new_cells[x]) do
  111. grid[x][y] = true
  112. end
  113. end
  114. end
  115.  
  116. function calculateCell(x, y)
  117. local count = 0
  118. local neighbors = {
  119. { x+1, y }, { x-1, y }, { x, y+1 }, { x, y-1 },
  120. { x+1, y+1 }, { x+1, y-1 }, { x-1, y+1 }, { x-1, y-1 }
  121. }
  122. for _, cell in ipairs(neighbors) do
  123. if cellAlive(cell[1], cell[2]) then
  124. count = count + 1
  125. end
  126. end
  127. return count
  128. end
  129.  
  130. function cellAlive(x, y)
  131. return grid[x] and grid[x][y]
  132. end
  133.  
  134. function resetGrid()
  135. grid = mergeGrids(grid1, grid2)
  136. draw(true)
  137. end
  138.  
  139. -- Main loop with 30-second reset timer
  140. local function run()
  141. while true do
  142. resetGrid()
  143. local start = os.epoch("utc")
  144. while os.epoch("utc") - start < 30000 do
  145. os.sleep(0)
  146. nextGeneration()
  147. draw(false)
  148. end
  149. end
  150. end
  151.  
  152. run()
  153.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement