Riddler Express
The year is 2000, and an arithmetical anarchist group has an idea. For the next 100 years, it will vandalize a famous landmark whenever the year (in two-digit form, for example this year is “18”) is the product of the month and date (i.e. month × date = year, in the MM/DD/YY format).
A few questions about the lawless ensuing century: How many attacks will happen between the beginning of 2001 and the end of 2099? What year will see the most vandalism? The least? What will be the longest gap between attacks?
Solution
from datetime import date, timedelta
days_in_month = {
1:31,
2:28,
3:31,
4:30,
5:31,
6:30,
7:31,
8:31,
9:30,
10:31,
11:30,
12:31
}
attacks_by_year = {}
date_last_attack = date(2001,1,1)
max_gap = 0
for year in range(1, 100):
attacks_by_year[year] = 0
for month in days_in_month.keys():
days = days_in_month[month]
if year % 4 == 0 and month == 2:
days += 1
for day in range(1, days+1):
if month*day == year:
attacks_by_year[year] += 1
date_latest_attack = date(2000+year, month, day)
gap = date_latest_attack - date_last_attack
if gap.days >= max_gap:
max_gap = gap.days
date_last_attack = date_latest_attack
print("Max gap in days between attacks:", max_gap-1)
print("Attacks by year: ", attacks_by_year)
print("Total number of attacks: ", sum(attacks_by_year.values()))
print("Year of max attacks: ", max(attacks_by_year.items(),
key = lambda x: x[1]))
The year with the maximum number of attacks is with attacks in total. There are a number of years with no attacks at all. The total number of attacks in all years is . The maximum gap in days between two attacks is .