Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def print_dates(dates, answer_text)
- puts "\nAnswers left:" if answer_text
- puts dates.join("; ")
- print "\n"
- end
- class Day
- attr_accessor :month, :day
- def initialize(month, day)
- @day = day;
- @month = month;
- end
- def to_s
- "#{@month.to_s.upcase}, #{@day}"
- end
- end
- possible_dates = [
- Day.new(:may, 15), Day.new(:may, 16), Day.new(:may, 19),
- Day.new(:june, 17), Day.new(:june, 18),
- Day.new(:july, 14), Day.new(:july, 16),
- Day.new(:august, 14), Day.new(:august, 15), Day.new(:august, 17)
- ]
- puts "Possible dates are"
- print_dates(possible_dates, false)
- puts "Albert: “I don’t know when your birthday is, but I know Bernard doesn’t know, either.”"
- # Albert doesn't know, so we take out the months that appear only once in the possible dates.
- answer = possible_dates - possible_dates.select{|a| possible_dates.map(&:month).count(a.month) == 1}
- # Albert knows Bernard doesn't know, so we take out the all answers that have
- # the same month of the answers that their day appears only once.
- answer -= answer.select{|a| answer.select{|b| answer.count{|c| c.day == b.day} == 1}.map(&:month).include?(a.month) }
- print_dates(answer, true)
- puts "Bernard: “I didn’t know originally, but now I do.”"
- # Bernard knows the answer, because the day is unique in the answers left. Remove repeated days.
- answer -= answer.select{|a| answer.map(&:day).count(a.day) > 1 }
- print_dates(answer, true)
- puts "Albert: “Well, now I know, too!”"
- # Albert knows cause he knows the month, so it must be a month that is not repeated in the answers.
- answer -= answer.select{|a| answer.map(&:month).count(a.month) > 1 }
- print_dates(answer, true)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement