Toggle navigation
?
users online
Logout
Open hangout
Open chat for current file
% solves this: % https://old.reddit.com/r/learncsharp/comments/18fzdwi/iteration_helpdirection/ :- use_module(library(clpfd)). % Finite domain constraints bits_int([B0,B1,B2,B3,B4,B5], Int) :- Int #= B0*32 + B1*16 + B2*8 + B3*4 + B4*2 + B5. bits_int_rev([B0,B1,B2,B3,B4,B5], Int) :- Int #= B5*32 + B4*16 + B3*8 + B2*4 + B1*2 + B0. diagonal_cells(Board, Diags) :- length(Board, Len), numlist(1,Len,Indices), maplist(nth1, Indices, Board, Diags, _). board(Rows, Ints) :- length(Rows, 6) % board of nested rows ,maplist(same_length(Rows), Rows) ,append(Rows, Cells) % all board cells are bits ,Cells ins 0..1 ,same_length(Rows, Rn) % and int for each row ,maplist(bits_int, Rows, Rn) % the bits in each row map to one int ,same_length(Rows, Rnrev) % int for each row reversed ,maplist(bits_int_rev, Rows, Rnrev) % reversed bits -> int ,transpose(Rows, Cols) % columns ,same_length(Cols, Cn) % int for each column ,maplist(bits_int, Cols, Cn) % bits in a column map to one int ,same_length(Cols, Cnrev) % int for reversed cols ,maplist(bits_int_rev, Cols, Cnrev) % col-bit-reverse to those. ,Diags = [D1,Dr1,D2,Dr2] ,diagonal_cells(Rows, Diag1) ,bits_int(Diag1, D1) ,bits_int_rev(Diag1, Dr1) ,maplist(reverse, Rows, RowRev) ,diagonal_cells(RowRev, Diag2) ,bits_int(Diag2, D2) ,bits_int_rev(Diag2, Dr2) ,flatten([Rn,Rnrev,Cn,Cnrev,Diags], Ints) % gather all the ints ,all_distinct(Ints) % no dupes in row, row-rev, col col-rev, diags . display_([]). display_([R|Rs]) :- bits_int_rev(R, Rn), bits_int(R, N), format('~|~` t~d~2+ <- ', [Rn]), display_list(R), format(' -> ~w~n', [N]), display_(Rs). display_list([]). display_list([L|Ls]) :- format('~|~` t~d~2+ ', [L]), display_list(Ls). show_board(Rows) :- diagonal_cells(Rows, Diag1), bits_int(Diag1, Dn1), bits_int_rev(Diag1, Dnr1), maplist(reverse, Rows, RowRev), diagonal_cells(RowRev, Diag2), bits_int(Diag2, Dn2), bits_int_rev(Diag2, Dnr2), transpose(Rows, Cols), maplist(bits_int_rev, Cols, Up), format('~w ~w ~w~n', [Dnr1, Up, Dnr2]), display_(Rows), maplist(bits_int, Cols, Down), format('~w ~w ~w~n', [Dn2, Down, Dn1]) . solve() :- board(Board1Rows, B1Ints), board(Board2Rows, B2Ints), flatten([B1Ints, B2Ints], AllInts), all_distinct(AllInts), AllInts ins 1..62, maplist(#\=(12), AllInts), maplist(#\=(18), AllInts), maplist(#\=(30), AllInts), maplist(#\=(33), AllInts), maplist(#\=(45), AllInts), maplist(#\=(51), AllInts), append(Board1Rows, B1s), append(Board2Rows, B2s), flatten([B1s, B2s,AllInts], AllCells), labeling([ff], AllCells), show_board(Board1Rows), show_board(Board2Rows).