|
Logic Problem
Whose Fish?
By Albert Einstein (maybe)
This brainteaser, reportedly written by Einstein is difficult and Einstein said that 98%
of the people in the world could not figure it out. Which percentage are you in?
There are five houses in a row in different colors. In each house lives a person with a different
nationality. The five owners drink a different drink, smoke a different brand of cigar and keep
a different pet, one of which is a Walleye Pike.
The question is-- who owns the fish?
Hints:
1. The Brit lives in the red house.
2. The Swede keeps dogs as pets.
3. The Dane drinks tea.
4. The green house is on the left of the white house.
5. The green house owner drinks coffee.
6. The person who smokes Pall Malls keeps birds.
7. The owner of the yellow house smokes Dunhills.
8. The man living in the house right in the center drinks milk.
9. The man who smokes Blends lives next to the one who keeps cats.
10. The Norwegian lives in the first house.
11. The man who keeps horses lives next to the one who smokes Dunhills.
12. The owner who smokes Bluemasters drinks beer.
13. The German smokes Princes.
14. The Norwegian lives next to the blue house.
15. The man who smokes Blends has a neighbor who drinks water.
logic.rb 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 class Possibility
21 attr_reader :count, :table, :type, :params, :c_size
22
23 def initialize(params=Array.new)
24 @count = 0
25 @table = Array.new
26 @type = ""
27 @params = params
28 end
29
30 def getCombos(size=@params.length)
31 @c_size = size
32 @type = "combination"
33 @table.clear
34 combination(@params,Array.new)
35 @table.uniq!
36 @count = @table.length
37 return @count
38 end
39
40 private
41
42 43 def combination(params,str)
44 if(str.length == @c_size)
45 @table.push(str)
46 else
47 if(params.length == 1)
48 @table.push(str.push(params[0]))
49 else
50 params.each_with_index do |item,i|
51 newstr = str.dup.push(item)
52 newparams = params.dup
53 newparams.slice!(i)
54 combination(newparams,newstr)
55 end
56 end
57 end
58 return 0
59 end
60
61 end
62
63 64 65
66 t = Time.now
67 tests = 0 68
69 70 puts "--WHO--\ntest 10"
71 who = []
72 73 w = Possibility.new( ["BR","SW","DA","NO","GE"] )
74 w.getCombos(5)
75 w.table.each do |combo|
76 77 tests+=1
78 if combo[0] == "NO"
79 who << combo
80 end
81 end
82 puts "size: #{w.count} >> #{who.size}"
83
84 85 puts "--COLOR--\ntest 4"
86 color = []
87 88 c = Possibility.new( ["RD","WT","GR","YL","BL"] )
89 c.getCombos(5)
90 c.table.each do |combo|
91 92 tests+=1
93 if combo[0] != "WT" and combo[combo.index("WT")-1] == "GR"
94 color << combo
95 end
96 end
97 puts "size: #{c.count} >> #{color.size}"
98
99 100 puts "--DRINKS--\ntest 8"
101 drinks = []
102 103 d = Possibility.new( ["TE","CF","MK","BE","WA"] )
104 d.getCombos(5)
105 d.table.each do |combo|
106 107 tests+=1
108 if combo[2] == "MK"
109 drinks << combo
110 end
111 end
112 puts "size: #{d.count} >> #{drinks.size}"
113
114 115 116 a = Possibility.new( ["DG","BD","CT","HR","FH"] )
117 a.getCombos(5)
118
119 120 121 s = Possibility.new( ["PM","DH","BL","BM","PR"] )
122 s.getCombos(5)
123
124 125 puts "--ANIMAL-SMOKES--\ntests 6,9,11"
126 an_sm = []
127 a.table.each do |comboa|
128 s.table.each do |combos|
129 tests+=1
130 if comboa[combos.index("PM")] == "BD" 131 x = combos.index("BL")
132 y = comboa.index("CT")
133 tests+=1
134 if (x-y).abs == 1 135 x = combos.index("DH")
136 y = comboa.index("HR")
137 tests+=1
138 if (x-y).abs == 1 139 an_sm << [comboa,combos]
140 end
141 end
142 end
143 end
144 end
145 puts "size: #{a.count}*#{s.count} >> #{an_sm.size}"
146
147 148 puts "--ANIMAL-SMOKES-DRINKS--\ntests 12,15"
149 an_sm_dr = []
150 an_sm.each do |combo|
151 drinks.each do |drink|
152 tests+=1
153 if drink[combo[1].index("BM")] == "BE" 154 x = combo[1].index("BL")
155 y = drink.index("WA")
156 tests+=1
157 if (x-y).abs == 1 158 an_sm_dr << (combo.dup << drink)
159 end
160 end
161 end
162 end
163 puts "size: #{an_sm.size}*#{drinks.size} >> #{an_sm_dr.size}"
164
165 166 puts "--ANIMAL-SMOKES-DRINKS-COLOR--\ntests 5,7"
167 an_sm_dr_cl = []
168 an_sm_dr.each do |combo|
169 color.each do |hue|
170 tests+=1
171 if combo[2][hue.index("GR")] == "CF" 172 tests+=1
173 if combo[1][hue.index("YL")] == "DH" 174 an_sm_dr_cl << (combo.dup << hue)
175 end
176 end
177 end
178 end
179 puts "size: #{an_sm_dr.size}*#{color.size} >> #{an_sm_dr_cl.size}"
180
181 182 puts "--ANIMAL-SMOKES-DRINKS-COLOR-WHO--\ntests 1,2,3,13,14"
183 answer = []
184 found = false
185 an_sm_dr_cl.each do |combo|
186 who.each do |person|
187 tests+=1
188 if combo[3][person.index("BR")] == "RD" 189 tests+=1
190 if combo[0][person.index("SW")] == "DG" 191 tests+=1
192 if combo[2][person.index("DA")] == "TE" 193 tests+=1
194 if combo[1][person.index("GE")] == "PR" 195 x = person.index("NO")
196 y = combo[3].index("BL")
197 tests+=1
198 if (x-y).abs == 1 199 answer << (combo.dup << person)
200 201 end
202 end
203 end
204 end
205 end
206 207 end
208 209 end
210 puts "size: #{an_sm_dr_cl.size}*#{who.size} >> #{answer.size}"
211
212 213 print "\nLogic Tests Used: #{tests}\nSolution(s): #{answer.size}\n\n"
214 puts "num\tclr\twho\tdrnk\tsmk\tpet"
215 puts "---\t---\t---\t----\t---\t---"
216 answer.each do |ans|
217 0.upto(4) do |i|
218 print "#{i+1}:\t#{ans[3][i]}"
219 print "\t#{ans[4][i]}"
220 print "\t#{ans[2][i]}"
221 print "\t#{ans[1][i]}"
222 print "\t#{ans[0][i]}\n"
223 end
224 puts
225 end
226
227 print "Elapsed Time: #{Time.now-t} secs\n\n"
Output
--WHO--
test 10
size: 120 >> 24
--COLOR--
test 4
size: 120 >> 24
--DRINKS--
test 8
size: 120 >> 24
--ANIMAL-SMOKES--
tests 6,9,11
size: 120*120 >> 288
--ANIMAL-SMOKES-DRINKS--
tests 12,15
size: 288*24 >> 456
--ANIMAL-SMOKES-DRINKS-COLOR--
tests 5,7
size: 456*24 >> 384
--ANIMAL-SMOKES-DRINKS-COLOR-WHO--
tests 1,2,3,13,14
size: 384*24 >> 1
Logic Tests Used: 51156
Solution(s): 1
num clr who drnk smk pet
--- --- --- ---- --- ---
1: YL NO WA DH CT
2: BL DA TE BL HR
3: RD BR MK PM BD
4: GR GE CF PR FH
5: WT SW BE BM DG
Elapsed Time: 0.25 secs
What do you know... it's the German in house #4!
|