Skip to content

Instantly share code, notes, and snippets.

@robmiller
Last active January 3, 2019 12:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robmiller/b416df48bcd972d6a3bd7edf0ffaa4bc to your computer and use it in GitHub Desktop.
Save robmiller/b416df48bcd972d6a3bd7edf0ffaa4bc to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
#
# Solutions to Diamond Geezer's "the digits of the year 2019 add up to twelve" puzzles:
# http://diamondgeezer.blogspot.com/2019/01/the-digits-of-year-2019-add-up-to-twelve.html
def main
# a) How many years ago did this last happen?
a = 2019 - 2018.downto(0).find { |year| sum_digits(year) == 12 }
# b) In what year?
b = 2018.downto(0).find { |year| sum_digits(year) == 12 }
# c) How many years until it next happens?
c = 2020.upto(9999).find { |year| sum_digits(year) == 12 } - 2019
# d) In what year?
d = 2020.upto(9999).find { |year| sum_digits(year) == 12 }
# e) How many times will it happen this century?
e = 2001.upto(2100).map { |year| [year, sum_digits(year)] }.count { |year, sum| sum == 12 }
# f) What's the maximum number of times it can happen in a century?
f = 1.upto(9999).each_slice(100).map { |c| [c.first, c.map { |year| [year, sum_digits(year)] }.count { |year, sum| sum == 12 }] }.max { |a, b| a[1] <=> b[1] }.last
# g) In which century will this maximum next happen?
g = 2001.upto(9999).each_slice(100).map { |c| [c.first, c.map { |year| [year, sum_digits(year)] }.count { |year, sum| sum == 12 }] }.find { |year, sum| sum == f }.first
g = century(g)
# h) In which century did this maximum last happen?
h = 1.upto(2000).each_slice(100).map { |c| [c.first, c.map { |year| [year, sum_digits(year)] }.count { |year, sum| sum == 12 }] }.find_all { |year, sum| sum == f }.last.first
h = century(h)
# i) When will there next be a 99-year gap?
i = 2019.upto(99999).map { |year| [year, sum_digits(year)] }.select { |year, sum| sum == 12 }.each_cons(2).find { |a, b| b[0] - a[0] == 99 }.map { |a| a.first }
i = "Between the years #{i[0]} and #{i[1]}"
# j) When will there next be a longer gap?
j = 2019.upto(99999).map { |year| [year, sum_digits(year)] }.select { |year, sum| sum == 12 }.each_cons(2).find { |a, b| b[0] - a[0] > 99 }.map { |a| a.first }
j = "Between the years #{j[0]} and #{j[1]}"
# k) When is the next century with no years when this happens?
k = 2001.upto(9999).each_slice(100).map { |c| [c.first, c.map { |year| [year, sum_digits(year)] }.count { |year, sum| sum == 12 }] }.find { |year, sum| sum == 0 }.first
k = century(k)
# l) How many times will it happen this millennium?
l = 2001.upto(3000).map { |year| [year, sum_digits(year)] }.count { |year, sum| sum == 12 }
("a".."l").each do |v|
puts "#{v}) #{binding.local_variable_get(v.to_sym)}"
end
end
def sum_digits(year)
year.to_s.chars.map(&:to_i).reduce(:+)
end
def ordinal(num)
case num.to_s
when "11", "12", "13"
"#{num}th"
when /1$/
"#{num}st"
when /2$/
"#{num}nd"
when /3$/
"#{num}rd"
else
"#{num}th"
end
end
def century(year)
ordinal((year / 100).floor + 1)
end
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment