Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Reddit Intermediate Challenge #118

By: a guest on Apr 9th, 2013  |  syntax: Java  |  size: 2.91 KB  |  views: 19  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. package reddit;
  2.  
  3. import java.math.BigDecimal;
  4. import java.util.Scanner;
  5.  
  6. public class RedditChallengeIntermediate118 {
  7.  
  8.    
  9.     public static void main(String[] args) {
  10.         RedditChallengeIntermediate118 rc118 = new RedditChallengeIntermediate118();
  11.         Scanner scan = new Scanner(System.in);
  12.         String input = scan.nextLine();
  13.         //String input = "5.00 2.00 2.00 1.00";
  14.         //rc118.calculate(input.split(" "));
  15.         //input = "99.99 1.23 3.21 5.01";
  16.         rc118.calculate(input.split(" "));
  17.     }
  18.    
  19.    
  20.     public void calculate(String[] inputs) {
  21.         int simLength = new BigDecimal(inputs[0]).multiply(new BigDecimal(100)).setScale(0).intValueExact();
  22.         int shellRetrieval = new BigDecimal(inputs[1]).multiply(new BigDecimal(100)).setScale(0).intValueExact();
  23.         int propellantRetrieval = new BigDecimal(inputs[2]).multiply(new BigDecimal(100)).setScale(0).intValueExact();
  24.         int firingTime = new BigDecimal(inputs[3]).multiply(new BigDecimal(100)).setScale(0).intValueExact();
  25.        
  26.         int shotsFired = 0;
  27.        
  28.         boolean cannonLoaded = false;
  29.         boolean cannonFired = false;
  30.         boolean cannonReady = true;
  31.         int cannonTimer = 0;
  32.        
  33.         boolean shellLoaded = false;
  34.         int shellTimer = 0;
  35.        
  36.         boolean propellantLoaded = false;
  37.         int propellantTimer = 0;
  38.        
  39.         //System.out.println("Length of simulation: " + simLength);
  40.         //System.out.println("Length of shell retrieval: " + shellRetrieval);
  41.         //System.out.println("Length of propellant retrieval: " + propellantRetrieval);
  42.         //System.out.println("Length of firingTime: " + firingTime);
  43.        
  44.         for (int timer = 1; timer <= simLength; timer++) {
  45.             if (cannonFired) cannonTimer++;
  46.             if (!shellLoaded) shellTimer++;
  47.             if (!propellantLoaded) propellantTimer++;
  48.            
  49.             if (shellTimer == shellRetrieval) shellLoaded = true;
  50.             if (propellantTimer == propellantRetrieval) propellantLoaded = true;
  51.            
  52.             if (!cannonReady) {
  53.                 if (cannonTimer == firingTime) {
  54.                     cannonReady = true;
  55.                     cannonTimer = 0;
  56.                     cannonFired = false;
  57.                 }
  58.             }
  59.            
  60.             if (cannonReady) {
  61.                 if (shellLoaded && propellantLoaded) cannonLoaded = true;
  62.            
  63.                 if (cannonLoaded) {
  64.                     shotsFired++;
  65.                     cannonFired = true;
  66.                     cannonLoaded = false;
  67.                     cannonReady = false;
  68.                     shellLoaded = false;
  69.                     shellTimer = 0;
  70.                     propellantLoaded = false;
  71.                     propellantTimer = 0;
  72.                 }
  73.             }
  74.         }
  75.         System.out.println("Number of shots fired: " + shotsFired);
  76.     }
  77. }
clone this paste RAW Paste Data