-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnakesolver.lua
More file actions
executable file
·162 lines (129 loc) · 4.24 KB
/
snakesolver.lua
File metadata and controls
executable file
·162 lines (129 loc) · 4.24 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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#!/usr/bin/luajit-2
require('utils')
require('dumper')
local dim= 4
local snake_short= "5 1 1 2 1 1 1 6 1 1 2 2 4 2 3 4 3 5 5 2 2 4 2 2 2"
-- local snake_short="7 1 1 2 1 1 1 6 1 1 2 2 4 2 3 4 3 5 5 2 2 4 2 2"
-- local snake_short="2 4 2 2 4 4 4 2 2 4 1 1 4 2 2 4 4 4 2 2 4 3 1"
local exit_after_first_solution= true
local dimm1= dim - 1
local dimp1= dim + 1;
local dimp2= dim + 2;
local dimp2sq= dimp2 * dimp2;
local slen= dim * dim * dim;
local snake= {}
local color= 2
for i,k in ipairs(split_string(snake_short, " ")) do
color= 3 - color
for j = 1,k do snake[#snake + 1]= color end
end
if slen ~= #snake then
print ("snake length is not slen!")
os.exit(1)
end
if #snake % 2 ~= 0 then
print ("snake length is uneven!")
os.exit(1)
end
local xyz2n= function(x, y, z) return z * dimp2sq + y * dimp2 + x + 1 end
local n2x= function(n) return (n - 1) % dimp2 end
local n2y= function(n) return math.floor((n - 1) / dimp2) % dimp2 end
local n2z= function(n) return math.floor((n - 1) / dimp2sq) % dimp2 end
local offset= 0
local tries= 0
local result= {}
local solved= {}
for x = 0,dimp1 do
for y = 0,dimp1 do
for z = 0,dimp1 do
local n= xyz2n(x, y, z)
if x == 0 or y == 0 or z == 0 or x == dimp1 or y == dimp1 or z == dimp1 then
result[n]= -1
solved[n]= 0
elseif x == 1 or y == 1 or z == 1 or x == dim or y == dim or z == dim then
result[n]= -2
solved[n]= (math.floor((x - 1) / 2) + math.floor((y - 1) / 2) + math.floor((z - 1) / 2))% 2 + 1;
else
result[n]= -2
solved[n]= 0
end
end
end
end
local get_cube= function()
local r= ""
for z = 1,dim do
r= r .. "z=" .. z .. "\n"
.. "\t+----+----+----+----+"
.. "\t+---+---+---+---+\n"
for y = 1,dim do
r= r .. "\t|"
for x = 1,dim do
local n= xyz2n(x, y, z)
r= r .. string.format(" %2d |", result[n])
end
r= r .. "\t|"
for x = 1,dim do
local n= xyz2n(x, y, z)
local snake_i= result[n]
if snake_i < 0 then
r= r .. " ? |"
elseif snake[snake_i] == 1 then
r= r .. " X |"
else
r= r .. " |"
end
end
r= r .. "\n\t+----+----+----+----+"
r= r .. "\t+---+---+---+---+\n"
end
r= r .. "\n"
end
return r
end
local print_cube= function()
print (get_cube())
end
local has_solved= function()
print ("\n\nSOLVED with " .. tries .. " tries\n")
print_cube()
if exit_after_first_solution then
os.exit(1)
end
end
local t= function(check, if_true, if_false)
if check then return if_true else return if_false end
end
local find_way
find_way= function(n, idx, dir, fin)
if fin then
-- ist naechstes element ist der anfang der schlange?
if result[n] == 1 then has_solved() end
return
end
if result[n] > -2 then return end -- already set
if solved[n] > 0 and solved[n] ~= snake[idx] then return end -- color cares but does not match
-- print ("fin=" .. t(fin, "true", "false") .. " idx=" .. idx .. " dir=" .. dir .. " n=" .. n .. " xyz=[" .. n2x(n) .. " " .. n2y(n) .. " " .. n2z(n) .. "]\n")
tries= tries + 1
if tries % 1000000 == 0 then
print ("\rtries: " .. tries .. " (" .. n2x(n) .. "," .. n2y(n) .. "," .. n2z(n) .. ") -> idx: " .. snake[idx])
end
result[n]= idx
idx= idx + 1
fin= idx > #snake
if dir ~= 1 then find_way(n + 1, idx, 0, fin) end
if dir ~= 0 then find_way(n - 1, idx, 1, fin) end
if dir ~= 3 then find_way(n + dimp2, idx, 2, fin) end
if dir ~= 2 then find_way(n - dimp2, idx, 3, fin) end
if dir ~= 5 then find_way(n + dimp2sq, idx, 4, fin) end
if dir ~= 4 then find_way(n - dimp2sq, idx, 5, fin) end
result[n]= -2
end
while offset < slen do
if offset > 0 then
table.insert(snake, 1, table.remove(snake, #snake))
print ("\nstarting over (" .. offset .. ")\n")
end
offset= offset + 1
find_way(xyz2n(1, 1, 1), 1, -1, false)
end