Courses/CS 460/Fall 2005/Homework/Jeff Bailey/Oct 29

From CSWiki

Jump to: navigation, search

Contents


I will not be in class, but here are my puzzles

[edit] Poodle Parlor

Last Friday two women (Mrs. Chow and Mrs. Mastiff) and two men (Mr. Shepherd and Mr. Basset) took their poodles to the Poodle Parlor for a complete grooming. Each has a minature poodle of a different color - apricot, chocolate, silver, or white (one of whom is named Zsa-Zsa). From the clues, determine the owner of each dog, the color of each poodle, and its name..

  • Mrs. Chow doesn’t own the silver poodle.
  • The apricot poodles isn’t the one that belongs to Mr. Basset.
  • The white poodle is named Mickey.
  • Mrs. Mastiff and Mr. Basset are the owners of the chocolate poodle and Fifi, in some order.
  • The silver poodle is named Pepi LaPue.
declare

% provided helper functions
proc {AllInstantiated Xs Values}
   {ForAll Xs proc {$ X} {IsIn X Values} end}
end

proc {Append ?Xs ?Ys ?Zs}
   choice
      Xs = nil 
      Ys = Zs
   [] X Xr in
      Xs = X | Xr
      Zs = X | {Append Xr Ys}
   end
end

fun {GetFields Field Elts}
   {Map Elts fun {$ Elt} Elt.Field end}
end

fun {IndexOf X Xs}
   fun {IndexOf3 X Xs N}
      case Xs of
	 nil then fail
      [] Y | Rest then
	 if X == Y then N else {IndexOf3 X Rest N+1} end
      end
   end
in
   {IndexOf3 X Xs 1}
end

proc {IsIn ?X Xs} {Append _ X|_ Xs} end

proc {IsAnElt ?X Elts} {IsIn X {Record.toList Elts}} end

proc {AllDistinct List}
   L = {Length List} in
   for I in 1; I =< L-1; I+1 do
      for J in I+1; J =< L; J+1 do
	 {NotEqual {Nth List I} {Nth List J}}
      end
   end
end

proc {NotEqual X Y} thread X == Y = false end end

proc {Precedes ?X1 ?X2 Xs}
   thread {IndexOf X1 Xs} < {IndexOf X2 Xs} = true end
end

fun {Match R1 R2}
   {Record.all
    {Record.zip R1 R2 fun {$ A B} A == B end}
    fun {$ X} X == true end}
end

proc {Propagate Condition Result R}
   thread if {Match Condition R} then {UnifyRecs Result R} end end    
end

proc {PropagateAll Condition Result Rec}
   {Record.forAll Rec proc {$ R} {Propagate Condition Result R} end}
end

proc {UnifyRecs R1 R2}
   {Record.zip R1 R2 fun {$ A B} A = B end _}
end

local
   RefColors = [apricot chocolate silver white]
   RefNames = [pepi fifi mickey zsazsa]

   % Mrs. Chow doesnt own the silver poodle.
   proc {Clue1 Xs}
      {NotEqual Xs.chow.color silver}      
   end

   % The apricot poodles isnt the one that belongs to Mr. Basset.
   proc {Clue2 Xs}
      {NotEqual Xs.basset.color apricot}
   end

   % The white poodle is named mickey
   proc {Clue3 Xs}
      {PropagateAll properties(color:white) properties(name:mickey) Xs}
   end

   % Mrs. Mastiff and Mr. Basset are the owners of the chocolate poodle and Fifi, in some order.
   proc {Clue4 Xs}
      choice
	 Xs.mastiff.color = chocolate
	 Xs.basset.name = fifi
      []
	 Xs.basset.color = chocolate
	 Xs.mastiff.name = fifi
      end

      % choclate is not mickey or fifi, must be one of remaining two
      choice
	 {PropagateAll properties(color:chocolate) properties(name:pepi) Xs}
      []
	 {PropagateAll properties(color:chocolate) properties(name:zsazsa) Xs}
      end

      % fifi isnt chocolate or white
      choice
	 {PropagateAll properties(name:fifi) properties(color:apricot) Xs}
      []
	 {PropagateAll properties(name:fifi) properties(color:silver) Xs}
      end      
   end

   % The silver poodle is named Pepi LaPue.
   proc {Clue5 Xs}
      {PropagateAll properties(name:pepi) properties(color:silver) Xs}
   end
     
   fun {PoodleParlor}
      Elts = {MakeRecord owners [basset chow mastiff shepherd]}
      Owners = {Record.toList Elts}
      Colors Names
   in
      % create all the properties
      {Record.forAll Elts proc {$ S} S = {Record.make properties [color name]} end}

      % get list of colors and names
      Colors = {GetFields color Owners}
      {AllDistinct Colors}
      Names = {GetFields name Owners}
      {AllDistinct Names}
     
      % run clues
      {Clue1 Elts}
      {Clue2 Elts}
      {Clue3 Elts}
      {Clue4 Elts}
      {Clue5 Elts}

      % make sure everything got a value
      {AllInstantiated Colors RefColors} 
      {AllInstantiated Names RefNames}

      Elts    
   end

in
   {Browse {SearchAll PoodleParlor}} 
end

[edit] Carnivale Games

Last Saturady night four young men (Aaron, Casey, Eldon, and Greg) took their dates (Beth, Dixie, Faye, and Heidi) to the Riverdale Winter Carnival. While there, each couple played one of the many games available along the small Midway trying to win a prize, but unfortunately, no one had any luck. From the clues, determine which game (one was the Duck Shoot) each couple tried their luck at.

  • Neither Faye and her date nor Aaron and his date, Heidi, stopped by the Dunk Tank.
  • Beth and her date (who was not Eldon) tossed rings trying to get them on pegs on a board, to no avail.
  • Greg and his date tried their luck at winning a prize by tossing dimes into flat glass dishes.
declare

% provided helper functions
proc {AllInstantiated Xs Values}
   {ForAll Xs proc {$ X} {IsIn X Values} end}
end

proc {Append ?Xs ?Ys ?Zs}
   choice
      Xs = nil 
      Ys = Zs
   [] X Xr in
      Xs = X | Xr
      Zs = X | {Append Xr Ys}
   end
end

fun {GetFields Field Elts}
   {Map Elts fun {$ Elt} Elt.Field end}
end

fun {IndexOf X Xs}
   fun {IndexOf3 X Xs N}
      case Xs of
	 nil then fail
      [] Y | Rest then
	 if X == Y then N else {IndexOf3 X Rest N+1} end
      end
   end
in
   {IndexOf3 X Xs 1}
end

proc {IsIn ?X Xs} {Append _ X|_ Xs} end

proc {IsAnElt ?X Elts} {IsIn X {Record.toList Elts}} end

proc {AllDistinct List}
   L = {Length List} in
   for I in 1; I =< L-1; I+1 do
      for J in I+1; J =< L; J+1 do
	 {NotEqual {Nth List I} {Nth List J}}
      end
   end
end

proc {NotEqual X Y} thread X == Y = false end end

proc {Precedes ?X1 ?X2 Xs}
   thread {IndexOf X1 Xs} < {IndexOf X2 Xs} = true end
end

fun {Match R1 R2}
   {Record.all
    {Record.zip R1 R2 fun {$ A B} A == B end}
    fun {$ X} X == true end}
end

proc {Propagate Condition Result R}
   thread if {Match Condition R} then {UnifyRecs Result R} end end    
end

proc {PropagateAll Condition Result Rec}
   {Record.forAll Rec proc {$ R} {Propagate Condition Result R} end}
end

proc {UnifyRecs R1 R2}
   {Record.zip R1 R2 fun {$ A B} A = B end _}
end

local
   RefDates = [beth dixie faye heidi]
   RefGames = [dimetoss duckshoot dunktank ringtoss]

   % Neither Faye and her date nor Aaron and his date, Heidi, stopped by
   % the Dunk Tank.
   proc {Clue1 Xs}
      Xs.aaron.date = heidi
      {NotEqual Xs.aaron.game dunktank}
      choice
	 Xs.casey.date = faye
	 {NotEqual Xs.casey.game dunktank}
      []
	 Xs.eldon.date = faye
	 {NotEqual Xs.eldon.game dunktank}
      []
	 Xs.greg.date = faye
	 {NotEqual Xs.greg.game dunktank}
      end    
   end

   % Beth and her date (who was not Eldon) tossed rings trying to get them
   % on pegs on a board, to no avail.
   proc {Clue2 Xs}
      {NotEqual Xs.eldon.date beth}
      {NotEqual Xs.eldon.game ringtoss}
      {PropagateAll properties(date:beth) properties(game:ringtoss) Xs}
   end

   % Greg and his date tried their luck at winning a prize by tossing dimes
   % into flat glass dishes.
   proc {Clue3 Xs}
      Xs.greg.game = dimetoss
   end

   fun {CarnivaleGames}
      Elts = {MakeRecord boys [aaron casey eldon greg]}
      Boys = {Record.toList Elts}
      Dates Games
   in
      % create all the properties
      {Record.forAll Elts proc {$ S} S = {Record.make properties [date game]} end}

      % guarantee uniqueness of properties
      Dates = {GetFields date Boys}
      {AllDistinct Dates}
      Games = {GetFields game Boys}
      {AllDistinct Games}
     
      % run clues
      {Clue1 Elts}
      {Clue2 Elts}
      {Clue3 Elts}
      
      % make sure everything got a value
      {AllInstantiated Dates RefDates} 
      {AllInstantiated Games RefGames}

      Elts    
   end

in
   {Browse {SearchAll CarnivaleGames}} 
end

[edit] Calamity Jane

Jane is a wonderful person but her friends call her Calamity Jane as it seems that she is forever having little accidents - simply because she doesn’t watch where she is going or what is around her. Last Friday she had a date with a gentleman she’d been hoping would ask her out, and so when she arrived home from work that even to prepare for her date, her mind was on other things and before he arrived to pick her up two hours later, she had four mishap. Each occurred in a different room of her house - bedroom room, dining room, kitchen and living room. She received a minor injury to a different part of her body (chin, elbow, knee, and toe). Thankfully, with clothing and a little make-up, her date was unaware of her bruises and they had a great time! From the clues, determine the room where Jane had each accident and the cause of it, as well as the area on her body which was slightly injured.

  • It was in the bedroom where Jane received an injury while opening her closet door.
  • Jane received on injury by tripping over a chair, but this wasn’t in her kitchen.
  • When Jane hurt her chin, it wasn’t when she tripped over her cat’s bowl.
  • It wasn’t in the living room where Jane received the injury to either her chin or knee.
  • The injury to Jane’s toe occurred in her dining room.
  • Jane injured her elbow on a bookcase while she brushed her hair
declare

% provided helper functions
proc {AllInstantiated Xs Values}
   {ForAll Xs proc {$ X} {IsIn X Values} end}
end

proc {Append ?Xs ?Ys ?Zs}
   choice
      Xs = nil 
      Ys = Zs
   [] X Xr in
      Xs = X | Xr
      Zs = X | {Append Xr Ys}
   end
end

fun {GetFields Field Elts}
   {Map Elts fun {$ Elt} Elt.Field end}
end

fun {IndexOf X Xs}
   fun {IndexOf3 X Xs N}
      case Xs of
	 nil then fail
      [] Y | Rest then
	 if X == Y then N else {IndexOf3 X Rest N+1} end
      end
   end
in
   {IndexOf3 X Xs 1}
end

proc {IsIn ?X Xs} {Append _ X|_ Xs} end

proc {IsAnElt ?X Elts} {IsIn X {Record.toList Elts}} end

proc {AllDistinct List}
   L = {Length List} in
   for I in 1; I =< L-1; I+1 do
      for J in I+1; J =< L; J+1 do
	 {NotEqual {Nth List I} {Nth List J}}
      end
   end
end

proc {NotEqual X Y} thread X == Y = false end end

proc {Precedes ?X1 ?X2 Xs}
   thread {IndexOf X1 Xs} < {IndexOf X2 Xs} = true end
end

fun {Match R1 R2}
   {Record.all
    {Record.zip R1 R2 fun {$ A B} A == B end}
    fun {$ X} X == true end}
end

proc {Propagate Condition Result R}
   thread if {Match Condition R} then {UnifyRecs Result R} end end    
end

proc {PropagateAll Condition Result Rec}
   {Record.forAll Rec proc {$ R} {Propagate Condition Result R} end}
end

proc {UnifyRecs R1 R2}
   {Record.zip R1 R2 fun {$ A B} A = B end _}
end

local
   RefObjects = [bookcase bowl chair door]
   RefInjuries = [chin elbow knee toe]

   % It was in the bedroom where Jane received an injury while opening her closet door.
   proc {Clue1 Xs}
      Xs.bedroom.object = door
   end

   % Jane received on injury by tripping over a chair, but this wasnt in her kitchen.
   proc {Clue2 Xs}
      {NotEqual Xs.kitchen.object chair}
   end

   % When Jane hurt her chin, it wasnt when she tripped over her cats bowl.
   proc {Clue3 Xs}
      choice
	 {PropagateAll properties(object:bookcase) properties(injury:chin) Xs}
      []
	 {PropagateAll properties(object:chair) properties(injury:chin) Xs}
      []
	 {PropagateAll properties(object:door) properties(injury:chin) Xs}
      end
   end

   % It wasnt in the living room where Jane received the injury to either her chin or knee.
   proc {Clue4 Xs}
      {NotEqual Xs.living.injury chin}
      {NotEqual Xs.living.injury knee}
   end

   % The injury to Janes toe occurred in her dining room.
   proc {Clue5 Xs}
      Xs.dining.injury = toe
   end

   % Jane injured her elbow on a bookcase while she brushed her hair
   proc {Clue6 Xs}
      {PropagateAll properties(object:bookcase) properties(injury:elbow) Xs}
   end
   
   fun {CalamityJane}
      Elts = {MakeRecord rooms [bedroom dining kitchen living]}
      Rooms = {Record.toList Elts}
      Objects Injuries
   in
      % create all the properties
      {Record.forAll Elts proc {$ S} S = {Record.make properties [object injury]} end}

      % get lists, guarantee uniqueness
      Objects = {GetFields object Rooms}
      {AllDistinct Objects}
      Injuries = {GetFields injury Rooms}
      {AllDistinct Injuries}
     
      % run clues
      {Clue1 Elts}
      {Clue2 Elts}
      {Clue3 Elts}
      {Clue4 Elts}
      {Clue5 Elts}
      {Clue6 Elts}
      
      % make sure everything got a value
      {AllInstantiated Objects RefObjects} 
      {AllInstantiated Injuries RefInjuries}

      Elts    
   end

in
   {Browse {SearchAll CalamityJane}} 
end
Personal tools