Advertisement
Musical_Muze

Day 1, Part 2

Dec 2nd, 2019
853
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.68 KB | None | 0 0
  1. import java.Math;
  2. import java.util.Scanner;
  3. import java.io.*;
  4.  
  5. public class Fuel2 {
  6.    
  7.     public static void main (String[] args) throws IOException{
  8.        
  9.         int sum = 0;
  10.        
  11.         File input = new File("modules.txt");
  12.         Scanner modules = new Scanner(input);
  13.        
  14.         FileWriter output = new FileWriter("answer.txt");
  15.        
  16.         while(modules.hasNext()){
  17.             int mass = modules.nextInt();
  18.             double fuel = (Math.floor((mass/3)))-2;
  19.             double fuelTotal = fuel;
  20.             while(fuel>0) {
  21.                 fuel = (Math.floor((fuel/3)))-2;
  22.                 if(fuel>0) {
  23.                     fuelTotal += fuel;
  24.                 }
  25.             }
  26.             sum += fuelTotal;
  27.         };
  28.        
  29.         System.out.println("The total fuel required is " + sum);
  30.         output.write(sum);
  31.    
  32.     };
  33.    
  34. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement