Skip to content

Instantly share code, notes, and snippets.

@magickatt
Created January 14, 2014 20:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save magickatt/8425277 to your computer and use it in GitHub Desktop.
Save magickatt/8425277 to your computer and use it in GitHub Desktop.
Sports Points r/dailyprogrammer
<?php
if ($argc <= 1)
exit("No score was passed\n");
$inputScore = (integer) $argv[1];
$x = 0;
$validScores = array($x);
print "Input score: $inputScore\n";
while ($x <= $inputScore) { // Do until last valid score is higher than input score
$array = $validScores;
foreach ($array as $score) { // For each of the valid scores
isScoreValid($score, 3, $inputScore, $validScores); // Field goal
isScoreValid($score, 6, $inputScore, $validScores); // Touchdown
isScoreValid($score, 7, $inputScore, $validScores); // Touchdown + Conversion
$x = isScoreValid($score, 8, $inputScore, $validScores); // Touchdown + Two-point Conversion
}
}
exit("Score is invalid\n"); // If input score not found yet, must be invalid
function isScoreValid($score, $points, $target, &$validScores) {
$x = $score + $points;
if (in_array($x, $validScores)) { // Check if this valid score already exists
/*print "Duplicate score: $x ($score + $points)\n";*/
return $x;
}
$validScores[] = $x; // If new valid score, add to array
print "Potential score: $x ($score + $points)\n";
if ($x == $target) { // Check if valid score is the input score
exit("Score is valid\n");
}
return $x;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment