summaryrefslogtreecommitdiff
path: root/automaton/init.lua
diff options
context:
space:
mode:
Diffstat (limited to 'automaton/init.lua')
-rw-r--r--automaton/init.lua129
1 files changed, 129 insertions, 0 deletions
diff --git a/automaton/init.lua b/automaton/init.lua
new file mode 100644
index 0000000..8c4bc40
--- /dev/null
+++ b/automaton/init.lua
@@ -0,0 +1,129 @@
+local actions = {}
+
+
+function actions:clone ()
+ local new = {}
+ for i, v in ipairs(self) do
+ new[i] = v
+ end
+ return setmetatable(new, getmetatable(self))
+end
+
+
+function actions:tostring ()
+ return
+ table.concat(self, nil, 1, 9) .."\n "..
+ table.concat(self, nil, 10, 16) .."\n"..
+ table.concat(self, nil, 17, 25)
+end
+
+
+function actions:pivot ()
+ local new = self:clone()
+ new[10] = self[3]
+ new[3] = self[12]
+ new[12] = self[19]
+ new[19] = self[10]
+ return new
+end
+
+
+function actions:up ()
+ local new = self:clone()
+ for i=1, 9 do
+ new[16 + i] = self[i]
+ end
+ for i=2, 8 do
+ new[i] = self[8 + i]
+ new[8 + i] = self[16 + i]
+ end
+ new[1] = self[17]
+ new[9] = self[25]
+ return new
+end
+
+
+function actions:right ()
+ local new = self:clone()
+ new[1] = self[9]
+ for i=2, 9 do
+ new[i] = self[i - 1]
+ end
+ new[10] = self[16]
+ for i=11, 16 do
+ new[i] = self[i - 1]
+ end
+ new[17] = self[25]
+ for i=18, 25 do
+ new[i] = self[i - 1]
+ end
+ return new
+end
+
+
+function actions:down ()
+ local new = self:clone()
+ for i=1, 9 do
+ new[i] = self[26 - i]
+ new[26 - i] = self[i]
+ end
+ for i=1, 3 do
+ new[9 + i] = self[17 - i]
+ new[17 - i] = self[9 + i]
+ end
+ new[13] = self[13]
+ return new
+end
+
+
+function actions:left ()
+ local new = self:clone()
+ for i=1, 8 do
+ new[i] = self[i + 1]
+ end
+ new[9] = self[1]
+ for i=10, 15 do
+ new[i] = self[i + 1]
+ end
+ new[16] = self[10]
+ for i=17, 24 do
+ new[i] = self[i + 1]
+ end
+ new[25] = self[17]
+ return new
+end
+
+
+local map = {
+ [0] = actions.pivot,
+ [1] = actions.up,
+ [2] = actions.right,
+ [3] = actions.down,
+ [4] = actions.left,
+}
+
+
+function actions:apply (eye)
+ return map[eye](self)
+end
+
+
+local mt = {
+ __index = actions,
+ __tostring = actions.tostring,
+}
+
+
+local function new (data)
+ if type(data) == "string" then
+ local obj = {}
+ for i=1, 25 do
+ obj[i] = data:sub(i, i)
+ end
+ data = obj
+ end
+ return setmetatable(data, mt)
+end
+
+
+return new