require "date" date = Date.new(2015, 1, 1) count = 0 1.upto(12) do |month| date = date.next_month 1.upto(date.prev_day.day) do |day| if month == day.to_s.chars.map(&:to_i).reduce(&:+) count += 1 end end end p count