How to correctly use feof() for Kattis problems in PHP

30 views Asked by At

I'm currently working on Problems on the Kattis programming challenges site. I'm solving my problems with php, as that is the language I am most comfortable with. Here a link to one of the problems: https://open.kattis.com/problems/different

When I run my code locally, even via the shell as recommended by Kattis it works flawlessly. It terminates properly and everything. But when I submit my code to Kattis it always gives me a Run-Time Error. At this point I'm pretty sure that it's got to do with the End of File handling, as the way you are supposed to recognize when to output your answer is via the eof pointer. As said this works in my local shell, but as I have run into this problem in two Kattis problems that use eof for termination and both don't work I'm pretty certain something is not quite right with my eof handling on Kattis.

Here is my code:

<?php
$Results = [];
while(feof(STDIN) === False)
  {
    $input = trim(fgets(STDIN));
    $data = explode(" ", $input);
    $data[0] = intval($data[0]);
    $data[1] = intval($data[1]);
    if(($data[0] - $data[1]) >= 0)
    {
      array_push($Results, ($data[0] - $data[1]));
    }
    else
    {
      array_push($Results, ($data[1] - $data[0]));
    }
  }
for($i = 0; $i < count($Results); $i++)
{
  fprintf(STDOUT, "%s", $Results[$i].PHP_EOL);
}
?>

I have also tried the more standard

while(!feof(STDIN)){}

But that also doesn't work.

enter image description here This is the output of my code when run locally. It terminates by itself and correctly solves the sample test case Kattis provides. Also before someone suggests it, no it's not got to do with the mismatched file name, I manually paste the code into Kattis and this has worked great for the first 10+ problems that did not terminate via eof.

I have also contacted Kattis support, but that hasn't been to helpful as of yet. My question is: what is the right way to detect eof such that it works on Kattis?

Here are all the Kattis support links I have already looked at:

0

There are 0 answers