package day01;

import common.BaseSolution;
import common.InputReader;

public class Solution extends BaseSolution implements common.Solution {

    public static void main(String[] args) {
        new Solution().run();
    }

    public String part1() {
        int mass, totalFuel = 0;
        InputReader reader = getInputReader();
        String line = reader.next();
        while (line != null) {
            mass = Integer.parseInt(line);
            totalFuel += getFuel(mass);
            line = reader.next();
        }
        return String.valueOf(totalFuel);
    }

    private int getFuel(int mass) {
        return mass / 3 - 2;
    }

    public String part2() {
        int mass, totalFuel = 0;
        InputReader reader = getInputReader();
        String line = reader.next();
        while (line != null) {
            mass = Integer.parseInt(line);
            totalFuel += getFuelWithResiduals(mass);
            line = reader.next();
        }
        return String.valueOf(totalFuel);
    }

    private int getFuelWithResiduals(int mass) {
        int fuel = 0;
        int fuelPiece = mass;
        do {
            fuelPiece = getFuel(fuelPiece);
            if (fuelPiece > 0) {
                fuel += fuelPiece;
            }
        } while (fuelPiece > 0);
        return fuel;
    }
}

@@ -228,32 +228,76 @@
     
-public String part1
+class BaseFuelCalculator %7B%0A        public int calculateFuel
 () %7B%0A
+    
     
@@ -317,32 +317,36 @@
  totalFuel = 0;%0A
+    
         InputRea
@@ -376,32 +376,36 @@
 ader();%0A        
+    
 String line = re
@@ -417,32 +417,36 @@
 next();%0A        
+    
 while (line != n
@@ -444,32 +444,36 @@
 line != null) %7B%0A
+    
             mass
@@ -503,32 +503,36 @@
 e);%0A            
+    
 totalFuel += get
@@ -547,32 +547,36 @@
 s);%0A            
+    
 line = reader.ne
@@ -573,32 +573,36 @@
  reader.next();%0A
+    
         %7D%0A      
@@ -603,38 +603,27 @@
         
+    
 return 
-String.valueOf(
 totalFue
@@ -615,35 +615,38 @@
 return totalFuel
-)
 ;%0A
+    
     %7D%0A%0A    priva
@@ -632,39 +632,45 @@
 %0A        %7D%0A%0A    
+ 
-private
+   protected
  int getFuel(int
@@ -665,16 +665,23 @@
  getFuel
+ForMass
 (int mas
@@ -677,32 +677,36 @@
 ass(int mass) %7B%0A
+    
         return m
@@ -726,282 +726,196 @@
     
-%7D%0A%0A
     
-public String part2() %7B%0A        int mass, totalFuel = 0;%0A        InputReader reader = getInputReader();%0A        String line = reader.next();%0A        while (line != null) %7B%0A   
+%7D%0A%0A        protected int getFuel(int mass) %7B%0A            return 0;%0A        %7D%0A
     
+%7D%0A%0A
     
- m
+cl
 ass 
-= Integer.parseInt(line);%0A            totalFuel += getFuelWithResiduals(
+Part1FuelCalculator extends BaseFuelCalculator %7B%0A        protected int getFuel(int 
 mass)
-;
+ %7B
 %0A   
@@ -923,35 +923,42 @@
         
-line = reader.next(
+return getFuelForMass(mass
 );%0A     
@@ -970,64 +970,87 @@
     
+%7D%0A%0A
     
-return String.valueOf(totalFuel);
+class Part2FuelCalculator extends BaseFuelCalculator %7B
 %0A    
-%7D%0A%0A
     pr
-iva
+otec
 te
+d
  int
@@ -1061,21 +1061,8 @@
 Fuel
-WithResiduals
 (int
@@ -1062,32 +1062,36 @@
 uel(int mass) %7B%0A
+    
         int fuel
@@ -1096,32 +1096,36 @@
 el = 0;%0A        
+ 
+   
 int fuelPiece = 
@@ -1142,13 +1142,21 @@
     
+   
+ 
 do %7B%0A
+    
     
@@ -1182,16 +1182,23 @@
  getFuel
+ForMass
 (fuelPie
@@ -1198,24 +1198,28 @@
 fuelPiece);%0A
+    
             
@@ -1251,24 +1251,28 @@
             
+  
+  
 fuel += fuel
@@ -1278,16 +1278,20 @@
 lPiece;%0A
+    
         
@@ -1304,16 +1304,20 @@
         
+    
 %7D while 
@@ -1337,24 +1337,28 @@
 0);%0A        
+  
+  
 return fuel;
@@ -1354,20 +1354,248 @@
 eturn fuel;%0A
+        %7D%0A    %7D%0A%0A    public String part1() %7B%0A        return String.valueOf(new Part1FuelCalculator().calculateFuel());%0A    %7D%0A%0A    public String part2() %7B%0A        return String.valueOf(new Part2FuelCalculator().calculateFuel());%0A
     %7D%0A%7D%0A

Refactor day 1 into reusable FuelCalculator resources