Skip to content

Instantly share code, notes, and snippets.

@calebmadrigal
Last active March 10, 2018 16:53
Show Gist options
  • Save calebmadrigal/0239bf5bdc044dc7e00dc4a9a64a6c01 to your computer and use it in GitHub Desktop.
Save calebmadrigal/0239bf5bdc044dc7e00dc4a9a64a6c01 to your computer and use it in GitHub Desktop.
XKCD 2018
import sys
from datetime import date,timedelta
def is_xkcd_day(d):
return d.weekday() in {0, 2, 4}
def find_date_by_xkcd_number(xkcd_num, current_xkcd=1964, start_date=date(2018, 3, 7)):
if not is_xkcd_day(start_date):
raise Exception("start_date must be an xkcd day (M, W, F")
xkcds_until_target = xkcd_num - current_xkcd
d = start_date
xkcd_counter = 0
date_with_xkcd_num = None
while True:
if xkcd_counter == xkcds_until_target:
if not is_xkcd_day(d):
d = d + timedelta(days=1)
continue
date_with_xkcd_num = d
break
d = d + timedelta(days=1)
if is_xkcd_day(d):
xkcd_counter += 1
return date_with_xkcd_num
if __name__ == '__main__':
xkcd_to_find = int(sys.argv[1]) if len(sys.argv) > 1 else 2018
xkcd_date = find_date_by_xkcd_number(xkcd_to_find)
print('Date of XKCD #{}: {}'.format(xkcd_to_find, xkcd_date))

The date of XKCD number 2018 will be July 11, 2018

[caleb@cmbpr:Tests]$ python3 xkcd_2018.py
Date of XKCD #2018: 2018-07-11

Other tests

[caleb@cmbpr:Tests]$ python3 xkcd_2018.py 1964
Date of XKCD #1964: 2018-03-07
[caleb@cmbpr:Tests]$ python3 xkcd_2018.py 1965
Date of XKCD #1965: 2018-03-09
[caleb@cmbpr:Tests]$ python3 xkcd_2018.py 1966
Date of XKCD #1966: 2018-03-12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment