Re: New riddle

From: Vladimir Nesterovsky (vnestr_at_netvision.net.il)
Date: 11/11/03

  • Next message: Alexei A. Morozov: "Re: New riddle"
    Date: Tue, 11 Nov 2003 14:25:06 +0200
    
    

    Well, if you still need the answer,... :-)
    (I haven't had time to look at this riddle before...)

    This time it is really simple - in a sence that it is
    a straightforward application of a general principle
    which these two puzzles show very well has a great value -----
    identify your domains of values and apply some given
    constraints (facts and relations) onto them to filter
    the wrong ones out (for efficiency, apply the constraints
    as soon as possible). It worked with the janitor puzzle and
    it works here immediately:

    select([A|As],S):- % select from list in arbitrary order
      select(A,S,S1),select(As,S1). % uses select/3
    select([],[]).

    house_colors([blue,green,yellow,red,brown]).
    sells_goods([wine,meat,cake,toys,trees]).
    person_names([keith,larry,sarah,kumi,gary]).
    house_kinds([insurance_company,garden_assosiation,flower_shop,
                 toy_store,snow_lovers]).

    house(house(_N,_C,_G,_P,_K)).
    has_goods(H,Goods):- arg(3,H,Goods).
    of_kind(H,Kind):- arg(5,H,Kind).

    show_houses:- % SWI-Prolog version
      houses(Hs), % to show all solutions
      show_houses(Hs),
      write('\n--------------'), nl, fail.

    show_houses([house(_,C,G,P,K)|Hs]):-
      writef('\n %10l %10l %10l %w ', [C,G,P,K] ),
      show_houses(Hs).
    show_houses([]).

    houses(Houses):- % find the answer to the riddle
      house_colors(Cs),sells_goods(Gs),
        person_names(Ps),house_kinds(Ks),
      Houses = [H1,H2,H3,H4,H5], % there are five houses
      H1 = house(1,C1,G1,P1,K1),
      H2 = house(2,C2,G2,P2,K2),
      H3 = house(3,C3,G3,P3,K3),
      H4 = house(4,C4,G4,P4,K4),
      H5 = house(5,C5,G5,P5,K5),

      house(HW),house(HTS),house(HCK),house(HGA), % ... for [7]
        has_goods(HW,wine),of_kind(HTS,toy_store),
        has_goods(HCK,cake),of_kind(HGA,garden_assosiation),
      HB = house(_,brown,_,sarah,_), % ... for [5]
        HI = house(_,_,_,_,insurance_company),
      HGG = house(_,green,_,gary,_), % ... for [0]

      of_kind(H1,flower_shop), %[1]
      !, % all constants defined - search from here

      ( H1=HGG ; H5=HGG ), %[0]
      append(_,[house(_,GRC1,_,SKP1,_),house(_,GRC2,_,SKP2,_)|_],Houses),
          select([GRC1,GRC2],[red,brown]),
          select([SKP1,SKP2],[kumi,sarah]), %[2]
      member(G1,[toys,trees]), %[3]
      member(house(_,red,meat,_,NotGA),Houses),
        member(house(_,NotYE,wine,larry,
                     garden_assosiation),Houses), %[4]
      ( append(_,[HI,HB|_],Houses) ;
          append(_,[HB,HI|_],Houses) ), %[5]
      member(house(_,_,KG,keith,snow_lovers),Houses),
        member(KG,[meat,cake]), %[6]
      append(_,[HTS,HW|_],Houses),
        append(_,[HGA,HCK|_],Houses), %[7]

      select([K1,K2,K3,K4,K5],Ks),
      NotGA \= garden_assosiation, % negative constraint --
      select([C1,C2,C3,C4,C5],Cs),
      NotYE \= yellow, % -- after instantiation
      select([G1,G2,G3,G4,G5],Gs),
      select([P1,P2,P3,P4,P5],Ps).

    The select/2 makes sure each property is picked only once
    from the pool of available values.

    HTH

    On Tue, 28 Oct 2003 16:34:35 +0100, "Damion" <global_killer@web.de> wrote in message
    news:<bnm2ab$1mth$1@ariadne.rz.tu-clausthal.de> :

    >I'm so sorry
    >
    >I forgot one more rule ;(((
    >
    >#0 in the green house lives gary, and it is house no1 or house no5
    >
    >damion
    >


  • Next message: Alexei A. Morozov: "Re: New riddle"