Toggle navigation
?
users online
Logout
Open hangout
Open chat for current file
:- use_module(library(clpfd)). /* Currently this times out... I think the logic is right, but the backtracing is killing it, * I've played about with rule order a bit but it hasn't helped. I think it needs to go breadth-first * on the sumbetween1and9 steps, there's a fairly small set out what can exist there, and maybe * handling all_distinct and sumbetween on rows first then do both on columns.*/ sumsudoku(PuzzleRows) :- transpose(PuzzleRows, PuzzleColumns), length(PuzzleRows, 10), length(PuzzleColumns, 10), reverse(PuzzleRows, [_|ValueRows]), reverse(PuzzleColumns, [_|ValueColumns]), transpose(ValueColumns, RowsAndColumnSums), reverse(RowsAndColumnSums, [_|Rows]), transpose(Rows, Columns), flatten(Rows, Values), Values ins 1..9, maplist(all_distinct, Rows), maplist(sumbetween1and9, ValueRows), maplist(all_distinct, Columns), maplist(sumbetween1and9, ValueColumns), maplist(label, Rows), /* still not sure what this does */ blocks(Rows, Blocks), maplist(all_distinct, Blocks). sumbetween1and9(SumAndValues) :- reverse(SumAndValues, [Sum|Values]), between1and9(Values, SumValues), sum(SumValues, #=, Sum). between1and9(List, Between) :- ( append(_, [1|Rest], List), append(Between, [9|_], Rest) ); ( append(_, [9|Rest], List), append(Between, [1|_], Rest) ). blocks([A,B,C,D,E,F,G,H,I], Blocks) :- blocks(A,B,C,Block1), blocks(D,E,F,Block2), blocks(G,H,I,Block3), append([Block1, Block2, Block3], Blocks). blocks([], [], [], []). blocks([A,B,C|Bs1],[D,E,F|Bs2],[G,H,I|Bs3], [Block|Blocks]) :- Block = [A,B,C,D,E,F,G,H,I], blocks(Bs1, Bs2, Bs3, Blocks). /* [ [_,_,_,_,_,_,_,_,_,_] , [_,_,_,_,_,_,_,_,_,_] , [_,_,_,_,_,_,_,_,_,_] , [_,_,_,_,_,_,_,_,_,_] , [_,_,_,_,_,_,_,_,_,_] , [_,_,_,_,_,_,_,_,_,_] , [_,_,_,_,_,_,_,_,_,_] , [_,_,_,_,_,_,_,_,_,_] , [_,_,_,_,_,_,_,_,_,_] , [_,_,_,_,_,_,_,_,_,0] ],*/ /* [ [_,_,_,_,_,_,_,_,_,4] , [_,_,_,_,_,_,_,_,_,33] , [_,_,1,_,_,_,_,_,_,20] , [_,_,_,_,_,_,_,_,_,17] , [_,_,_,_,5,_,_,_,_,26] , [_,_,_,_,_,_,_,_,_,10] , [_,_,_,_,_,_,9,_,_,16] , [_,_,_,_,_,_,_,_,_,24] , [_,_,_,_,_,_,_,_,_,0] , [8,4,17,35,14,13,3,10,25,0] ],*/ /* Puz = [ [_,_,_,_,_,_,_,_,_] , [_,_,_,_,_,_,_,_,_] , [_,_,_,_,_,_,_,_,_] , [_,_,_,_,_,_,_,_,_] , [_,_,_,_,_,_,_,_,_] , [_,_,_,_,_,_,_,_,_] , [_,_,_,_,_,_,_,_,_] , [_,_,_,_,_,_,_,_,_] , [_,_,_,_,_,_,_,_,_] ],*/