save-the-gnomes.rb
### A programmatic proof of one of my favorite riddles
#
# Ten gnomes are captured by an evil wizard.
#
# The gnomes know that tomorrow they will be lined up in a single file line,
# all facing the same direction so that each gnome only sees the gnomes
# in front of him or her. Then each gnome will have a red or white hat
# placed on their head. Starting with the gnome in back each gnome will
# get one chance to guess the color of his/her own hat. If that gnome is right
# he/she lives. Otherwise he/she dies.
#
# The night before the execution the gnomes devise a plan to guarantee the
# survival of at least 9 of themselves.
#
# What is their plan?
A programmatic proof of one of my favorite riddles
Ten gnomes are captured by an evil wizard.

The gnomes know that tomorrow they will be lined up in a single file line,
all facing the same direction so that each gnome only sees the gnomes
in front of him or her. Then each gnome will have a red or white hat
placed on their head. Starting with the gnome in back each gnome will
get one chance to guess the color of his/her own hat. If that gnome is right
he/she lives. Otherwise he/she dies.

The night before the execution the gnomes devise a plan to guarantee the
survival of at least 9 of themselves.


A programmatic proof of one of my favorite riddles Ten gnomes are captured by an evil wizard . The gnomes know that tomorrow they will be lined up in a single file line all facing the same direction so that each gnome only sees the gnomes in front of him or her . Then each gnome will have a red or white hat placed on their head . Starting with the gnome in back each gnome will get one chance to guess the color of his / her own hat . If that gnome is right he / she lives . Otherwise he / she dies . The night before the execution the gnomes devise a plan to guarantee the survival of at least 9 of themselves .
def wizards_trial()
	hats = ["red", "white"]
	gnomes = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"]
	hatted_gnomes = {}
	gnomes.each do |gnome|
		hatted_gnomes[gnome] = hats.sample
	end
	guesses = gnome_guesses(hatted_gnomes)
	judge_the_gnomes(guesses, hatted_gnomes)
end
def wizards_trial ( ) hats = [ "red" , "white" ] gnomes = [ "one" , "two" , "three" , "four" , "five" , "six" , "seven" , "eight" , "nine" , "ten" ] hatted_gnomes = { } gnomes . each do | gnome | hatted_gnomes [ gnome ] = hats . sample end guesses = gnome_guesses ( hatted_gnomes ) judge_the_gnomes ( guesses , hatted_gnomes ) end
A programmatic proof of one of my favorite riddles
Ten gnomes are captured by an evil wizard.
A programmatic proof of one of my favorite riddles Ten gnomes are captured by an evil wizard .
#Initialize hats and gnomes #Line up gnomes and place hats on them #Have gnomes guess which color hats they have on #Judge the gnomes