Created
April 10, 2013 03:00
-
-
Save yangchenyun/5351413 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
################ | |
# Properties | |
################ | |
# | |
# color | |
# red, green, ivory, blue. yellow | |
# nationality | |
# englishman, spaniard, ukrainian, norwegian, japanese | |
# drinks | |
# coffee, tea, milk, orange_juice, water | |
# pets | |
# dog, snail, horse, fox, zebra | |
# smoke | |
# old_gold, kool, chesterfields, lucky_strike, parliaments | |
################ | |
# Conditions | |
################ | |
# 1 There are five houses. | |
# 2 The Englishman lives in the red house. | |
# englishman == red | |
# 3 The Spaniard owns the dog. | |
# spaniard == dog | |
# 4 Coffee is drunk in the green house. | |
# green = coffee | |
# 5 The Ukrainian drinks tea. | |
# ukrainian == tea | |
# 6 The green house is immediately to the right of the ivory house. | |
# green - ivory == 1 | |
# 7 The Old Gold smoker owns snails. | |
# old_gold == snail | |
# 8 Kools are smoked in the yellow house. | |
# kool == yellow | |
# 9 Milk is drunk in the middle house. | |
# milk == 3 | |
# 10 The Norwegian lives in the first house. | |
# norwegian = 1 | |
# 11 The man who smokes Chesterfields lives in the house next to the man with the fox. | |
# (chesterfields - fox).abs == 1 | |
# 12 Kools are smoked in a house next to the house where the horse is kept. | |
# (kool - horse).abs == 1 | |
# 13 The Lucky Strike smoker drinks orange juice. | |
# lucky_strike == orange_juice | |
# 14 The Japanese smokes Parliaments. | |
# japanese == parliaments | |
# 15 The Norwegian lives next to the blue house. | |
# (norwegian - blue).abs == 1 | |
## Solution 1, efficient, but too many nesting | |
orderings = [1, 2, 3, 4, 5].permutation | |
for colors in orderings do | |
red, green, ivory, blue, yellow = colors | |
test = green - ivory == 1 # 6 The green house is immediately to the right of the ivory house. | |
# print "colors:", red, green, ivory, blue, yellow, "\n" if test | |
if test | |
for nationalities in orderings do | |
englishman, spaniard, ukrainian, norwegian, japanese = nationalities | |
test = englishman == red && # 2 The Englishman lives in the red house. | |
norwegian == 1 && # 10 The Norwegian lives in the first house. | |
(norwegian - blue).abs == 1 # 15 The Norwegian lives next to the blue house. | |
# print "nations: ", englishman, spaniard, ukrainian, norwegian, japanese, "\n" if test | |
if test | |
for drinks in orderings do | |
coffee, tea, milk, orange_juice, water = drinks | |
test = green == coffee && # 4 Coffee is drunk in the green house. | |
ukrainian == tea && # 5 The Ukrainian drinks tea. | |
milk == 3 # 9 Milk is drunk in the middle house. | |
# print "drinks: ", coffee, tea, milk, orange_juice, water, "\n" if test | |
if test | |
for pets in orderings do | |
dog, snail, horse, fox, zebra = pets | |
test = spaniard == dog # 3 The Spaniard owns the dog. | |
# print "pets: ", dog, snail, horse, fox, zebra, "\n" if test | |
if test | |
for smokes in orderings do | |
old_gold, kool, chesterfields, lucky_strike, parliaments = smokes | |
test = old_gold == snail && # 7 The Old Gold smoker owns snails. | |
kool == yellow && # 8 Kools are smoked in the yellow house. | |
(chesterfields - fox).abs == 1 && # 11 The man who smokes Chesterfields lives in the house next to the man with the fox. | |
(kool - horse).abs == 1 && # 12 Kools are smoked in a house next to the house where the horse is kept. | |
lucky_strike == orange_juice && # 13 The Lucky Strike smoker drinks orange juice. | |
japanese == parliaments # 14 The Japanese smokes Parliaments. | |
if test | |
puts "final result >>>> " | |
print water, "\n" | |
print zebra, "\n" | |
print "colors:", red, green, ivory, blue, yellow, "\n" | |
print "nations: ", englishman, spaniard, ukrainian, norwegian, japanese, "\n" | |
print "drinks: ", coffee, tea, milk, orange_juice, water, "\n" | |
print "pets: ", dog, snail, horse, fox, zebra, "\n" | |
print "smokes: ", old_gold, kool, chesterfields, lucky_strike, parliaments, "\n" | |
end | |
end | |
end | |
end | |
end | |
end | |
end | |
end | |
end | |
end | |
## Solution 2, but so inefficient, it doesn't so lazy evaluation of lists | |
orderings = [1, 2, 3, 4, 5].permutation.to_a | |
orderings. | |
product(orderings). | |
product(orderings). | |
product(orderings). | |
product(orderings).each do |colors, nationalities, drinks, pets, smokes| | |
red, green, ivory, blue, yellow = colors | |
englishman, spaniard, ukrainian, norwegian, japanese = nationalities | |
coffee, tea, milk, orange_juice, water = drinks | |
old_gold, kool, chesterfields, lucky_strike, parliaments = smokes | |
dog, snail, horse, fox, zebra = pets | |
if ( | |
# 1 There are five houses. | |
# 2 The Englishman lives in the red house. | |
englishman == red && | |
# 3 The Spaniard owns the dog. | |
spaniard == dog && | |
# 4 Coffee is drunk in the green house. | |
green = coffee && | |
# 5 The Ukrainian drinks tea. | |
ukrainian == tea && | |
# 6 The green house is immediately to the right of the ivory house. | |
green - ivory == 1 && | |
# 7 The Old Gold smoker owns snails. | |
old_gold == snail && | |
# 8 Kools are smoked in the yellow house. | |
kool == yellow && | |
# 9 Milk is drunk in the middle house. | |
milk == 3 && | |
# 10 The Norwegian lives in the first house. | |
norwegian = 1 && | |
# 11 The man who smokes Chesterfields lives in the house next to the man with the fox. | |
(chesterfields - fox).abs == 1 && | |
# 12 Kools are smoked in a house next to the house where the horse is kept. | |
(kool - horse).abs == 1 && | |
# 13 The Lucky Strike smoker drinks orange juice. | |
lucky_strike == orange_juice && | |
# 14 The Japanese smokes Parliaments. | |
japanese == parliaments && | |
# 15 The Norwegian lives next to the blue house. | |
(norwegian - blue).abs == 1 | |
) | |
print water, "\n", zebra, "\n" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment