Last active
January 4, 2016 21:19
-
-
Save ggggggggg/8679916 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# implement WirelessKeyboard as K | |
immutable K | |
f::Int | |
s::Int | |
end | |
# implement InterferesWith as < | |
>(k::K,K::K)=k.f==K.f | |
#prepare randomized array | |
k = [K(f,(j-1)*3+f) for j=1:12,f=1:3] | |
k9 = [K(f,(j-1)*3+f) for j=1:3,f=1:3] # smaller testing array | |
randomize!(a) = for j = 1:length(a); i = rand(j:length(a)); a[j],a[i] = a[i],a[j]; end | |
randomize!(k);randomize!(k9) | |
# implement algorithm (all golf code in next line) | |
s(k)=while any([k[j]>k[j-1]||j>6&&k[j]>k[j-6] for j=2:36]);i,j=rand(1:36,2);k[[i,j]]=k[[j,i]];end | |
# implement same algorithm for smaller array for testing | |
s9(k)=while any([k[j]>k[j-1]||j>3&&k[j]>k[j-3] for j=2:9]);i,j=rand(1:9,2);k[[i,j]]=k[[j,i]];end | |
# call smaller array test algorithm | |
s9(k9) | |
println(k9) # display result | |
# call full algorithm | |
s(k) | |
println(k) # display result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Since the algorithm is so slow I left it running overnight, and came back to a correct solution.
julia> kr = reshape(k,(6,6))
6x6 Array{K,2}:
K(2,5) K(3,12) K(1,25) K(3,30) K(1,19) K(3,33)
K(3,9) K(1,10) K(2,20) K(1,22) K(3,18) K(1,28)
K(2,32) K(3,21) K(1,16) K(2,11) K(1,13) K(2,35)
K(3,3) K(1,7) K(2,8) K(1,31) K(2,29) K(1,34)
K(1,1) K(2,23) K(3,6) K(2,26) K(3,24) K(2,17)
K(2,14) K(3,27) K(2,2) K(3,36) K(1,4) K(3,15)