019 Counting Sundays.sf 634 B

1234567891011121314151617181920212223242526272829303132333435
  1. #!/usr/bin/ruby
  2. # Author: Daniel "Trizen" Șuteu
  3. # License: GPLv3
  4. # Website: https://github.com/trizen
  5. # How many Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)?
  6. # https://projecteuler.net/problem=19
  7. # Runtime: 0.446s
  8. require('DateTime')
  9. func first_sundays(year) {
  10. var count = 0
  11. for i in (1..12) {
  12. var date = %O<DateTime>.new(
  13. year => year,
  14. month => i,
  15. day => 1,
  16. )
  17. ++count if (date.dow == 7)
  18. }
  19. return count
  20. }
  21. var total = 0
  22. for year in (1901 .. 2000) {
  23. total += first_sundays(year)
  24. }
  25. say total