Solving algo ok
This commit is contained in:
parent
9ca9766799
commit
d7ac47ce24
4
.vscode/settings.json
vendored
Normal file
4
.vscode/settings.json
vendored
Normal file
|
@ -0,0 +1,4 @@
|
|||
{
|
||||
"editor.insertSpaces": false,
|
||||
"editor.detectIndentation": false
|
||||
}
|
113
main.lua
113
main.lua
|
@ -9,8 +9,6 @@ local YELLOW = 2
|
|||
-- Utils
|
||||
--
|
||||
|
||||
local a = BLUE
|
||||
|
||||
function idx_xy(idx, width)
|
||||
return (idx - 1) % width + 1, (idx - 1) \ width + 1
|
||||
end
|
||||
|
@ -63,20 +61,47 @@ function count(tbl, p)
|
|||
return c
|
||||
end
|
||||
|
||||
function find(tbl, o)
|
||||
local indices = {}
|
||||
for k,v in ipairs(tbl) do
|
||||
if v == o then add(indices, k) end
|
||||
end
|
||||
return indices
|
||||
end
|
||||
|
||||
-- shallow copy
|
||||
function copy(tbl)
|
||||
local copy = {}
|
||||
for v in all(tbl) do add(copy, v) end
|
||||
return copy
|
||||
end
|
||||
|
||||
-- shallow equality
|
||||
function equal(tbl1, tbl2)
|
||||
for k, _ in ipairs(tbl1) do
|
||||
if tbl1[k] ~= tbl2[k] then return false end
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
--
|
||||
-- Board
|
||||
--
|
||||
|
||||
local Board = {}
|
||||
function Board.new()
|
||||
local width = 6
|
||||
local width = 10
|
||||
local t = [[
|
||||
0, 0, 0, 0, 1, 0,
|
||||
2, 0, 0, 0, 1, 2,
|
||||
0, 0, 2, 0, 0, 0,
|
||||
0, 0, 0, 1, 1, 0,
|
||||
0, 0, 0, 0, 0, 0,
|
||||
0, 0, 2, 0, 0, 2
|
||||
2,0,2,0,0,2,1,0,1,2,
|
||||
0,0,0,0,0,0,0,0,0,1,
|
||||
0,1,0,0,0,0,0,0,0,0,
|
||||
0,2,0,0,0,0,0,1,0,2,
|
||||
0,0,1,0,0,2,0,1,0,1,
|
||||
0,0,0,0,1,1,0,0,0,0,
|
||||
0,2,2,0,0,0,0,0,0,2,
|
||||
0,0,0,0,2,0,0,0,0,0,
|
||||
0,0,2,0,0,1,0,0,0,0,
|
||||
0,2,0,0,2,0,2,0,0,0
|
||||
]]
|
||||
local tiles = split(t)
|
||||
|
||||
|
@ -214,7 +239,7 @@ function Board.new()
|
|||
end,
|
||||
|
||||
fillRow = function(self, y, color)
|
||||
printh("Filling line " .. y .. " in " .. color == BLUE and "blue" or "yellow")
|
||||
printh("Filling line " .. y .. " in " .. (color == BLUE and "blue" or "yellow"))
|
||||
local idx = self:xy_idx(1, y)
|
||||
for i = idx, (idx+width-1) do
|
||||
if self:getTile(i) == 0 then
|
||||
|
@ -224,7 +249,7 @@ function Board.new()
|
|||
end,
|
||||
|
||||
fillCol = function(self, x, color)
|
||||
printh("Filling column " .. x .. " in " .. color == BLUE and "blue" or "yellow")
|
||||
printh("Filling column " .. x .. " in " .. (color == BLUE and "blue" or "yellow"))
|
||||
local idx = self:xy_idx(x, 1)
|
||||
for i = idx, #tiles, width do
|
||||
if self:getTile(i) == 0 then
|
||||
|
@ -234,13 +259,73 @@ function Board.new()
|
|||
end,
|
||||
|
||||
noIdenticalLines = function(self)
|
||||
-- columns
|
||||
local cols = self:getCols()
|
||||
for x,col in ipairs(cols) do
|
||||
if count(col, 0) == 2 and count(col, BLUE) == count(col, YELLOW) then
|
||||
local y1, y2 = unpack(find(col, 0))
|
||||
local ab = copy(col) ab[y1] = BLUE ab[y2] = YELLOW
|
||||
local ba = copy(col) ba[y1] = YELLOW ba[y2] = BLUE
|
||||
-- Check if a dupe exists
|
||||
for x2,col in ipairs(cols) do
|
||||
if equal(col, ab) then
|
||||
printh("No-dupe at col " .. x .. " from col " .. x2)
|
||||
printh(" " .. self:xy_idx(x,y1) .. " in yellow")
|
||||
printh(" " .. self:xy_idx(x,y2) .. " in blue")
|
||||
self:fill(self:xy_idx(x,y1), YELLOW)
|
||||
self:fill(self:xy_idx(x,y2), BLUE)
|
||||
goto continue
|
||||
elseif equal(col, ba) then
|
||||
printh("No-dupe at col " .. x .. " from col " .. x2)
|
||||
printh(" " .. self:xy_idx(x,y1) .. " in blue")
|
||||
printh(" " .. self:xy_idx(x,y2) .. " in yellow")
|
||||
self:fill(self:xy_idx(x,y1), BLUE)
|
||||
self:fill(self:xy_idx(x,y2), YELLOW)
|
||||
goto continue
|
||||
end
|
||||
end
|
||||
end
|
||||
::continue::
|
||||
end
|
||||
|
||||
-- rows
|
||||
local rows = self:getRows()
|
||||
for y,row in ipairs(rows) do
|
||||
if count(row, 0) == 2 and count(row, BLUE) == count(row, YELLOW) then
|
||||
local x1, x2 = unpack(find(row, 0))
|
||||
local ab = copy(row) ab[x1] = BLUE ab[x2] = YELLOW
|
||||
local ba = copy(row) ba[x1] = YELLOW ba[x2] = BLUE
|
||||
-- Check if a dupe exists
|
||||
for y2,row in ipairs(rows) do
|
||||
if equal(row, ab) then
|
||||
printh("No-dupe at row " .. y .. " from row " .. y2)
|
||||
printh(" " .. self:xy_idx(x1,y) .. " in yellow")
|
||||
printh(" " .. self:xy_idx(x2,y) .. " in blue")
|
||||
self:fill(self:xy_idx(x1,y), YELLOW)
|
||||
self:fill(self:xy_idx(x2,y), BLUE)
|
||||
goto continue
|
||||
elseif equal(row, ba) then
|
||||
printh("No-dupe at row " .. y .. " from row " .. y2)
|
||||
printh(" " .. self:xy_idx(x1,y) .. " in blue")
|
||||
printh(" " .. self:xy_idx(x2,y) .. " in yellow")
|
||||
self:fill(self:xy_idx(x1,y), BLUE)
|
||||
self:fill(self:xy_idx(x2,y), YELLOW)
|
||||
goto continue
|
||||
end
|
||||
end
|
||||
end
|
||||
::continue::
|
||||
end
|
||||
end,
|
||||
|
||||
draw = function(self)
|
||||
for k,v in ipairs(tiles) do
|
||||
local x,y = self:idx_xy(k)
|
||||
local color = v == BLUE and 12 or v == YELLOW and 10 or 1
|
||||
rectfill((x-1)*10 + x*8 + 4, (y-1)*10 + y*8 + 4, 12, 12, color)
|
||||
local w = 10
|
||||
local p = 1
|
||||
rectfill((x-1)*w + (x-1)*(p+1), (y-1)*w + (y-1)*(p+1), w, w, color)
|
||||
print(k, (x-1)*w + (x-1)*(p+1), (y-1)*w + (y-1)*(p+1), 8)
|
||||
end
|
||||
end
|
||||
}
|
||||
|
@ -260,7 +345,9 @@ end
|
|||
|
||||
function _draw()
|
||||
cls()
|
||||
if time() % 1 == 0 then
|
||||
if time() % .5 == 0 then
|
||||
printh("==")
|
||||
printh("==")
|
||||
board:solve()
|
||||
end
|
||||
board:draw()
|
||||
|
|
Loading…
Reference in New Issue
Block a user