Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- //Input method, nothing fancy, deal with it.
- $input = <<<END
- 5
- 15
- 34
- 79
- 1
- 200
- END;
- $input = explode("\n", $input); //split input into an array by new line feed (\n)
- array_shift($input); //eject the first value out of the array (can be captured, but not necessary)
- //Create list of optimal places to hit per each number
- for($x = 1; $x < 21; $x++){
- $scores_list[$x] = "S$x";
- }
- for($x = 1; $x < 21; $x++){
- if(!isset($scores_list[$x*2])){
- $scores_list[$x*2] = "D$x";
- }
- }
- for($x = 1; $x < 21; $x++){
- if(!isset($scores_list[$x*3])){
- $scores_list[$x*3] = "T$x";
- }
- }
- //Insert bulls-eye scores
- $scores_list[50] = 'IB';
- $scores_list[25] = 'OB';
- ksort($scores_list); //Sort array by array keys
- $scores_list = array_reverse($scores_list, true); //Reverse the order of the elements and preserve the keys
- foreach($input as $key => $value){
- //find largest number that can be taken away
- //take it away and record the new total, and the value that was taken away
- //if remainder is 0, stop. If remainder does not have a solution, take away the next largest value possible
- //loop
- $taken_away = array(); //array of the scores that are used to reach zero
- $remainder = $value; //The amount that still has to be scored
- $loop = 0; //Loop counter (similar to the counter in a for loop)
- $sum = 0; //Sum of the targets that have been hit
- while($remainder > 0 && $loop < 3){ //While there are still numbers to be taken away and three dart throws are still to be made
- foreach ($scores_list as $key => $value2) { //For each of the scores as $key and $value2
- if(($remainder - $key) >= 0) { //If the remainder, minus the target to hit, is a valid place to hit, do:
- $remainder -= $key; //Remainder = remainder - target_to_hit
- $taken_away[] = $value2; //Add the target that was hit to the array 'taken_away'
- $sum += $key; //Add the target that was hit to the sum total.
- break; //Break out of the foreach loop as it has found a value and doesn't need to search anymore
- }
- }
- $loop++; //Increase loop counter by 1
- }
- //The rest of this is just displaying the output
- echo $value.": ";
- $first = true;
- foreach($taken_away as $key => $value){
- if($first){
- echo $value.' ';
- $first = false;
- }else{
- echo '+ '.$value.' ';
- }
- }
- echo '= '.$sum."\n";
- }
Advertisement
Add Comment
Please, Sign In to add comment