# [Community Puzzle] How time flies

Hi everyone, I am working on the “How time flies” puzzle (https://www.codingame.com/training/easy/how-time-flies) but there seem to be an issue with either my logic or the test answers to “Full months” and “Complex”.

For “Full month” I expect answer to have a total of 214 days and not 213 because the 7 months separating Jan 1rst and Aug 1rst respectively have (Jan)31,(Feb)30,(Mar)31,(Apr)30,(May)31,(Jun)30,(Jul)31 days (as 2016 is a leap year). To reach the same day from one month (A) to the next (B) you have to spend the number of days in A.

For complex, same logic, I expect 1141 days and not 1140 as you need

• 3 years to go to 28.02.2018 representing 1096 days (365 from 2015 to 2016, 366 to 2017 as 2016 is a leap year, and 365 to 2018).
• 1 month to go to 28.03.2018 representing 29 days (same logic as for “Full month” and 2018 is not a leap year)
• 16 days to go to 13.04.2018 (3 days till March 31rst and 13 additional days till April 13th)
1096+29+16 = 1141

Where am I wrong ?

February with 30 days?

1 Like

Lol thanks

Clearly it is a double leap year. Pssshaw!

Haha that’s what happens when you wanna finish the puzzle even though your brain is asking for sleep ! I wanted to delete the discussion as I don’t want people to waste their time on that but I don’t see any option to do that …

1 Like

Naw, bro. This is one of those “relatable silly mistakes”

Anybody who makes fun of you… just hasn’t done this ten times yet.

Also, make certain not to over exert yourself. Burning out isn’t that fun.

Hi there.
I’m here again for a specific puzzle in Community; How Time Flies
I can’t remember all the times where I passed the tests in editor, but not the “official” tests.
Each time, I managed to figure out what was wrong with my code.
But this time, given how simple the code is, I’m just unable to debug it.
Here is the culprit:

``````<?php

function getRightFormat(\$d) {
return substr(\$d, 6, 4) . '-' . substr(\$d, 3, 2) . '-' . substr(\$d, 0, 2);
}

fscanf(STDIN, "%s",
\$BEGIN
);
fscanf(STDIN, "%s",
\$END
);

error_log(\$BEGIN);
error_log(\$END);

date_default_timezone_set('Europe/Paris');

\$d1 = new DateTime(getRightFormat(\$BEGIN));
\$d2 = new DateTime(getRightFormat(\$END));
\$diff = \$d2->diff(\$d1);

if (\$diff->y > 0) {
echo \$diff->y . ' year';
if (\$diff->y > 1) echo 's';
echo ', ';
}

if (\$diff->m > 0) {
echo \$diff->m . ' month';
if (\$diff->m > 1) echo 's';
echo ', ';
}

echo 'total ';
\$days = \$diff->format('%a');
echo \$days . ' days';

?>
``````

It passes all the tests but “Full months” and “Years and months”.
I did some tests with February and leap years, I saw no problem.
Where am I wrong?

Try
01.01.2014
01.05.2014
and
01.01.2014
01.06.2014

Hi @RoboStac,

Thanks for these tests,
The second passes correctly, showing ‘5 months, total 151 days’,
But the first shows ‘3 months, total 120 days’…
So apparently, the right amount of days, but not the right amount of months. Wtf?
I suppose it hasn’t to do with leap years, since 2014 is not. I will investigate on this. Thanks!

I come with another solution, again it passes all the tests, but this time it fails the “Days only” and the “Complex” tests…
But I now display the correct answer for the test cases you suggested. I don’t understand.
My new code:

``````<?php

\$BEGIN = getInputString();
\$END = getInputString();

error_log(\$BEGIN);
error_log(\$END);

\$data1 = getDateAsIntArray(\$BEGIN); // [0] = day, [1] = month, [2] = year
\$data2 = getDateAsIntArray(\$END);

date_default_timezone_set('Europe/Paris');

\$d1 = new DateTime(getPHPDateFormat(\$BEGIN));
\$d2 = new DateTime(getPHPDateFormat(\$END));

\$days = \$d2->diff(\$d1)->format('%a');

\$months = 0;
\$years = 0;

if (\$data1[1] != \$data2[1]) { // Not same month
\$months = abs(\$data2[1] - \$data1[1]);
if (\$data2[0] < \$data1[0]) \$months--;
if (\$data2[1] < \$data1[1]) \$months = 12 - \$months;
}

if (\$data1[2] != \$data2[2]) { // Not same year
\$years = \$data2[2] - \$data1[2];
if (\$data2[1] < \$data1[1] || \$data2[1] == \$data1[1] && \$data2[0] < \$data1[0]) \$years--;
}

displayResult(\$days, \$months, \$years);

function getInputString() {
fscanf(STDIN, "%s", \$var);
return \$var;
}

function getDateAsIntArray(\$date) {
\$ret = explode('.', \$date);
\$ret[0] = (int)\$ret[0];
\$ret[1] = (int)\$ret[1];
\$ret[2] = (int)\$ret[2];
return \$ret;
}

function displayResult(\$days, \$months, \$years) {

if (\$years > 0) {
echo \$years . ' year';
if (\$years != 1) echo 's';
echo ', ';
}

if (\$months > 0) {
echo \$months . ' month';
if (\$months != 1) echo 's';
echo ', ';
}

echo 'total ' . \$days . ' day';
if (\$days != 1) echo 's';
}

function getPHPDateFormat(\$date) {
return substr(\$date, 6, 4) . '-' . substr(\$date, 3, 2) . '-' . substr(\$date, 0, 2);
}

?>``````

Hi there !

I’ve got the same issue. Does anyone ever passed those validations ? What are the final validation rules against hard-coded answers ?

I feel like I’m close.
Days and years seem always correct with my code.
I have 2 ways to calculate months. Both of them pass editor tests, but not submission tests.

• The easy way is to rely on PHP’s diff() function. However, it fails “Full months” and “Years and months”.
• The “hard” way is to calculate it myself. However, it fails “Days only” and “Complex”…!!

So, if I use “hard” way only if i’m in “Full months” case, I manage to pass it and I am at 83%…
But I still fail “Years and months”.

EDIT: Just realized I had to do the exact same method for the “Years and months” test case. I’m such a n00b.
Removed the code so others n00bs don’t cheat Solved!

Can somebody help me with this?

I managed to pass all the test cases but I submit the code I get only 50% correct and it says that I failed on validators 3, 4 and 6.
I think I have a very good code, I have no idea of what could be wrong. I think it is not any hard coded solution.

Can somebody help with these three validators?

Can you please tell me what is wrong with this code, perfectly working in Visual Studio, but failing at date conversions on Codin Game??

using System;
using System.Linq;
using System.IO;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;

namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{

``````        string pattern = "dd.mm.yyyy";
DateTime start, end;
DateTime.TryParse(BEGIN, out start);
DateTime.TryParse(END, out end);
Console.WriteLine(start);
``````
``````        TimeSpan timeSpan = end - start;
DateTime timeDifference = DateTime.MinValue + timeSpan;
int years = timeDifference.Year - 1;
int months = timeDifference.Month - 1;
int days = (int)timeSpan.TotalDays;
``````
``````        if (years == 0)
{ }
else
{
Console.Write(years + " year" + pluralize(years) + ", ");
}
``````
``````        if (months == 0)
{ }
else
{
Console.Write(months + " month" + pluralize(months) + ", ");
``````
``````        }
``````
``````        Console.Write("total " + days + " day" + pluralize(days));
``````
``````        Console.ReadKey();
}
``````
``````    private static string pluralize(int number)
{
return number > 1 ? "s" : "";
}
}
``````

}

Hi @anaasa,
What is exactly happening when you say “failing at date conversions”?
Does it compile on CodinGame?
Did you remove the “namespace, class Program”?

Only the following is on codin

static void Main(string[] args)
{

``````    string pattern = "dd.mm.yyyy";
DateTime start, end;
DateTime.TryParse(BEGIN, out start);
DateTime.TryParse(END, out end);
Console.WriteLine(start);
``````
``````    TimeSpan timeSpan = end - start;
DateTime timeDifference = DateTime.MinValue + timeSpan;
int years = timeDifference.Year - 1;
int months = timeDifference.Month - 1;
int days = (int)timeSpan.TotalDays;
``````
``````    if (years == 0)
{ }
else
{
Console.Write(years + " year" + pluralize(years) + ", ");
}
``````
``````    if (months == 0)
{ }
else
{
Console.Write(months + " month" + pluralize(months) + ", ");
``````
``````    }
``````
``````    Console.Write("total " + days + " day" + pluralize(days));
``````
``````    Console.ReadKey();
}
``````
``````private static string pluralize(int number)
{
return number > 1 ? "s" : "";
}
``````

}

How to get around that mono limitation?

Hi @Eldoir, That is what I had in Visual Studio(which perfectly worked for ALL coding games test cases), but, when adjusted for Coding Games(yes, removed Program namespace and all that) , it fails miserably.

define “fail miserably”

For

01.01.2016
01.08.2016

using

DateTime start, end;
DateTime.TryParse(BEGIN, out start);
DateTime.TryParse(END, out end);
TimeSpan timeSpan = end - start;

On Codin Game C# timeSpan is 7.00:00:00 while in Visual Studio C# timeSpan is 213.00:00:00

That is clearly an issue.