Skip to content

Instantly share code, notes, and snippets.

@jsbain
Created August 24, 2014 07:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jsbain/4a3ed356cfb62fe3e9b2 to your computer and use it in GitHub Desktop.
Save jsbain/4a3ed356cfb62fe3e9b2 to your computer and use it in GitHub Desktop.
solve-ccc-riddle.py
import datetime, logging
def elapsed_datetime(start_datetime, end_datetime = datetime.datetime.now()):
return end_datetime - start_datetime # returns a datetime.timedelta
def find_day(days_on_earth, ref_datetime=datetime.datetime(2014,8,23), ref_is_start=True):
'''find day (datetime obj) corresponding to days_on_earth, tuple of days, hours, min, sec.
ref_is_start == True means ref date is start, return end date. itherwise, return start date given this end date
'''
dt=datetime.timedelta(**dict(zip(['days','hours','minutes','seconds'],days_on_earth)))
return (ref_datetime+dt) if ref_is_start else (ref_datetime-dt)
def find_nextprime_bday(dob,end):
assert(end>=dob)
from sympy.ntheory import isprime
from sympy.ntheory.generate import nextprime
# had birthday if current month/day is larger than bday month/ day
had_bday_yet=end.strftime('%m%d')>=dob.strftime('%m%d')
age = end.year - dob.year - (1 if had_bday_yet else 0)
return age if isprime(age) else nextprime(age)
def solve_riddle():
#specific day format with mixed 3 and 4 letters, rather than using %a in strftime
weekdaynames=['Mon','Tues','Wed','Thurs','Fri','Sat','Sun']
start_datetime= find_day([20000, 0, 3, 55.76],ref_is_start=False ) #dob
answer1=start_datetime.strftime('%Y/%m/%d')
#next, check day 0, 10000, 20000, 30000, and grab weekday abbrev
check_days=[0, 10000, 20000,30000 ]
answer2= '/'.join([weekdaynames[find_day([x, 0, 4],start_datetime).weekday()] for x in check_days])
#finally, check next prime bday for doe 30000
answer3= find_nextprime_bday(start_datetime, find_day([30000,0,4],start_datetime))
return (answer1,answer2,answer3)
print solve_riddle()
@jsbain
Copy link
Author

jsbain commented Aug 24, 2014

('1959/11/19', 'Fri/Tues/Sat/Wed', 83)

@cclauss
Copy link

cclauss commented Aug 25, 2014

('1959/11/20', 'Fri/Tue/Sat/Wed', 83)

I got a huge smile on my face when I saw your answer because you got everything correct except that you missed birthday by just one day. I think this was related to your discussion on the forum about zero-based-counting and off-by-one errors. However, the reason that it made me smile was because you guessed Nov. 19th which is my father's birthday instead of Nov. 20th which is my birthday.

When I was to be born, my mother wanted to give me my father's name (Charles) but my father did not like the idea of Charles Jr. or even worse, Charles II. As a compromise, they agreed that if I was born exactly on his birthday then I would be named Charles Jr. However, I was born one day after his birthday (off-by-one) so Charles is my middle name instead of my first name (thus CCC).

Thanks for making me smile on 20,001st day. What a difference a day makes. ;-)

import datetime  # http://omz-forums.appspot.com/pythonista/post/5578251246239744

def is_prime(n):
    for i in range(2,n-1):
        if not n % i:
            return False
    return True

def calc_weekdays(birthdate):
    return '/'.join([(birthdate + datetime.timedelta(days=days)).strftime('%a')
                     for days in xrange(0, 30001, 10000)])

def calc_next_age_at_30000_days():
    age = 30000 / 365  # or int(30000 / 365.242) 
    while not is_prime(age):
        age += 1
    return age

birthday = end_date = datetime.date(2014, 8, 23) - datetime.timedelta(days=20000)
print((birthday.strftime('%Y/%m/%d'), calc_weekdays(birthday), calc_next_age_at_30000_days()))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment