Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.forth > #59183 > unrolled thread

Rosetta: Zebra puzzle (2.)

Started by"Robert L." <No_spamming@noWhere_7073.org>
First post2017-05-13 17:26 +0000
Last post2017-05-13 17:26 +0000
Articles 1 — 1 participant

Back to article view | Back to comp.lang.forth


Contents

  Rosetta: Zebra puzzle (2.) "Robert L." <No_spamming@noWhere_7073.org> - 2017-05-13 17:26 +0000

#59183 — Rosetta: Zebra puzzle (2.)

From"Robert L." <No_spamming@noWhere_7073.org>
Date2017-05-13 17:26 +0000
SubjectRosetta: Zebra puzzle (2.)
Message-ID<of7fl2$pmp$1@gioia.aioe.org>
> There are five houses.
> The English man lives in the red house.
> The Swede has a dog.
> The Dane drinks tea.
> The green house is immediately to the left of the white house.
> They drink coffee in the green house.
> The man who smokes Pall Mall has birds.
> In the yellow house they smoke Dunhill.
> In the middle house they drink milk.
> The Norwegian lives in the first house.
> The man who smokes Blend lives in the house next to the house with cats.
> In a house next to the house where they have a horse, they smoke Dunhill.
> The man who smokes Blue Master drinks beer.
> The German smokes Prince.
> The Norwegian lives next to the blue house.
> They drink water in a house next to the house where they smoke Blend. 
> 
> 
> The question is, who owns the zebra? 
> 
> Additionally, list the solution for all the houses.
> Optionally, show the solution is unique.

def build_permutations things
  if block_given?
    things.permutation.select{|x| yield x}
  else
    things.permutation.to_a
  end
end

def distance(x, xs, y, ys)
  ys.index(y) - xs.index(x) 
end

def same_house?( *args )
  0 == distance( *args )
end

def next_to?( *args )
  1 == distance( *args ).abs
end

build_permutations(%i(danish english german norwegian swedish)){|ary|
  ary[0] == :norwegian}.
product(build_permutations(%i(beer coffee milk tea water)){|ary|
  ary[2] == :milk}).
select{|men,drinks| same_house?(:danish,men, :tea,drinks)}.
product(build_permutations(%i(blue green red white yellow)){|ary|
  1 == distance(:green,ary, :white,ary)}).
map{|a,b| a + [b]}.
select{|men,drinks,colors|
  same_house?(:english,men, :red,colors)  and
  same_house?(:coffee,drinks, :green,colors)  and
  next_to?(:norwegian,men, :blue,colors)}.
product(build_permutations(%i(blend blue_master dunhill pall_mall prince))).
map{|a,b| a + [b]}.
select{|men,drinks,colors,smokes|
  same_house?(:german,men, :prince,smokes)  and
  same_house?(:dunhill,smokes, :yellow,colors)  and
  same_house?(:blue_master,smokes, :beer,drinks)  and
  next_to?(:water,drinks, :blend,smokes)}.
product(build_permutations(%i(birds cats dog horse zebra))).
map{|a,b| a + [b]}.
select{|men,drinks,colors,smokes,pets|
  same_house?(:swedish,men, :dog,pets)  and
  same_house?(:pall_mall,smokes, :birds,pets)  and
  next_to?(:cats,pets, :blend,smokes)  and
  next_to?(:horse,pets, :dunhill,smokes)}.
each{|ary| puts ary.transpose.map{|a| a.join " "}; puts}

norwegian water yellow dunhill cats
danish tea blue blend horse
english milk red pall_mall birds
german coffee green prince zebra
swedish beer white blue_master dog

In Forth?

-- 
Jews totally run Hollywood....  But I don't care if Americans think we're
running the news media, Hollywood, Wall Street, or the government.  I just care
that we get to keep running them. --- Joel Stein
articles.latimes.com/2008/dec/19/opinion/oe-stein19
archive.org/download/DavidDukeTv/DoJewsControlTheMediaTheLaTimesSaysYes.flv

[toc] | [standalone]


Back to top | Article view | comp.lang.forth


csiph-web