From 9a33095ecce6964a0451445dd1a64cbaa0e57492 Mon Sep 17 00:00:00 2001 From: Linus Miller Date: Thu, 29 Mar 2018 11:41:15 +0200 Subject: [PATCH] Problem 19 --- 0019.py | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 0019.py diff --git a/0019.py b/0019.py new file mode 100644 index 0000000..cafb13e --- /dev/null +++ b/0019.py @@ -0,0 +1,72 @@ +target = 171 + +def is_leap_year (year): + return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0) + +def days_since_1900 (year, month = 1, day = 1): + days = 0 + + for y in range(1900, year): + if (is_leap_year(y)): + days += 366 + else: + days += 365 + + for m in range(1, month): + days += get_days_in_month(m, year) + + days += day - 1 + + return days + +days_in_month = [ + None, + 31, + 28, + 31, + 30, + 31, + 30, + 31, + 31, + 30, + 31, + 30, + 31, +] + +week_days = [ + 'mon', + 'tue', + 'wed', + 'thu', + 'fri', + 'sat', + 'sun', +] + +def get_days_in_month (month, year): + days = days_in_month[month] + + if (month == 2 and is_leap_year(year)): + return days + 1 + + return days + +def get_day_of_week (year, month, day): + days = days_since_1900(year, month, day) + + rest = days % 7 + + return week_days[rest] + + +count = 0 + +for year in range(1901, 2001): + for month in range(1, 13): + if (get_day_of_week(year, month, 1) == 'sun'): + count += 1 + +print(count) +print(count == target)