Permalink
Browse files

Check that output returns valid results. Close #1

  • Loading branch information...
ShaneMcC committed Dec 3, 2018
1 parent 823c979 commit bfb438877b6fddc84dcd0aada828bfe08953bb76
Showing with 70 additions and 20 deletions.
  1. +64 −20 bench.php
  2. +6 −0 config.php
@@ -87,16 +87,21 @@ function getInput($day) {
if (file_exists($inputsDir . '/' . $day . '.txt')) {
$input = file_get_contents($inputsDir . '/' . $day . '.txt');
$answer1 = $input !== FALSE ? getInputAnswer($day, 1) : null;
$answer2 = $input !== FALSE ? getInputAnswer($day, 2) : null;
} else {
$source = $participants[0];
$cwd = getcwd();
$person = preg_replace('#[^A-Z0-9-_]#i', '', $source->getName());
chdir($participantsDir . '/' . $person);
$input = $source->getInput($day);
$answer1 = $input !== FALSE ? $source->getInputAnswer($day, 1) : null;
$answer2 = $input !== FALSE ? $source->getInputAnswer($day, 2) : null;
chdir($cwd);
}
return $input;
return [$input, $answer1, $answer2];
}
foreach ($participants as $participant) {
@@ -125,32 +130,42 @@ function getInput($day) {
if (!$participant->hasDay($day)) { continue; }
if (!preg_match('#^' . $wantedDay. '$#', $day)) { continue; }
$thisDay = isset($data['results'][$person]['days'][$day]) ? $data['results'][$person]['days'][$day] : ['times' => [], 'version' => 'Unknown'];
$thisDay = isset($data['results'][$person]['days'][$day]) ? $data['results'][$person]['days'][$day] : ['times' => []];
echo 'Day ', $day, ':';
if (isset($thisDay['version'])) {
if ($thisDay['version'] == $participant->getVersion($day)) {
if ($force) {
echo ' [Forced]';
} else {
echo ' No changes.', "\n";
continue;
}
}
if ($normaliseInput) {
[$input, $answer1, $answer2] = getInput($day);
$checkOutput = ($answer1 !== NULL && $answer2 !== NULL);
} else {
$input = $answer1 = $answer2 = NULL;
$checkOutput = FALSE;
}
if ($normaliseInput) {
$input = getInput($day);
if ($input !== FALSE) {
$participant->setInput($day, $input);
}
$currentVersion = isset($thisDay['version']) ? $thisDay['version'] : 'Unknown';
$checkedOutput = isset($thisDay['checkedOutput']) ? $thisDay['checkedOutput'] : FALSE;
// Should we skip this?
$skip = !empty($thisDay['times']);
$skip &= ($currentVersion == $participant->getVersion($day));
$skip &= (!$checkOutput || $checkOutput == $checkedOutput);
if ($force) {
echo ' [Forced]';
$skip = false;
}
if ($skip) { echo ' Up to date.', "\n"; continue; }
if ($input !== FALSE) {
$participant->setInput($day, $input);
}
// Run the day.
$long = false;
// Reset the times.
$thisDay['times'] = [];
$failedRun = false;
for ($i = 0; $i < ($long ? $longRepeatCount : $repeatCount); $i++) {
$start = time();
@@ -162,9 +177,29 @@ function getInput($day) {
if ($ret != 0) {
echo ' !';
echo "\n";
echo 'Exited with error.', "\n";
echo 'Output:', "\n";
foreach ($result as $out) { echo ' > ', $out, "\n"; }
$failedRun = true;
break;
} else { echo ' ', $i; }
} else {
echo ' ', $i;
if ($checkOutput) {
$rightAnswer = preg_match('#' . preg_quote($answer1, '#') . '.+' . preg_quote($answer2, '#') . '#', implode(' ', $result));
if (!$rightAnswer) {
echo 'F';
echo "\n";
echo 'Wanted Answers:', "\n";
echo ' Part 1:', $answer1, "\n";
echo ' Part 2:', $answer2, "\n";
echo "\n";
echo 'Actual Output:', "\n";
foreach ($result as $out) { echo ' > ', $out, "\n"; }
$failedRun = true;
break;
}
}
}
// If this was a long-running day, run future days less often.
if ($end - $start > $longTimeout) { $long = true; }
@@ -177,9 +212,18 @@ function getInput($day) {
}
echo "\n";
// Update data if we've actually ran enough times.
if (count($thisDay['times']) >= ($long ? $longRepeatCount : $repeatCount)) {
sort($thisDay['times']);
// Update data if we've actually ran enough times or if we failed.
if ($failedRun || count($thisDay['times']) >= ($long ? $longRepeatCount : $repeatCount)) {
// Invalidate any times if we failed or sort them for later.
if ($failedRun) {
unset($thisDay['times']);
} else {
sort($thisDay['times']);
if ($checkOutput) {
$thisDay['checkedOutput'] = true;
}
}
$thisDay['version'] = $participant->getVersion($day);
$data['results'][$person]['days'][$day] = $thisDay;
}
@@ -37,6 +37,8 @@ public function setInput($day, $input) {
file_put_contents($this->getInputFilename($day), $input);
}
public function getInputAnswer($day, $part) { return NULL; }
public function extractTime($output) {
$time = $output[count($output) - 3];
$time = trim(preg_replace('#^real#', '', $time));
@@ -72,3 +74,7 @@ public function cleanup() { }
if (file_exists(dirname(__FILE__) . '/config.local.php')) {
include(dirname(__FILE__) . '/config.local.php');
}
if (!function_exists('getInputAnswer')) {
function getInputAnswer($day, $part) { return NULL; }
}

0 comments on commit bfb4388

Please sign in to comment.