One of the New Year cards from Ghent University (Belgium) teaches us that we could have reused any calendar of 2013, 2002 or 1991 in the year 2019.

kalenders hergebruiken
In 2019 you can reuse the calenders of 2013, 2002 and 1991.

But how long must we keep the calendar of 2018 before it can be used again? Well, we have to wait until 2029. If you think this is way too long, you can already reuse the 2018 calendar during the first two months of 2024 (a leap year).

Assignment

We can reuse the calendar of year $$y_1$$ in year $$y_2$$ if all corresponding days fall on the same date (day and month) and weekday if we run through both calendars day by day. Your task:

Example

>>> first_difference(2018, 2019)
datetime.date(2019, 1, 1)
>>> first_difference(2018, 2024)
datetime.date(2024, 2, 29)
>>> first_difference(2018, 2029)

>>> reuse_calendar(2018)
2029
>>> reuse_calendar(2018, True)
2007
>>> reuse_calendar(2019, previous=False)
2030
>>> reuse_calendar(2019, previous=True)
2013

>>> reuse_calendars(2018, 10)
[2029, 2035, 2046, 2057, 2063, 2074, 2085, 2091, 2103, 2114]
>>> reuse_calendars(2018, 10, True)
[2007, 2001, 1990, 1979, 1973, 1962, 1951, 1945, 1934, 1923]
>>> reuse_calendars(2019, 10, previous=False)
[2030, 2041, 2047, 2058, 2069, 2075, 2086, 2097, 2109, 2115]
>>> reuse_calendars(2019, 10, previous=True)
[2013, 2002, 1991, 1985, 1974, 1963, 1957, 1946, 1935, 1929]

Epilogue

Apparently we only need 14 calendars this century.

calendar can be reused in
2001 2007, 2018, 2029, 2035, 2046, 2057, 2063, 2074, 2085, 2091
2002 2013, 2019, 2030, 2041, 2047, 2058, 2069, 2075, 2086, 2097
2003 2014, 2025, 2031, 2042, 2053, 2059, 2070, 2081, 2087, 2098
2004 2032, 2060, 2088
2005 2011, 2022, 2033, 2039, 2050, 2061, 2067, 2078, 2089, 2095
2006 2017, 2023, 2034, 2045, 2051, 2062, 2073, 2079, 2090
2008 2036, 2064, 2092
2009 2015, 2026, 2037, 2043, 2054, 2065, 2071, 2082, 2093, 2099
2010 2021, 2027, 2038, 2049, 2055, 2066, 2077, 2083, 2094, 2100
2012 2040, 2068, 2096
2016 2044, 2072
2020 2048, 2076
2024 2052, 2080
2028 2056, 2084