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
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 …
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:
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);
}
?>
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!
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 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;
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”?
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;
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.