Skip to content

Instantly share code, notes, and snippets.

@a-y-khan
Last active March 11, 2020 06:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save a-y-khan/8ee45012baea9bf0aa867ca76794e7cc to your computer and use it in GitHub Desktop.
Save a-y-khan/8ee45012baea9bf0aa867ca76794e7cc to your computer and use it in GitHub Desktop.
Sudoku solution from Seven Languages in Seven Weeks, SWI-Prolog version
%% https://www.swi-prolog.org/man/clpfd.html
:- use_module(library(clpfd)).
%% ---
%% Excerpted from "Seven Languages in Seven Weeks",
%% published by The Pragmatic Bookshelf.
%% Copyrights apply to this code. It may not be used to create training material,
%% courses, books, articles, and the like. Contact us if you are in doubt.
%% We make no guarantees that this code is fit for any purpose.
%% Visit http://www.pragmaticprogrammer.com/titles/btlang for more book information.
%% ---
valid([]).
valid([Head|Tail]) :-
%fd_all_different(Head),
all_distinct(Head),
valid(Tail).
sudoku(Puzzle, Solution) :-
Solution = Puzzle,
Puzzle = [S11, S12, S13, S14,
S21, S22, S23, S24,
S31, S32, S33, S34,
S41, S42, S43, S44],
%fd_domain(Solution, 1, 4),
Solution ins 1..4,
Row1 = [S11, S12, S13, S14],
Row2 = [S21, S22, S23, S24],
Row3 = [S31, S32, S33, S34],
Row4 = [S41, S42, S43, S44],
Col1 = [S11, S21, S31, S41],
Col2 = [S12, S22, S32, S42],
Col3 = [S13, S23, S33, S43],
Col4 = [S14, S24, S34, S44],
Square1 = [S11, S12, S21, S22],
Square2 = [S13, S14, S23, S24],
Square3 = [S31, S32, S41, S42],
Square4 = [S33, S34, S43, S44],
valid([Row1, Row2, Row3, Row4,
Col1, Col2, Col3, Col4,
Square1, Square2, Square3, Square4]).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment