Created
January 14, 2014 20:40
-
-
Save magickatt/8425277 to your computer and use it in GitHub Desktop.
Sports Points r/dailyprogrammer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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