Last active
August 29, 2015 14:19
-
-
Save Freaky/2e48c3cae15477a1d60d to your computer and use it in GitHub Desktop.
This file contains 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
#!/usr/bin/env ruby | |
D = Struct.new(:month, :day) | |
dates = <<EOC | |
May 15 May 16 May 19 | |
June 17 June 18 | |
July 14 July 16 | |
August 14 August 15 August 17 | |
EOC | |
candidates = dates.scan(/(\w+) (\d+)/).map {|mon, day| D.new(mon, day)} | |
def candidates.group(how) | |
group_by(&how).select {|_, dates| dates.size == 1 }.map {|_, dates| dates.first } | |
end | |
# Albert: I don't know. Neither does Bernard. | |
months = candidates.group(:day).map(&:month) | |
candidates.reject! {|x| months.include? x.month } | |
# Bernard: I didn't know, but now I do. | |
days = candidates.group(:day).map(&:day) | |
candidates.select! {|x| days.include? x.day } | |
# Albert: Woo. | |
months = candidates.group(:month).map(&:month) | |
puts candidates.select! {|x| months.include? x.month }.map(&:to_a).flatten.join(' ') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment