[Community Puzzle] How time flies

Hi guys, I fail the 3rd and 8th validator and I have no idea why… :frowning_face:

Maybe you are handling leap years incorrectly?
Validator 8 is similar to this:

19.03.2024
19.03.2025

Answer

1 year, total 365 days

2 Likes

I handled it wrong, but got it now. Thanks a lot! :grin:

Hello

The code passes all of the testcases, but unfortunatly only passes about half of the tests once submitted.

I would very much appreciate some guidence as to how to improve my solution.

I’m a beginner programmer, so I apologize for my messy code.

Here it is below: (in Java)

import java.util.*;

import java.io.*;

import java.math.*;

class Solution 

{

    private static int[] daysInEachMonth;

    public static int numDays;

    public static int numMonths;

    public static int numYears;

    public static int startDay;

    public static int startMonth;

    public static int startYear;

    public static int endDay;

    public static int endMonth;

    public static int endYear;

    public static int totalDays;

    public static String BEGIN;

    public static String END;

    //converts string to int

    public static int toInt(String str)

        {

            int newInt = Integer.parseInt(str);

            return newInt;

        }

    //adds s if plural and removes text if difference is 0

    public static String updateString(String date, int number)

    {

        if(number > 1)

        {

            date += "s";

        }

        else if(number == 0)

        {

             return "";

        }

        return number + " " + date + ", ";

    }

//adds number of days in each month to corresponding spot in array; ex: 1 = 31, 2 = 28

    private static void addDays()

    {

        for(int i = 0 ; i < 12; i++)

        {

            daysInEachMonth[i] = 31;

            if(i == 3 || i == 5 || i == 8 || i == 10)

            {

                daysInEachMonth[i] = 30;

            }

            else if(i == 1)

            {

                daysInEachMonth[i] = 28;

            }

            

        }

    }

    private static void calculateYears()

    {

        numYears = 0;

        

        for(int y = startYear; y < endYear; y++)

        {

            numYears++;

        }

        if(startMonth > endMonth)

        {

            numYears--;

        }

        else if(startMonth == endMonth && startDay > endDay)

        {

            numYears--;

            numMonths = 12;

        }

    }

    public static void calculateMonths()

    {

        if(startDay != endDay)

        {

            if(endDay > startDay)

            {

                numMonths++;

            }

            else

            numMonths--;

        }

        for(int m = startMonth; m != endMonth; m++)

        {

            if(m == 12)

            {

                m = 0;

            }

            numMonths++;

        }

    }

    public static void calculateDays()

    {

        numDays = 0;

        if(startDay != endDay)

        {

            numDays = daysInEachMonth[startMonth - 1] - startDay;

            numDays += endDay;

        }

    }

    public static void calculateTotalDays()

    {

        totalDays = 0;

        totalDays += numYears * 365;

        totalDays += numDays;

        for(int x = startYear; x <= endYear; x++)

        {

            if(x % 4 == 0 && !BEGIN.equals(END))

            {

                if(startMonth <= 2 && x == startYear)

                {

                    totalDays++;

                }

                else if(startMonth >= 2 && x == endYear)

                {

                    totalDays++;

                }

                else if(x != startYear && x != endYear)

                {

                    totalDays++;

                }

            }

        }

        if(startDay != endDay)

        {

            if(startDay > 1)

            {

                startMonth++;

                if(startMonth > 12)

                {

                    startMonth = 1;

                }

            }

            else if(endMonth < daysInEachMonth[endMonth - 1])

            {

                endMonth--;

                if(endMonth < 1)

                {

                    endMonth = 12;

                }

            }

        }

            for(int i = startMonth; i != endMonth; i++)

            {

                totalDays += daysInEachMonth[i - 1];

                if(i == 12)

                {

                    i = 1;

                    totalDays += daysInEachMonth[i - 1];

                }

            }

    }

    public static void getNumbers()

    {

        startDay = toInt(BEGIN.substring(0,2));

        startMonth = toInt(BEGIN.substring(3,5));

        startYear = toInt(BEGIN.substring(6));

        endDay = toInt(END.substring(0,2));

        endMonth = toInt(END.substring(3,5));

        endYear = toInt(END.substring(6));

    }

    public static void main(String args[]) {

        String year = "year";

        String month = "month";

        String day = "day";

        numMonths = 0;

        Scanner in = new Scanner(System.in);

        BEGIN = in.next();

        END = in.next();

        getNumbers();

        daysInEachMonth = new int[12];

        addDays();

        calculateYears();

        calculateMonths();

        calculateDays();

        calculateTotalDays();

        System.out.println(updateString(year, numYears) + updateString(month, numMonths) + "total " + totalDays + " days");

    }

}

As a java programmer, you should never manually calculate dates and leap years like what you are doing.
You can make good use of the built-in Calendar and similar classes for date/time calculations.

2 Likes

okay, thanks!
That made solving this puzzle a bit easier

Hi, I solved all of the testcases but when I sumbit it got 1 case wronge any other people having this problem?

I solved it in C#
The code:

    string BEGIN = Console.ReadLine();

    string END = Console.ReadLine();

    DateTime dtBEGIN = DateTime.ParseExact(BEGIN, "dd.MM.yyyy",null);

    DateTime dtEND = DateTime.ParseExact(END, "dd.MM.yyyy",null);

    //get difference of two dates

    var tsDiffOfDates = dtEND - dtBEGIN; // dtEND.Subtract(dtBEGIN); // System.TimeSpan

    int NN =  tsDiffOfDates.Days;

    int YY =  NN/365;

    int MM =  (NN%365)/30;

   

    if (YY>1) Console.Write($"{YY} years, ");

    else if (YY==1) Console.Write($"{YY} year, ");

    if (MM>1) Console.Write($"{MM} months, ");

    else if (MM==1 && NN!=30) Console.Write($"{MM} month, ");

    Console.WriteLine($"total {NN} days");

Try this case, which is similar to the validator your code fails at but not exactly the same:

10.05.2000
04.02.2020

The correct answer should be:
19 years, 8 months, total 7209 days

Some may also find the following cases useful for debugging (especially when failed to pass the validators “One month” or “Leap year”):

Input

15.01.2000
29.02.2000

Output

1 month, total 45 days

Input

15.02.2000
29.02.2008

Output

8 years, total 2936 days

Hi, I’m failing to the validator “Leap year” ; I tried the examples you posted on your last 2 messages, it helped me fix an error for “One month” but not for “Leap year”.
For now, I don’t really have an idea of what could be faulty in my logic, could you provide me another example please ?
Thanks in advance.

Please send me your code in private message so that I may come up with another custom case.

Thanks to @5DN1, here are two test cases which helped me fix my code, it may help some of you.

(1)
Input

10.02.2064
14.02.2072

Output

8 years, total 2926 days

(2)
Input

29.02.2020
28.02.2022

Output

2 years, total 730 days
1 Like

Hey, please, I only have the 3rd case that is not working. What are we testing there?

I masked one of your message.
Anyway, you can send me your code (and its language) in private, I’ll send back what’s wrong.

Please send your code by private message instead of posting it publicly on the forum. Thanks.