Skip to content

Instantly share code, notes, and snippets.

@gboncoffee
Created November 30, 2023 21:13
Show Gist options
  • Save gboncoffee/2ea36da607b3b2e2e1aef0af64ff0109 to your computer and use it in GitHub Desktop.
Save gboncoffee/2ea36da607b3b2e2e1aef0af64ff0109 to your computer and use it in GitHub Desktop.
8 queens problem solution in Lua
#!/usr/bin/env lua
--
-- Copyright (c) 2023 Gabriel G. de Brito
--
-- Permission is hereby granted, free of charge, to any person obtaining a copy
-- of this software and associated documentation files (the "Software"), to deal
-- in the Software without restriction, including without limitation the rights
-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-- copies of the Software, and to permit persons to whom the Software is
-- furnished to do so, subject to the following conditions:
--
-- The above copyright notice and this permission notice shall be included in
-- all copies or substantial portions of the Software.
--
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
-- SOFTWARE.
--
print_coord = function(c)
io.write(string.format("(%d,%d)", c.x, c.y))
end
print_queens = function(q)
io.write("[")
for i = 1, #q do
print_coord(q[i])
end
io.write("]")
end
coord = function(x, y)
return { x = x, y = y }
end
transp = function(c, size)
return coord(c.y, size + 1 - c.x)
end
nextp = function(c, size)
if not c then
return nil
end
if c.y < size then
return coord(c.x, c.y + 1)
elseif c.x < size then
return coord(c.x + 1, 1)
end
return nil
end
sameax = function(p1, p2)
return p1.x == p2.x or p1.y == p2.y
end
samediag = function(p1, p2, size)
local pt1 = transp(p1, size)
local pt2 = transp(p2, size)
return p1.x - p1.y == p2.x - p2.y or pt1.x - pt1.y == pt2.x - pt2.y
end
kills = function(p1, p2, size)
return sameax(p1, p2) or samediag(p1, p2, size)
end
killsl = function(p1, l, size)
for i = 1,#l do
if kills(p1, l[i], size) then
return true
end
end
return false
end
queens8 = function(queens, size, n)
if n > size then
return queens
end
local np = nextp(queens[#queens], size) or coord(1, 1)
while np do
io.write("trying ")
print_queens(queens)
io.write(" + ")
print_coord(np)
print ""
if not killsl(np, queens, size) then
table.insert(queens, np)
if queens8(queens, size, n + 1) then
return queens
end
queens[#queens] = nil
end
np = nextp(np, size)
end
return nil
end
queens = {}
size = 8
q = queens8(queens, size, 1)
if q then
print "solution found!!"
print_queens(queens)
print ""
else
print "no solution found"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment