Last active
September 18, 2018 18:37
-
-
Save Ismael-VC/f5107608f9780e30132f to your computer and use it in GitHub Desktop.
Eight Queens Puzzle in Julia.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env julia | |
#= | |
The following code is licensed under the MIT "Expat" License: | |
Copyright (c) 2015: Ismael Venegas Castelló. | |
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. | |
=# | |
__precompile__(true) | |
""" | |
# EightQueensPuzzle | |
Ported to **Julia** from examples in several languages from | |
here: https://hbfs.wordpress.com/2009/11/10/is-python-slow | |
""" | |
module EightQueensPuzzle | |
export main | |
type Board | |
cols::Int | |
nodes::Int | |
diag45::Int | |
diag135::Int | |
solutions::Int | |
Board() = new(0, 0, 0, 0, 0) | |
end | |
"Marks occupancy." | |
function mark!(b::Board, k::Int, j::Int) | |
b.cols $= (1 << j) | |
b.diag135 $= (1 << (j+k)) | |
b.diag45 $= (1 << (32+j-k)) | |
end | |
"Tests if a square is menaced." | |
function test(b::Board, k::Int, j::Int) | |
b.cols & (1 << j) + | |
b.diag135 & (1 << (j+k)) + | |
b.diag45 & (1 << (32+j-k)) == 0 | |
end | |
"Backtracking solver." | |
function solve!(b::Board, niv::Int, dx::Int) | |
if niv > 0 | |
for i in 0:dx-1 | |
if test(b, niv, i) == true | |
mark!(b, niv, i) | |
solve!(b, niv-1, dx) | |
mark!(b, niv, i) | |
end | |
end | |
else | |
for i in 0:dx-1 | |
if test(b, 0, i) == true | |
b.solutions += 1 | |
end | |
end | |
end | |
b.nodes += 1 | |
b.solutions | |
end | |
"C/C++-style `main` function." | |
function main() | |
for n = 1:17 | |
gc() | |
b = Board() | |
@show n | |
print("elapsed:") | |
solutions = @time solve!(b, n-1, n) | |
@show solutions | |
println() | |
end | |
end | |
end | |
using EightQueensPuzzle | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Julia tests ran at JuliaBox using
v0.5+
, the others are from the original (dated) article: Is Python Slow ...oh yep! 🙊Still Python can be made fast also but with more hasle: How To Make Python Run As Fast As Julia ...but why bother? 🙈