Created
February 8, 2009 03:30
-
-
Save sintaxi/60164 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
# | |
# ##################################### | |
# Who is the killer? | |
# ##################################### | |
# | |
# Handel has been killed and Beethoven is on the case. He has | |
# interviewed the four suspects and their statements are shown below. | |
# Each suspect has said two sentences. One sentence of each suspect is a | |
# lie and one sentence is the truth. Help Beethoven figure out who the | |
# killer is. | |
# | |
# Joplin: I did not kill Handel. Either Grieg is the killer or none of us is. | |
# Grieg: I did not kill Handel. Gershwin is the killer. | |
# Strauss: I did not kill Handel. Grieg is lying when he says Gershwin is the killer. | |
# Gershwin: I did not kill Handel. If Joplin did not kill him, then Grieg did. | |
# | |
# | |
# Fact: each person have made a true and false statement. | |
# Fact: each persons first statement is that they did not kill Handel. | |
# | |
# therefore... | |
# Fact: killers second statement must be true | |
# Fact: non-killers second statement must be false | |
# | |
# | |
# | |
def reveal_killers | |
puts Joplin.killer? ? "Joplin: killer!" : "Joplin: is innocent" | |
puts Grieg.killer? ? "Grieg: killer!" : "Grieg: is innocent" | |
puts Strauss.killer? ? "Strauss: killer!" : "Strauss: is innocent" | |
puts Gershwin.killer? ? "Gershwin killer!" : "Gershwin: is innocent" | |
end | |
def make_assumption | |
assumtion = [Joplin, Grieg, Strauss, Gershwin][rand(4)] | |
puts "assuming #{ assumtion.to_s } is the killer..." | |
assumtion.instance_eval{ | |
def killer? | |
true | |
end | |
} | |
assumtion.results_of_assumtion | |
end | |
class Suspect | |
def self.killer? | |
@killer ||= self.statement == true | |
end | |
def self.results_of_assumtion | |
begin | |
reveal_killers | |
rescue | |
puts "wait a second... this logic is broken. he cannot be the killer. try someone else...\n\n" | |
self.instance_eval{ | |
def killer? | |
false | |
end | |
} | |
make_assumption | |
end | |
end | |
end | |
#### Suspects | |
class Joplin < Suspect | |
def self.statement | |
Grieg.killer? || ( !Grieg.killer? && !Strauss.killer? && !Gershwin.killer? && !Joplin.killer? ) | |
end | |
end | |
class Grieg < Suspect | |
def self.statement | |
Gershwin.killer? | |
end | |
end | |
class Strauss < Suspect | |
def self.statement | |
Grieg.statement == false | |
end | |
end | |
class Gershwin < Suspect | |
def self.statement | |
Joplin.killer? || Grieg.killer? | |
end | |
end | |
make_assumption | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment