Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.