ClarkeRubber

UNSW ProgComp: Problem 3 - 2011

Jul 18th, 2012
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.32 KB | None | 0 0
  1. <?php
  2.  
  3. //Input method, nothing fancy, deal with it.
  4. $input = <<<END
  5. 5
  6. 15
  7. 34
  8. 79
  9. 1
  10. 200
  11. END;
  12.  
  13. $input = explode("\n", $input); //split input into an array by new line feed (\n)
  14. array_shift($input); //eject the first value out of the array (can be captured, but not necessary)
  15.  
  16. //Create list of optimal places to hit per each number
  17.  
  18. for($x = 1; $x < 21; $x++){
  19.     $scores_list[$x] = "S$x";
  20. }
  21.  
  22. for($x = 1; $x < 21; $x++){
  23.     if(!isset($scores_list[$x*2])){
  24.         $scores_list[$x*2] = "D$x";
  25.     }
  26. }
  27.  
  28. for($x = 1; $x < 21; $x++){
  29.     if(!isset($scores_list[$x*3])){
  30.         $scores_list[$x*3] = "T$x";
  31.     }
  32. }
  33.  
  34. //Insert bulls-eye scores
  35.  
  36. $scores_list[50] = 'IB';
  37. $scores_list[25] = 'OB';
  38.  
  39. ksort($scores_list); //Sort array by array keys
  40. $scores_list = array_reverse($scores_list, true); //Reverse the order of the elements and preserve the keys
  41.  
  42. foreach($input as $key => $value){
  43.     //find largest number that can be taken away
  44.  
  45.     //take it away and record the new total, and the value that was taken away
  46.  
  47.     //if remainder is 0, stop. If remainder does not have a solution, take away the next largest value possible
  48.  
  49.     //loop
  50.    
  51.     $taken_away = array(); //array of the scores that are used to reach zero
  52.     $remainder = $value; //The amount that still has to be scored
  53.     $loop = 0; //Loop counter (similar to the counter in a for loop)
  54.     $sum = 0; //Sum of the targets that have been hit
  55.  
  56.     while($remainder > 0 && $loop < 3){ //While there are still numbers to be taken away and three dart throws are still to be made
  57.         foreach ($scores_list as $key => $value2) { //For each of the scores as $key and $value2
  58.             if(($remainder - $key) >= 0) { //If the remainder, minus the target to hit, is a valid place to hit, do:
  59.                 $remainder -= $key; //Remainder = remainder - target_to_hit
  60.                 $taken_away[] = $value2; //Add the target that was hit to the array 'taken_away'
  61.                 $sum += $key; //Add the target that was hit to the sum total.
  62.                 break; //Break out of the foreach loop as it has found a value and doesn't need to search anymore
  63.             }
  64.         }
  65.         $loop++; //Increase loop counter by 1
  66.     }
  67.  
  68.     //The rest of this is just displaying the output
  69.  
  70.     echo $value.": ";
  71.     $first = true;
  72.     foreach($taken_away as $key => $value){
  73.         if($first){
  74.             echo $value.' ';
  75.             $first = false;
  76.         }else{
  77.             echo '+ '.$value.' ';
  78.         }
  79.     }
  80.     echo '= '.$sum."\n";
  81. }
Advertisement
Add Comment
Please, Sign In to add comment