summaryrefslogtreecommitdiff
path: root/graph.lua
diff options
context:
space:
mode:
Diffstat (limited to 'graph.lua')
-rwxr-xr-xgraph.lua63
1 files changed, 63 insertions, 0 deletions
diff --git a/graph.lua b/graph.lua
new file mode 100755
index 0000000..1feefd1
--- /dev/null
+++ b/graph.lua
@@ -0,0 +1,63 @@
+#!/usr/bin/env lua
+local automaton = require "automaton"
+local generate = require "generate"
+local init = automaton(generate.trange(1, 25))
+
+
+local function node (graph, id)
+ local x = graph.nodes[id]
+ if x then
+ return x
+ end
+ x = {
+ id = id,
+ outcoming = {},
+ incoming = {},
+ distance = {},
+ }
+ graph.nodes[id] = x
+ return x
+end
+
+
+local function add_edges (graph, state, kind)
+ for dst, src in ipairs(state) do
+ local a = node(graph, src)
+ local b = node(graph, dst)
+ a.outcoming[kind] = b
+ b.incoming[kind] = a
+ end
+end
+
+
+local graph = {
+ nodes = {},
+}
+add_edges(graph, init:left(), "left")
+add_edges(graph, init:right(), "right")
+add_edges(graph, init:pivot(), "centre")
+add_edges(graph, init:up(), "up")
+add_edges(graph, init:down(), "down")
+
+
+local function calculate_distances_from (start)
+ local visited = {}
+ local queue = {{node=start, distance=0}}
+ while #queue > 0 do
+ local step = table.remove(queue, 1)
+ if not visited[step.node.id] then
+ step.node.distance[start.id] = step.distance
+ for _, x in pairs(step.node.incoming) do
+ table.insert(queue, {node=x, distance=step.distance+1})
+ end
+ visited[step.node.id] = true
+ end
+ end
+end
+
+
+calculate_distances_from(graph.nodes[3])
+calculate_distances_from(graph.nodes[19])
+for _, v in ipairs(graph.nodes) do
+ print(v.id, v.distance[3], v.distance[19])
+end