TopCoder problem "BlueMoons" used in SRM 189 (Division I Level Three)

Problem Statement

    For the purposes of this problem, there are exactly 29.53 days between two full moons. A blue moon occurs whenever there is a full moon twice in one calendar month (January, February, etc). For example, if a full moon occurs at some point on January 1st, then another one will occur at some point on January 30th or 31st (depending on what time it occurred on the 1st). The second one is the blue moon.

Given an interval from one month to another, you are to determine how many blue moons there are between those two months, inclusive. To solve this you must know the following:
  1. Every year has 12 months.
  2. In a normal year, the number of days in each month are, in order starting with January, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
  3. In leap years, the second month (February) has 29 days instead of 28.
  4. Leap years occur every year that is divisible by 4 except for years that are divisible by 100 but not divisible by 400. Thus 1996 was a leap year, as was 2000 (2000 is divisible by 400), but not 1700, 1800, or 1900 (1700, 1800 and 1900 are divisible by 100 but not divisible by 400).

Your task is to write a method, count, which takes a String, interval, and another String, fullMoon, as input and determines how many blue moons there are in that interval. interval will be formatted as "MM/YYYY to MM/YYYY" where "MM" represents the month (January is "01" and December is "12") and "YYYY" represents the year. fullMoon will represent a specific time at which there was a full moon and will be formatted as "DD.DD/MM/YYYY" where "MM" and "YYYY" are formatted the same as they are in interval. "DD.DD" represents the day of the month, as a fraction. Thus "01.00" would mean 12 AM on the first day of the month. "01.50" would be exactly half way through the first day of the month (12 NOON). Thus, if fullMoon were "05.75/05/2002", this would mean that there was a full moon precisely 3 quarters of the way through the 5th day of the 5th month of 2002 (6 PM May 5th 2002).


Parameters:String, String
Method signature:int count(String interval, String fullMoon)
(be sure your method is public)


-If a full moon occurs at 12AM (Midnight) it is part of the day which has just started, not the one that has just finished.


-interval will be formatted exactly as "MM/YYYY to MM/YYYY", with no leading, trailing, or extra spaces.
-fullMoon will be formatted exactly as "DD.DD/MM/YYYY", with no leading or trailing spaces.
-Each MM will contain leading 0's, if necessary, to have exactly 2 digits.
-DD.DD will contain leading and trailing 0's if necessary so that it is always formatted as "DD.DD"
-All dates and times will be valid times since 1900, inclusive. Thus "MM" will be between "01" and "12", inclusive, "YYYY" will be between "1900" and "9999" inclusive, and "DD.DD" will be between "01.00" and "31.99" (depending on the month, the upper bound for the day could be lower), inclusive.


"01/2002 to 05/2002"
Returns: 0
Given that there was a full moon at the specified time, there are no blue moons during this period.
"01/2002 to 05/2002"
Returns: 2
"01/2002 to 01/2002"
Returns: 1
"01/2002 to 01/2002"
Returns: 0
Note that fullMoon need not be within interval.

Problem url:

Problem stats url:




lbackstrom , brett1479

Problem categories:

Math, Simulation