News for hackers. Stuff that matters.
Re:Logic programming is overrated, at least for logic puzzle... by tannhaeuser on Thursday June 01, 2023 @04:53PM (#36157181)
How is that "easier" than the following straightforward Prolog code?

    zebra(Houses) :-
        houses(Houses),
        member(house(red, english, _, _, _), Houses),
        member(house(_, spanish, dog, _, _), Houses),
        member(house(green, _, _, coffee, _), Houses),
        member(house(_, ukrainian, _, tea, _), Houses),
        right_of(house(green,_,_,_,_),
          house(ivory,_,_,_,_), Houses),
        member(house(_, _, snails, _, winstons), Houses),
        member(house(yellow, _, _, _, kools), Houses),
        Houses = [_, _, house(_, _, _, milk, _), _,_],
        Houses = [house(_, norwegian, _, _, _)|_],
        next_to(house(_,_,_,_,chesterfields),
          house(_,_,fox,_,_), Houses),
        next_to(house(_,_,_,_,kools),
          house(_,_,horse,_,_), Houses),
        member(house(_, _, _, orange_juice, lucky_strikes), Houses),
        member(house(_, japanese, _, _, parliaments), Houses),
        next_to(house(_,norwegian,_,_,_),
          house(blue,_,_,_,_), Houses),
        member(house(_, _, zebra, _, _), Houses),
        member(house(_, _, _, water, _), Houses).

    houses([
        house(_, _, _, _, _),
        house(_, _, _, _, _),
        house(_, _, _, _, _),
        house(_, _, _, _, _),
        house(_, _, _, _, _)]).
    
    right_of(A, B, [B, A | _]).
    right_of(A, B, [_ | Y]) :- right_of(A, B, Y).
    
    next_to(A, B, [A, B | _]).
    next_to(A, B, [B, A | _]).
    next_to(A, B, [_ | Y]) :- next_to(A, B, Y).
    
    member(X, [X|_]).
    member(X, [_|Y]) :- member(X, Y).
    
    ?- zebra(Houses) 
To check it out yourself, copy/paste this into https://quantumprolog.sgml.io/browser-demo/browser-demo.html and execute on your browser.
Re:Logic programming is overrated, at least for logic puzzle... by Jtsummers on Thursday June 01, 2023 @05:31PM (#36157644)

  solution(Pairs, Water, Zebra, Vs) :-
          Table   = [Houses,Nations,Drinks,Smokes,Animals],
          Houses  = [Red,Green,Yellow,Blue,Ivory],
          Nations = [England,Spain,Ukraine,Norway,Japan],
          Names   = [england,spain,ukraine,norway,japan],
          Drinks  = [Coffee,Milk,OrangeJuice,Tea,Water],
          Smokes  = [OldGold,Kools,Chesterfield,LuckyStrike,Parliaments],
          Animals = [Dog,Snails,Horse,Fox,Zebra],
          pairs_keys_values(Pairs, Nations, Names),
          maplist(all_distinct, Table),
          append(Table, Vs),
          Vs ins 1..5,
          England #= Red,               % hint 1
          Spain #= Dog,                 % hint 2
          Coffee #= Green,              % hint 3
          Ukraine #= Tea,               % hint 4
          Green #= Ivory + 1,           % hint 5
          OldGold #= Snails,            % hint 6
          Kools #= Yellow,              % hint 7
          Milk #= 3,                    % hint 8
          Norway #= 1,                  % hint 9
          next_to(Chesterfield, Fox),   % hint 10
          next_to(Kools, Horse),        % hint 11
          LuckyStrike #= OrangeJuice,   % hint 12
          Japan #= Parliaments,         % hint 13
          next_to(Norway, Blue).        % hint 14

  next_to(H, N) :- abs(H-N) #= 1.
It's subjective, but the above is far clearer about the rules to me. Compare `next_to` in both:

  next_to(Kools, Horse)
  next_to(house(_,_,_,_,kools),
          house(_,_,horse,_,_), Houses)
The former is much more straightforward as a representation of the hint. You don't need to know which entry in a `house` corresponds to cigarette brand and animal, you can just directly compare the two and say Kools is next to Horse.