save-the-gnomes.rb
def gnome_guesses(placements)

	guesses = {}

	white_hats_ahead = 0

	#count white hats ahead of gnome "one"
	placements.each do |gnome, hat|
		unless gnome == "one"
			white_hats_ahead += 1 if hat == "white"
		end
	end

	#set a starting total to compare to:
	#each gnome will subtract 1 from this number in their head
	#whenever a white hat is guessed
	white_hats_total = white_hats_ahead

	#the first gnome will say "white" if he/she sees an even number of white hats
	#or "red" for an odd number of white hats
	if white_hats_ahead % 2 == 0
		guesses["one"] = "white"
	else
		guesses["one"] = "red"
	end
	#this lets the gnomes compare what they see to the white_hats_total,
	#because they know if they should see an odd or even number of white
	#hats in front of them

	#the rest of the gnomes will guess according to what they see
	#and know their own hat to be based on the white_hats_total
	placements.each do |gnome, hat|
		
		unless gnome == "one"
			#remove the current gnome's hat from the white hat count if it is white
			white_hats_ahead -= 1 if hat == "white"

			#have gnome evaluate whether he/she is white or red
			#by comparing white_hats_ahead to what they know to be
			#the actual number of white hats
			if white_hats_ahead == white_hats_total
				guesses[gnome] = "red"
			else
				guesses[gnome] = "white"
				white_hats_total -= 1
			end
		end
	end

	guesses
end
#Have the gnomes assign guesses to themselves, following the constraints of the puzzle
#placements will be the hash "hatted_gnomes" from wizards_trial
Have the gnomes assign guesses to themselves, following the constraints of the puzzle
placements will be the hash "hatted_gnomes" from wizards_trial
Have the gnomes assign guesses to themselves following the constraints of the puzzle placements will be the hash " hatted_gnomes " from wizards_trial
def gnome_guesses(placements)
	guesses = {}
	white_hats_ahead = 0
	placements.each do |gnome, hat|
		unless gnome == "one"
			white_hats_ahead += 1 if hat == "white"
		end
	end
	white_hats_total = white_hats_ahead
	if white_hats_ahead % 2 == 0
		guesses["one"] = "white"
	else
		guesses["one"] = "red"
	end
	placements.each do |gnome, hat|
		unless gnome == "one"
			white_hats_ahead -= 1 if hat == "white"
			if white_hats_ahead == white_hats_total
				guesses[gnome] = "red"
			else
				guesses[gnome] = "white"
				white_hats_total -= 1
			end
		end
	end
	guesses
end
def gnome_guesses ( placements ) guesses = { } white_hats_ahead = 0 placements . each do | gnome , hat | unless gnome == "one" white_hats_ahead += 1 if hat == "white" end end white_hats_total = white_hats_ahead if white_hats_ahead % 2 == 0 guesses [ "one" ] = "white" else guesses [ "one" ] = "red" end placements . each do | gnome , hat | unless gnome == "one" white_hats_ahead -= 1 if hat == "white" if white_hats_ahead == white_hats_total guesses [ gnome ] = "red" else guesses [ gnome ] = "white" white_hats_total -= 1 end end end guesses end
Have the gnomes assign guesses to themselves, following the constraints of the puzzle
placements will be the hash "hatted_gnomes" from wizards_trial
Have the gnomes assign guesses to themselves following the constraints of the puzzle placements will be the hash " hatted_gnomes " from wizards_trial
#count white hats ahead of gnome "one" #set a starting total to compare to: #each gnome will subtract 1 from this number in their head #whenever a white hat is guessed #the first gnome will say "white" if he/she sees an even number of white hats #or "red" for an odd number of white hats #this lets the gnomes compare what they see to the white_hats_total, #because they know if they should see an odd or even number of white #hats in front of them #the rest of the gnomes will guess according to what they see #and know their own hat to be based on the white_hats_total #remove the current gnome's hat from the white hat count if it is white #have gnome evaluate whether he/she is white or red #by comparing white_hats_ahead to what they know to be #the actual number of white hats
placements