summaryrefslogtreecommitdiff
path: root/automaton/init.lua
blob: 8c4bc40e39336731d96c3bb669e5b46a0697a194 (plain)
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
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