Created
January 21, 2012 12:51
-
-
Save melborne/1652697 to your computer and use it in GitHub Desktop.
Quiz from Professor Stewart's Cabinet of Mathematical Curiosities
This file contains 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
Alien = Struct.new(:name, :species) | |
species = { Veracitor: :==, Gibberish: :!= } | |
aliens = %w(Alfy Betty Gemma).product(species.keys).map { |name, sp| Alien[ name, sp ] } | |
a, b, g = aliens.group_by { |alien| alien.name }.map(&:last) | |
combinations = a.product(b,g) # provide all posible combinations of aliens. | |
desc = ->who_said,left,right{ left.send species[who_said.species], right } | |
combinations.find do |alfy, betty, gemma| | |
x = desc[alfy, betty.species, :Gibberish] # Alfy said, Betty is Gibberish. | |
y = desc[betty, alfy.species, gemma.species] # Betty said, Alfy is same as Gemma. | |
z = desc[gemma, betty.species, :Veracitor] # Gemma said, Betty is Veracitor. | |
x && y && z | |
end.map { |alien| p "#{alien.name} is #{alien.species}" } | |
# >> "Alfy is Veracitor" | |
# >> "Betty is Gibberish" | |
# >> "Gemma is Gibberish" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment