Solving Sudoku with Prolog (2016)
Triska on the front page!  Sweet.<p>It was in large part thanks to &quot;The Power of Prolog&quot; that I was able to figure out a nice solution to the &quot;Zebra&quot; puzzle:<p><pre><code>    &#x2F;*

    Via https:&#x2F;&#x2F;www.metalevel.at&#x2F;prolog&#x2F;puzzles

    The &quot;Zebra Puzzle&quot; https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Zebra_Puzzle

    -    There are five houses.
    -    The Englishman lives in the red house.
    -    The Spaniard owns the dog.
    -    Coffee is drunk in the green house.
    -    The Ukrainian drinks tea.
    -    The green house is immediately to the right of the ivory house.
    -    The Old Gold smoker owns snails.
    -    Kools are smoked in the yellow house.
    -    Milk is drunk in the middle house.
    -    The Norwegian lives in the first house.
    -    The man who smokes Chesterfields lives in the house next to the man with the fox.
    -    Kools are smoked in the house next to the house where the horse is kept.
    -    The Lucky Strike smoker drinks orange juice.
    -    The Japanese smokes Parliaments.
    -    The Norwegian lives next to the blue house.

    Now, who drinks water? Who owns the zebra?

    In the interest of clarity, it must be added that each of the five houses is painted
    a different color, and their inhabitants are of different national
    extractions, own different pets, drink different beverages and smoke
    different brands of American cigarets [sic].
    One other thing: in statement 6, right means your right.

    *&#x2F;
    :- use_module(library(clpfd)).


    house_next_to(H0, H1) :- abs(H0 - H1) #= 1.
    &#x2F;* Thanks https:&#x2F;&#x2F;www.metalevel.at&#x2F;prolog&#x2F;puzzles *&#x2F;

    puzzle(Vars) :- 
 
     Vars = [GreenHouse, RedHouse, IvoryHouse, YellowHouse, BlueHouse,
      Englishman, Spaniard, Ukrainian, Norwegian, Japanese,
      Dog, Snails, Fox, Horse, Zebra,
      Coffee, Tea, Milk, OrangeJuice, Water,
      OldGold, Kools, Chesterfields, LuckyStrike, Parliaments
     ],
 
     Vars ins 1..5,
 
     all_distinct([GreenHouse, RedHouse, IvoryHouse, YellowHouse, BlueHouse]),
     all_distinct([Englishman, Spaniard, Ukrainian, Norwegian, Japanese]),
     all_distinct([Dog, Snails, Fox, Horse, Zebra]),
     all_distinct([Coffee, Tea, Milk, OrangeJuice, Water]),
     all_distinct([OldGold, Kools, Chesterfields, LuckyStrike, Parliaments]),
 
     Englishman #= RedHouse,
     Spaniard #= Dog,
     Coffee #= GreenHouse,
     Ukrainian #= Tea,
     GreenHouse #= 1 + IvoryHouse,
     OldGold #= Snails,
     Kools #= YellowHouse,
     Milk #= 3,
     Norwegian #= 1,
     house_next_to(Chesterfields, Fox),
     house_next_to(Kools, Horse),
     LuckyStrike #= OrangeJuice,
     Japanese #= Parliaments,
     house_next_to(Norwegian, BlueHouse).


    &#x2F;*

    Then to print out the solution after loading the program above use:

    puzzle([
     GreenHouse, RedHouse, IvoryHouse, YellowHouse, BlueHouse,
     Englishman, Spaniard, Ukrainian, Norwegian, Japanese,
     Dog, Snails, Fox, Horse, Zebra,
     Coffee, Tea, Milk, OrangeJuice, Water,
     OldGold, Kools, Chesterfields, LuckyStrike, Parliaments]),
    label([
     GreenHouse, RedHouse, IvoryHouse, YellowHouse, BlueHouse,
     Englishman, Spaniard, Ukrainian, Norwegian, Japanese,
     Dog, Snails, Fox, Horse, Zebra,
     Coffee, Tea, Milk, OrangeJuice, Water,
     OldGold, Kools, Chesterfields, LuckyStrike, Parliaments]).

    *&#x2F;


</code></pre>
See also &quot;Boring Sudoku&quot;:<p><pre><code>    &#x2F;*
    Boring Prolog Sudoku.
    *&#x2F;
    :- use_module(library(clpfd)).


    puzzle(Vars) :-

        Vars = [
            A11, A12, A13, A14, A15, A16, A17, A18, A19,
            A21, A22, A23, A24, A25, A26, A27, A28, A29,
            A31, A32, A33, A34, A35, A36, A37, A38, A39,
            A41, A42, A43, A44, A45, A46, A47, A48, A49,
            A51, A52, A53, A54, A55, A56, A57, A58, A59,
            A61, A62, A63, A64, A65, A66, A67, A68, A69,
            A71, A72, A73, A74, A75, A76, A77, A78, A79,
            A81, A82, A83, A84, A85, A86, A87, A88, A89,
            A91, A92, A93, A94, A95, A96, A97, A98, A99],

        Vars ins 1..9,

        all_distinct([A11, A12, A13, A14, A15, A16, A17, A18, A19]),
        all_distinct([A21, A22, A23, A24, A25, A26, A27, A28, A29]),
        all_distinct([A31, A32, A33, A34, A35, A36, A37, A38, A39]),
        all_distinct([A41, A42, A43, A44, A45, A46, A47, A48, A49]),
        all_distinct([A51, A52, A53, A54, A55, A56, A57, A58, A59]),
        all_distinct([A61, A62, A63, A64, A65, A66, A67, A68, A69]),
        all_distinct([A71, A72, A73, A74, A75, A76, A77, A78, A79]),
        all_distinct([A81, A82, A83, A84, A85, A86, A87, A88, A89]),
        all_distinct([A91, A92, A93, A94, A95, A96, A97, A98, A99]),

        all_distinct([A11, A21, A31, A41, A51, A61, A71, A81, A91]),
        all_distinct([A12, A22, A32, A42, A52, A62, A72, A82, A92]),
        all_distinct([A13, A23, A33, A43, A53, A63, A73, A83, A93]),
        all_distinct([A14, A24, A34, A44, A54, A64, A74, A84, A94]),
        all_distinct([A15, A25, A35, A45, A55, A65, A75, A85, A95]),
        all_distinct([A16, A26, A36, A46, A56, A66, A76, A86, A96]),
        all_distinct([A17, A27, A37, A47, A57, A67, A77, A87, A97]),
        all_distinct([A18, A28, A38, A48, A58, A68, A78, A88, A98]),
        all_distinct([A19, A29, A39, A49, A59, A69, A79, A89, A99]),

        all_distinct([A11, A21, A31, A12, A22, A32, A13, A23, A33]),
        all_distinct([A41, A51, A61, A42, A52, A62, A43, A53, A63]),
        all_distinct([A71, A81, A91, A72, A82, A92, A73, A83, A93]),
        all_distinct([A14, A24, A34, A15, A25, A35, A16, A26, A36]),
        all_distinct([A44, A54, A64, A45, A55, A65, A46, A56, A66]),
        all_distinct([A74, A84, A94, A75, A85, A95, A76, A86, A96]),
        all_distinct([A17, A27, A37, A18, A28, A38, A19, A29, A39]),
        all_distinct([A47, A57, A67, A48, A58, A68, A49, A59, A69]),
        all_distinct([A77, A87, A97, A78, A88, A98, A79, A89, A99]).</code></pre>