diff --git a/19.py b/19.py index 3d31dee..643cad5 100755 --- a/19.py +++ b/19.py @@ -38,50 +38,6 @@ class Date: def isLeap(year): return (year % 4 == 0) and ((year % 100 != 0) or (year % 400 == 0)) -def calcNumDays(start,end): - days = 0 - currentMonth = start.month - currentDay = start.day - for year in range(start.year+1,end.year): - if(isLeap(year)): - days += 366 - else: - days += 365 - - leap = 0 - # Beginning - year = start.year - if(isLeap(year)): - leap = 1 - - for month in range(start.month+1, DEC+1): - if(month in MONTHS_31): - days += 31 - elif(month == FEB): - days += 28 - else: - days += 30 - - days += (31 - start.day + 1 + leap) - - leap = 0 - # End - year = end.year - if(isLeap(year)): - leap = 1 - - for month in range(JAN,end.month): - if(month in MONTHS_31): - days += 31 - elif(month == FEB): - days += 28 - else: - days += 30 - - days += (end.day + leap) - - return days - def getMonthLimit(month,year): limit = 0 if(month in MONTHS_31): @@ -96,7 +52,7 @@ def getMonthLimit(month,year): return limit -def SundaysFirstOfMonth(start,initial,numDays): +def SundaysFirstOfMonth(start,end,initial): sundays = 0 currentDay = start.day currentMonth = start.month @@ -105,7 +61,9 @@ def SundaysFirstOfMonth(start,initial,numDays): limit = getMonthLimit(currentMonth, currentYear) - for i in range(0,numDays): + while True: + if(end.day == currentDay and end.month == currentMonth and end.year == currentYear): + return sundays if (currentDay > limit): currentMonth += 1 currentDay = 1 @@ -120,15 +78,12 @@ def SundaysFirstOfMonth(start,initial,numDays): currentDay += 1 currentWeekday = (currentWeekday + 1) % 7 - return sundays - strDateEnd = sys.argv[1].split('/') dateStart = Date(1,1,1901) dateEnd = Date(int(strDateEnd[0]),int(strDateEnd[1]),int(strDateEnd[2])) initialDay = TUE -numDays = calcNumDays(dateStart,dateEnd) -numSundays = SundaysFirstOfMonth(dateStart,initialDay,numDays) +numSundays = SundaysFirstOfMonth(dateStart,dateEnd,initialDay) print "Result is: " + str(numSundays)