mirror of
https://github.com/ckaczor/Advent2019.git
synced 2026-01-13 17:22:15 -05:00
48 lines
912 B
C#
48 lines
912 B
C#
using System;
|
|
using System.IO;
|
|
|
|
namespace Advent
|
|
{
|
|
public static class Day1
|
|
{
|
|
public static void Execute()
|
|
{
|
|
var lines = File.ReadAllLines(@".\Day1\input.txt");
|
|
|
|
var total = 0.0;
|
|
|
|
foreach (var line in lines)
|
|
{
|
|
var mass = double.Parse(line);
|
|
|
|
var fuel = CalculateFuel(mass);
|
|
|
|
total += fuel;
|
|
}
|
|
|
|
Console.WriteLine(total);
|
|
}
|
|
|
|
private static double CalculateFuel(double mass)
|
|
{
|
|
var totalFuel = 0.0;
|
|
|
|
var fuel = Math.Floor(mass / 3) - 2;
|
|
|
|
totalFuel += fuel;
|
|
|
|
while (fuel > 2)
|
|
{
|
|
fuel = Math.Floor(fuel / 3) - 2;
|
|
|
|
if (fuel < 0)
|
|
fuel = 0;
|
|
|
|
totalFuel += fuel;
|
|
}
|
|
|
|
return totalFuel;
|
|
}
|
|
}
|
|
}
|