Advertisement
Guest User

Untitled

a guest
May 28th, 2015
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. def print_dates(dates, answer_text)
  2. puts "\nAnswers left:" if answer_text
  3. puts dates.join("; ")
  4. print "\n"
  5. end
  6.  
  7. class Day
  8. attr_accessor :month, :day
  9. def initialize(month, day)
  10. @day = day;
  11. @month = month;
  12. end
  13.  
  14. def to_s
  15. "#{@month.to_s.upcase}, #{@day}"
  16. end
  17. end
  18.  
  19. possible_dates = [
  20. Day.new(:may, 15), Day.new(:may, 16), Day.new(:may, 19),
  21. Day.new(:june, 17), Day.new(:june, 18),
  22. Day.new(:july, 14), Day.new(:july, 16),
  23. Day.new(:august, 14), Day.new(:august, 15), Day.new(:august, 17)
  24. ]
  25.  
  26. puts "Possible dates are"
  27. print_dates(possible_dates, false)
  28.  
  29. puts "Albert: “I don’t know when your birthday is, but I know Bernard doesn’t know, either.”"
  30. # Albert doesn't know, so we take out the months that appear only once in the possible dates.
  31. answer = possible_dates - possible_dates.select{|a| possible_dates.map(&:month).count(a.month) == 1}
  32.  
  33. # Albert knows Bernard doesn't know, so we take out the all answers that have
  34. # the same month of the answers that their day appears only once.
  35. answer -= answer.select{|a| answer.select{|b| answer.count{|c| c.day == b.day} == 1}.map(&:month).include?(a.month) }
  36.  
  37. print_dates(answer, true)
  38.  
  39. puts "Bernard: “I didn’t know originally, but now I do.”"
  40. # Bernard knows the answer, because the day is unique in the answers left. Remove repeated days.
  41. answer -= answer.select{|a| answer.map(&:day).count(a.day) > 1 }
  42.  
  43. print_dates(answer, true)
  44.  
  45. puts "Albert: “Well, now I know, too!”"
  46. # Albert knows cause he knows the month, so it must be a month that is not repeated in the answers.
  47. answer -= answer.select{|a| answer.map(&:month).count(a.month) > 1 }
  48.  
  49. print_dates(answer, true)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement