Skip to content

Instantly share code, notes, and snippets.

@Freaky
Last active August 29, 2015 14:19
Show Gist options
  • Save Freaky/2e48c3cae15477a1d60d to your computer and use it in GitHub Desktop.
Save Freaky/2e48c3cae15477a1d60d to your computer and use it in GitHub Desktop.
#!/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