Skip to content

Instantly share code, notes, and snippets.

@melborne
Created January 21, 2012 12:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save melborne/1652697 to your computer and use it in GitHub Desktop.
Save melborne/1652697 to your computer and use it in GitHub Desktop.
Quiz from Professor Stewart's Cabinet of Mathematical Curiosities
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