We need to write a program that reads multiple lines, each with six integers. For each dataset, we need to compute the number of days from date1 (inclusive) to date2 (exclusive) i.e., the number of days between date1 and date2 where date1 is counted, date2 is not counted. So it's basically (date2 - date1) in days, with date1 included, date2 excluded. So if date1 == date2, answer is 0? Possibly, but in the problem statement date1 <= date2, and they might have case where same? Example given: 2006 9 2 to 2006 9 3 gives 1. So that's difference of one day. If same date, answer 0.
We need to compute days difference. Since dates can span many years, we need to compute efficiently. Approach: compute ordinal day number for each date (days since some epoch) then subtract. For date1 inclusive, date2 exclusive => days = ordinal(date2) - ordinal(date1). Where ordinal(date) gives number of days from a fixed point (like year 0 or something) to the given date, with day 1 being the first day? Actually we need to define: number of days before the given date. Usually compute days from an epoch like 0001-01-01. But careful: we need to handle leap years correctly. The range: years might be up to perhaps large? Example includes 2101. Input constraints: datasets <= 50, but year could be any? Not specified but likely not huge. Could compute ordinal by summing days in years before given year, days in months before given month, and day of month. Then subtract.
But careful: ordinal(2000-01-01) = sum_{year=1}^{1999} days_in_year(year) + 0? But easier: compute days from 1/1/1. Then days difference = ordinal(date2) - ordinal(date1). Since date1 inclusive, date2 exclusive, if date1 = 2006-09-02 and date2 = 2006-09-03, ordinal(date2)-ordinal(date1)=1. Works.
But need to ensure correct handling of leap years: days in month: for Feb: 28 or 29. Use standard rules.
Implementation steps:
-
Read all numbers line by line until negative detected (all six negative? Actually spec: "When any of y1, m1, d1, y2, m2, and d2 is a negative number, the input ends." So if any is negative, we stop. So we need to check after reading: if y1<0 or m1<0 or d1<0 or y2<0 or m2<0 or d2<0: break. But example input shows "-1 -1 -1 -1 -1 -1" which is all negative; but condition says any negative, so break.
-
For each dataset, compute days between.
Edge cases: Ensure date1 <= date2. Not required to check, but input says so.
Compute days: Could compute using Python's datetime module? But this is a typical problem for coding competition; they often expect manual calculation due to large year ranges? But datetime module supports only year up to 9999 maybe. Actually Python's datetime supports year from 1 to 9999, so years like 2101 are fine. But if years beyond 9999, then not. Since constraints not huge, could use datetime. But might want to be safe: implement manual ordinal.
Better to implement manual: define function is_leap(year): (year%4==0 and year%100!=0) or (year%400==0). Then days_in_month array: for month 1..12; for Feb: 29 if leap else 28.
Compute days from epoch: We'll take epoch as year 1, month 1, day 1. Compute total days up to but not including that date? Actually we need ordinal such that ordinal(date) = number of days from 1/1/1 to that date (date inclusive? exclusive? Let's define precisely: Let ordinal(date) = number of days from 0001-01-01 to (year, month, day) where date is considered to start at day 1? Actually if we define ordinal(0001-01-01) = 1 (meaning it's day 1), then difference between two dates would be ordinal(d2) - ordinal(d1) gives the number of days from d1 inclusive to d2 inclusive? Let's test: d1=2006-09-02, d2=2006-09-03. If ordinal(d1)=some value A, ordinal(d2)=A+1. Then difference =1. That would count d1? Actually if we count days in the interval [d1, d2) we want 1 day (only d1). So if ordinal(d1)=A, ordinal(d2)=A+1, then ordinal(d2)-ordinal(d1)=1, which matches desired count. So we can set ordinal such that it's like "days since 0001-01-01" with day 1 as 1. Then days difference = ordinal(d2) - ordinal(d1). That works.
Thus we need to compute ordinal date. For a given date (y,m,d):
- Sum days for years from 1 to y-1 inclusive (full years). Each year: 365 + (1 if leap else 0).
- Sum days for months from 1 to m-1 in year y, using correct days per month depending on leap status of y.
- Add d.
But careful: If we treat days as 1-indexed, then adding d means that from 1 Jan of year y, the day number for Jan 1 is 1. So indeed ordinal( y, 1, 1 ) = 1 + sum_{years=1}^{y-1} days_in_year. So we need to ensure that sum_{years=1}^{y-1} days_in_year yields the number of days before year y. Then adding month days and day yields ordinal.
Alternatively, compute number of days from a base (like 0000-12-31) so that Jan 1 is 1? Let's define:
Let days_before_year(y) = total days for years 1..y-1.
Then ordinal = days_before_year(y) + days_before_month(y,m) + d
Where days_before_month(y,m) = sum_{i=1}^{m-1} days_in_month(i, leap(y)).
Now we need to compute days_before_year for year 1.. y-1. Year 1 is year 1 (AD). So for y=1, days_before_year(1)=0.
But what about year 0? Not needed.
Now compute days_in_year(y) = 365 + is_leap(y).
Implement as function.
Check for correctness with examples:
Example 1: 2006-09-02 to 2006-09-03.
Compute ordinal for 2006-09-02:
First compute days_before_year(2006): sum over years 1..2005.
We can compute with formula but easier to test via script? Let's manually verify small.
But we can test logic later.
Example 2: 2006-09-02 to 2006-11-11: output 70.
Example 3: 2004-01-01 to 2005-01-01: output 366 (since 2004 is leap, so days in 2004 = 366, from Jan 1 inclusive to Jan 1 exclusive next year => 366 days). Good.
Example 4: 2000-01-01 to 2006-01-01: output 2192. Let's verify: Years 2000,2001,2002,2003,2004,2005. 2000 leap (366), 2001 (365), 2002 (365), 2003 (365), 2004 leap (366), 2005 (365). Sum = 366+365+365+365+366+365 = compute: 366+365=731, +365=1096, +365=1461, +366=1827, +365=2192. Yes.
Example 5: 2000-01-01 to 2101-01-01: output 36890. We can check later.
So algorithm is straightforward.
We need to read input lines until negative.
Implementation details: Use Python.
Potential issues: Year range might be large (like up to 10^4 or more). But summing years up to 10^4 is trivial. However, if there are many datasets but each sum over many years (like 2000 to 2101 is 101 years, fine). Even worst-case if year up to maybe 10^5? Still OK for 50 datasets? 50 * 100,000 = 5 million, acceptable. But we can implement more efficient by using formula for leap year count. However, not needed.
But for correctness, note that we are counting days from year 1. But what about year 0? It's not used. Since year in input is positive? Could be year 0? But it says when negative number ends, so year likely positive. Could be year 0? Probably not, but maybe year 0 is not valid in Gregorian calendar? Usually no year 0. So assume year >= 1.
Also, the input may have years > 9999? Not sure, but we can handle with integer arithmetic.
Thus, implement:
def is_leap(y):
return (y % 4 == 0) and (y % 100 != 0) or (y % 400 == 0)
days_in_month = [0,31,28,31,30,31,30,31,31,30,31,30,31] (index 1-12). Adjust Feb.
def days_before_year(y):
total = 0
for year in range(1, y):
total += 365 + (1 if is_leap(year) else 0)
return total
But this loop could be slow for large years. We can compute leap count quickly: number of leap years from 1 to y-1 inclusive. Using formula: leaps = (y-1)//4 - (y-1)//100 + (y-1)//400. But careful: Year 1..y-1 inclusive. Actually year 1 is not leap? 1%4=1, not leap. So formula for number of leap years in [1, n] inclusive? Usually leap year calculation for Gregorian calendar: number of leap years from year 1 to year n inclusive = (n // 4) - (n // 100) + (n // 400). But this counts year 0? Let's test: For n=1, (1//4)=0, (1//100)=0, (1//400)=0 => 0. Correct (year 1 not leap). For n=4, (4//4)=1, (4//100)=0, (4//400)=0 =>1 (year 4 is leap). Good. So number of leap years from 1 to n inclusive is leaps(n) = n//4 - n//100 + n//400. For years from 1 to y-1 inclusive: leaps(y-1) = (y-1)//4 - (y-1)//100 + (y-1)//400. Then days_before_year(y) = (y-1)*365 + leaps(y-1).
But careful: Are we counting year 0? No.
Thus we can compute:
def days_before_year(y):
y1 = y - 1
return y1 * 365 + y1 // 4 - y1 // 100 + y1 // 400
Now days_before_month: need to compute sum of days for months 1..m-1 with leap adjustment.
Precompute cumulative days for non-leap year: cum_days = [0,0,31,59,90,120,151,181,212,243,273,304,334] (for month index m: cumulative days before month m). For leap year, from March onward add 1. So days_before_month(y,m) = cum_days[m] + (1 if m > 2 and is_leap(y) else 0).
Then ordinal = days_before_year(y) + days_before_month(y,m) + d.
But note: days_before_year(y) gives number of days from year 1 Jan 1 to the start of year y? Let's verify: For y=1, days_before_year(1)=0. For y=2, days_before_year(2)= days in year 1 = 365 (since 1 is not leap). So ordinal(2,1,1) = 365 + 0 + 1 = 366? Actually we want ordinal(1,1,1) = 1, ordinal(2,1,1) = 1+365 = 366? That would be correct if ordinal counts days since 1/1/1 inclusive. Then the difference between (2,1,1) and (1,1,1) is 365 days (the days of year 1). So that's fine. So ordinal(1,1,1)=1. Then ordinal(2,1,1)=366. Yes.
Check: For date (1,1,1): days_before_year(1)=0, days_before_month(1,1)=0, d=1 => 1.
Now for date (2006,9,2): compute days_before_year(2006) = (2005)365 + leaps(2005). leaps(2005) = 2005//4 - 2005//100 + 2005//400 = 501 - 20 + 5 = 486? Actually 2005//4=501 (since 4501=2004), 2005//100=20, 2005//400=5. So 501-20+5=486. So days_before_year = 2005365 = 2005365 =? 2005365 = (2000365)+(5365)=730000+1825=731825? Let's compute precisely: 2000365=730,000; 5*365=1,825; total=731,825. Then +486=732,311. So days_before_year(2006) = 732,311. Now days_before_month(2006,9): non-leap? 2006 is not leap. cum_days[9] = sum Jan-Aug: let's compute cum_days: Jan 0, Feb 31, Mar 31+28? Actually cum_days array: standard for non-leap: month index: 1:0,2:31,3:59,4:90,5:120,6:151,7:181,8:212,9:243,10:273,11:304,12:334. So cum_days[9]=243. m=9>2 but leap? false, so add 0. So days_before_month = 243. Then d=2. So ordinal = 732311+243+2 = 732556. For date (2006,9,3): similar: 732311+243+3=732557. diff=1. Good.
Second example: 2006-11-11. days_before_month(2006,11): cum_days[11]=304. So ordinal=732311+304+11=732626? Actually 732311+304=732615, +11=732626. For 2006-09-02: 732556. diff=732626-732556=70. Works.
Third example: 2004-01-01: y=2004. days_before_year(2004) = (2003)365 + leaps(2003). leaps(2003)=2003//4 - 2003//100 + 2003//400 = 500 - 20 + 5 = 485? Actually 2003//4=500, 2003//100=20, 2003//400=5 => 485. days_before_year=2003365=2003365. 2000365=730000, 3365=1095, total=731095. +485=731580. days_before_month(2004,1)=0, d=1 => ordinal=731581. For 2005-01-01: days_before_year(2005)=2004365 + leaps(2004). leaps(2004)=2004//4 - 2004//100 + 2004//400 = 501 - 20 + 5 = 486. 2004365=2004365=2000365=730000, 4365=1460, total=731460? Wait 2004365 = (2000365=730,000) + (4*365=1,460) = 731,460. Then +486 = 731,946. days_before_month=0,d=1 => ordinal=731,947. diff = 731,947-731,581 = 366. Good.
So our ordinal function is correct.
Thus for each dataset compute ordinal2 - ordinal1.
Potential issues: input may have trailing spaces and newlines. We'll read from sys.stdin, split.
Implementation:
import sys
def is_leap(y):
return (y % 400 == 0) or (y % 4 == 0 and y % 100 != 0)
def days_before_year(y):
y1 = y - 1
return y1 * 365 + y1 // 4 - y1 // 100 + y1 // 400
cumulative days before month (for non-leap)
cum_days = [0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334] # index 0 not used, index 1 Jan?
Actually careful: For month 1 (Jan), before month 1 sum = 0, we need cum_days[1]=0. So list of length 13: cum = [0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334]. We'll index from 1 to 12.
Alternative: predefine month_days = [31,28,31,30,31,30,31,31,30,31,30,31] and compute cum.
But we can define:
cum_days = [0]*13
for i in range(2,13):
cum_days[i] = cum_days[i-1] + month_days[i-2]?? Actually easier: month_days_noleap = [31,28,31,30,31,30,31,31,30,31,30,31]
cumulative = 0
cum = [0]
for days in month_days_noleap:
cum.append(cumulative)
cumulative += days
This gives cum[1]=0, cum[2]=31, cum[3]=59, etc. Good.
We'll define.
def ordinal(y, m, d):
days = days_before_year(y)
# add days before month m
days_before_m = cum[m] + (1 if m > 2 and is_leap(y) else 0)
return days + days_before_m + d
Then for each dataset: read line, split into ints; if any <0 break; compute ans = ordinal(y2,m2,d2) - ordinal(y1,m1,d1); print ans.
Now, check example 5: 2000-01-01 to 2101-01-01.
Compute ordinal(2000,1,1) and ordinal(2101,1,1). Need to verify output 36890.
Compute days_before_year(2000) = (1999)365 + leaps(1999). leaps(1999)=1999//4 - 1999//100 + 1999//400. 1999//4=499, 1999//100=19, 1999//400=4 => leaps=499-19+4=484. So days_before_year(2000)=1999365 + 484.
1999*365 = (2000-1)365 = 730000 - 365 = 729635? Actually 2000365=730,000; minus 365 = 729,635. Then +484 = 730,119. So days_before_year(2000)=730,119.
Now ordinal(2000,1,1)=730119 + 0 + 1 = 730,120.
Compute for 2101: days_before_year(2101) = (2100)365 + leaps(2100). leaps(2100)=2100//4 - 2100//100 + 2100//400. 2100//4=525, 2100//100=21, 2100//400=5 => leaps=525-21+5=509. So days_before_year=2100365 + 509.
2100365 = (2000365=730,000) + (100365=36,500) = 766,500? Actually 2000365=730,000, plus 100365=36,500 gives 766,500. But careful: 2100 = 2000+100, so yes 2100365 = 2100*365 = let's compute precisely: 2100 * 365 = (2000 * 365) + (100 * 365) = 730,000 + 36,500 = 766,500. Then +509 = 767,009. Then ordinal(2101,1,1) = 767,009 + 0 + 1 = 767,010.
Difference = 767,010 - 730,120 = 36,890. Yes matches output 36890.
Great.
Edge Cases:
- date1 = date2 => ans = 0.
- month and day valid? Input assumed valid.
Now implement.
Now, some nuance: The problem statement: "Date 1 is included in the number of days, not date 2." So we count from date1 to date2-1 day. That's exactly diff.
Now we must output each answer on separate line.
Potential problem: Input may contain multiple datasets separated by whitespace. Could read all ints from stdin and process six at a time until negative. That's simpler: read all ints from sys.stdin.read().split() and iterate.
But careful: The input ends when any of the six numbers is negative. So if we read six ints, check if any is negative, if yes break. But what if the negative number appears in the middle of a dataset? It says "When any of y1, m1, d1, y2, m2, and d2 is a negative number, the input ends." So it's safe to break if any negative appears. In the example, the sentinel is all negative but could be just -1 for first etc. So break if any of the six is <0.
Implementation:
import sys
def main():
data = sys.stdin.read().strip().split()
# process in chunks of 6
i = 0
out_lines = []
while i + 6 <= len(data):
y1, m1, d1, y2, m2, d2 = map(int, data[i:i+6])
if y1 < 0 or m1 < 0 or d1 < 0 or y2 < 0 or m2 < 0 or d2 < 0:
break
ans = ordinal(y2, m2, d2) - ordinal(y1, m1, d1)
out_lines.append(str(ans))
i += 6
sys.stdout.write("\n".join(out_lines))
But we need to define ordinal and cumulative before main.
We'll define cum list globally.
Implementation details:
month_days = [31,28,31,30,31,30,31,31,30,31,30,31]
cum = [0] # cum[0]=0 for month 0? but we'll index from 1
total = 0
for days in month_days:
cum.append(total)
total += days
cum length 13: cum[1]=0, cum[2]=31, cum[3]=59,...
Alternatively we could hardcode cum = [0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334].
Now, is_leap as defined.
Now, days_before_year function: y1 = y-1; return y1*365 + y1//4 - y1//100 + y1//400.
But careful: For y = 1, y1=0, returns 0. Good.
Now ordinal: return days_before_year(y) + cum[m] + (1 if (m>2 and is_leap(y)) else 0) + d
Note: d is day of month. So ordinal includes the day number.
Now test on given examples.
Edge Cases: Need to ensure that month and day are within valid ranges? Not necessary but assume valid.
Now produce output.
One more nuance: The problem statement mentions "The number of datasets does not exceed 50." So performance fine.
Now just write final code.
But wait: The problem says "Date 1 (y1, m1, d1) is the same as or earlier than date 2 (y2, m2, d2)." So we don't need to handle reversed.
Now we can produce final answer.
I'll output code in Python.
One potential improvement: Use sys.stdin buffer.
Write as:
import sys
def is_leap(y):
return y % 400 == 0 or (y % 4 == 0 and y % 100 != 0)
def days_before_year(y):
y -= 1
return y * 365 + y // 4 - y // 100 + y // 400
cumulative days before month (non-leap)
cum_days = [0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334]
def ordinal(y, m, d):
days = days_before_year(y) + cum_days[m] + d
if m > 2 and is_leap(y):
days += 1
return days
def main():
import sys
out = []
for line in sys.stdin:
parts = line.split()
if len(parts) < 6:
continue # maybe skip empty lines
y1, m1, d1, y2, m2, d2 = map(int, parts[:6])
if y1 < 0 or m1 < 0 or d1 < 0 or y2 < 0 or m2 < 0 or d2 < 0:
break
ans = ordinal(y2, m2, d2) - ordinal(y1, m1, d1)
out.append(str(ans))
sys.stdout.write("\n".join(out))
if name == "main":
main()
That processes line by line. Could also read all.
Now test with example:
Input:
2006 9 2 2006 9 3
2006 9 2 2006 11 11
2004 1 1 2005 1 1
2000 1 1 2006 1 1
2000 1 1 2101 1 1
-1 -1 -1 -1 -1 -1
Will output:
1
70
366
2192
36890
Matches.
Edge: If the sentinel appears as just "-1" somewhere? The spec says "When any of y1, m1, d1, y2, m2, and d2 is a negative number, the input ends." So we break as soon as any negative in a dataset. So line "-1 -1 -1 -1 -1 -1" triggers break.
Thus solution works.
Now produce final answer with code.
But the problem is likely from an online judge (maybe AIZU?). They expect output exactly as described.
Now, we need to return the answer: "Create a program that takes two dates as input..." So final code.
Now produce final answer. ```python
import sys
def is_leap(y: int) -> bool:
"""Return True if y is a leap year (Gregorian calendar)."""
return (y % 400 == 0) or (y % 4 == 0 and y % 100 != 0)
Days from 1/1/1 to the start of year y (i.e. before Jan 1 of year y)
def days_before_year(y: int) -> int:
y1 = y - 1
return y1 * 365 + y1 // 4 - y1 // 100 + y1 // 400
Cumulative days before each month in a non‑leap year.
cum[m] = total days in months 1 .. m-1 (1‑based, cum[1] = 0)
cum = [0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334]
def ordinal(y: int, m: int, d: int) -> int:
"""Number of days from 1/1/1 to (y,m,d) inclusive."""
days = days_before_year(y) + cum[m] + d
if m > 2 and is_leap(y):
days += 1 # February 29 is counted
return days
def main() -> None:
out_lines = []
for line in sys.stdin:
parts = line.split()
if len(parts) < 6:
continue # ignore empty or malformed lines
y1, m1, d1, y2, m2, d2 = map(int, parts[:6])
# Stop at the first negative number (sentinel)
if y1 < 0 or m1 < 0 or d1 < 0 or y2 < 0 or m2 < 0 or d2 < 0:
break
ans = ordinal(y2, m2, d2) - ordinal(y1, m1, d1)
out_lines.append(str(ans))
sys.stdout.write("\n".join(out_lines))
if name == "main":
main()