static void Main(string[] args) { int totalFuel = 0; foreach (int mass in Fuel.masses) { totalFuel += FuelCalc.calcTotalFuel(mass); } Console.WriteLine(totalFuel); }
public static int calcTotalFuel(int mass) { int[] fuelArr = new int[] { mass }; while (true) { int fuel = FuelCalc.calcFuel(fuelArr[fuelArr.Length - 1]); if (fuel < 0) { break; } Array.Resize(ref fuelArr, fuelArr.Length + 1); fuelArr[fuelArr.Length - 1] = fuel; } return(fuelArr.Sum() - mass); }