[Community Puzzle] [Solved] How time flies: Not all tests passed


#1

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?

Thanks in advance :slight_smile:


#2

Try
01.01.2014
01.05.2014
and
01.01.2014
01.06.2014


#3

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!


#4

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);
}

?>

#5

Hi there !

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


#6

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 :slight_smile: Solved!


#7

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?


#8

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 BEGIN = Console.ReadLine();
string END = Console.ReadLine();

        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" : "";
    }
}

}


#9

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”?


#10

Only the following is on codin

static void Main(string[] args)
{
string BEGIN = Console.ReadLine();
string END = Console.ReadLine();

    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?


#11

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.


#12

define “fail miserably”


#13

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.


#14

What is your Culture of your Visual Studio.

Your date format can be interpreted like ‘day.month.year’ like in French thus 7 month +/- 213 days OR ‘month.day.year’ like in US thus 7 days :slight_smile:

Ps: You should be able to keep your code as it (with namespace and whatesoever) on CG


#15

Hi @_CG_SaiksyApo,
I tried this:
Console.WriteLine(Thread.CurrentThread.CurrentCulture);
gives me: en-US. Is that your question?


#16

Yes, I used:

using System.Globalization;
Console.WriteLine(CultureInfo.CurrentCulture);

And it shows en-US too (on CG), I don’t understand why it’s parsed like ‘day.month.year’ in your VS tho :’(


#17

indeed, the culture needed to be set though VS did not have a problem with that. thank you.


#18

Hi, I think you all are cheating, using Date/Time functions of languages.
I suppose the spirit of the puzzle is solving it without them, otherwise some language could do all job for you.
But I can be wrong :wink:


#19

You can solve with just this function and the plain old math:

function daysinmonth(m,y){
  var days=[31,28,31,30,31,30,31,31,30,31,30,31];
  return days[m-1] + ( ((m==2)&&(((y%4==0)&&(y%100!=0))||(y%400==0))) ?1:0);
}

or, if you don’t like arrays and really love math :wink: :

function daysinmonth(m,y){
 return (((Math.abs(2*m-15)+1)/2)%2) + ( (m!=2)?30: ( (((y%4==0)&&(y%100!=0))||(y%400==0)) ?29:28) );
}

#20

is there anyone can shed some light as to why some of the areas in the validators didn’t pass?