Posts

Showing posts with the label Puzzle Solver

Can You Beat The British Intelligence? (Nonogram Solver)

Answer : Haskell, 242 230 201 199 177 163 160 149 131 bytes import Data.Lists m=map a#b=[x|x<-m(chunk$length b).mapM id$[0,1]<$(a>>b),g x==a,g(transpose x)==b] g=m$list[0]id.m sum.wordsBy(<1) Finally under 200 bytes, credit to @Bergi. Huge thanks to @nimi for helping almost halving the size. Wow. Almost at half size now, partly because of me but mainly because of @nimi. The magic function is (#) . It finds all solutions of a given nonogram. This is able to solve all cases, but may be super slow, since it's complexity is about O(2^(len a * len b)) . A quick benchmark revealed 86GB allocated for a 5x5 nonogram. Fun fact: It works for all nonograms, not only square ones. How it works: a#b : Given lists of lists of integers which represent the number of squares, generate all grids ( map(chunk$length b).mapM id$a>>b>>[[0,1]] ) and filter the results to keep only the valid ones. g : Given a potential nonogram it sums the runs of 1's...

Build A Killer Sudoku Solver

Answer : GolfScript, 138 characters n%~[~]:N;1/:P.&:L;9..*?{(.[{.9%)\9/}81*;]:§;L{.`{\~@@=*}+[P§]zip%{+}*\L?N==}%§9/..zip+\3/{{3/}%zip{{+}*}%}%{+}*+{.&,9=}%+1-,!{§puts}*.}do; This is a killer sudoku solver in GolfScript. It expects input on STDIN in two rows as given in the example above. Please note: Since the puzzle description does not make any restrictions on execution time I preferred small code size over speed. The code tests all 9^81 grid configurations for a solution which may take some time on a slow computer ;-) R - 378 characters Assuming x="AABBBCDEFGGHHCCDEFGGIICJKKFLMMINJKOFLPPQNJOORSPTQNUVVRSTTQWUUXXSYZWWaaXXSYZWbbbcc" y="3 15 22 4 16 15 25 17 9 8 20 6 14 17 17 13 20 12 27 6 20 6 10 14 8 16 15 13 17" 378 characters: z=strsplit v=sapply R=rep(1:9,9) C=rep(1:9,e=9) N=1+(R-1)%/%3+3*(C-1)%/%3 G=z(x,"")[[1]] M=as.integer(z(y," ")[[1]])[order(unique(G))] s=c(1,rep(NA,80)) i=1 repeat if({n=function(g)!any(v(split(s,...