-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReplicatorDetect.lua
More file actions
85 lines (70 loc) · 2.37 KB
/
ReplicatorDetect.lua
File metadata and controls
85 lines (70 loc) · 2.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
-- Detect a single replicator on the grid
-- Utilizing clusters with hashing to detect the copy of a pattern seen at some previous time step
local g = golly()
-- A function to find clusters of connected cells
local function find_clusters()
local all_cells = g.getcells(g.getrect())
local clusters = {}
local visited = {}
-- Helper function to check if a cell is live
local function is_live(x, y)
for i = 1, #all_cells, 2 do
if all_cells[i] == x and all_cells[i + 1] == y then
return true
end
end
return false
end
-- Depth-first search to find connected component
local function dfs(x, y, cluster)
local stack = {{x, y}}
while #stack > 0 do
local pos = table.remove(stack)
local cx, cy = pos[1], pos[2]
local key = cx .. "," .. cy
if not visited[key] and is_live(cx, cy) then
visited[key] = true
table.insert(cluster, cx)
table.insert(cluster, cy)
-- Push neighboring cells to stack
stack[#stack + 1] = {cx + 1, cy}
stack[#stack + 1] = {cx - 1, cy}
stack[#stack + 1] = {cx, cy + 1}
stack[#stack + 1] = {cx, cy - 1}
end
end
end
-- Find all clusters on grid
for i = 1, #all_cells, 2 do
local x, y = all_cells[i], all_cells[i + 1]
if not visited[x .. "," .. y] and is_live(x, y) then
local new_cluster = {}
dfs(x, y, new_cluster)
if #new_cluster > 0 then
clusters[#clusters + 1] = new_cluster
end
end
end
return clusters
end
-- Track configurations and detect replicators
local function detect_replicators()
local clusters = find_clusters()
local hashes = {}
for i, cluster in ipairs(clusters) do
local rect = g.getrect(cluster)
local hash_val = g.hash(rect)
if hashes[hash_val] then
g.show("Possible replicator detected!")
return true
end
hashes[hash_val] = true
end
return false
end
-- Main simulation loop for 'gen' generations
for gen = 1, 100 do
g.run(1) -- Advance one generation
if detect_replicators() then break end
end
g.show("Simulation complete - No replicator detected within 100 generations.")