2022-10-27 15:42:05 +00:00
|
|
|
namespace AOC2019;
|
|
|
|
|
2023-09-20 18:38:58 +00:00
|
|
|
public sealed class Day01() : Day(2019, 1, "The Tyranny of the Rocket Equation")
|
2022-10-27 15:42:05 +00:00
|
|
|
{
|
2022-12-08 19:40:40 +00:00
|
|
|
private IEnumerable<int>? _masses;
|
2022-10-27 15:42:05 +00:00
|
|
|
|
2022-12-08 19:40:40 +00:00
|
|
|
public override void ProcessInput()
|
2022-10-27 15:42:05 +00:00
|
|
|
{
|
|
|
|
_masses = Input.Select(int.Parse);
|
|
|
|
}
|
|
|
|
|
2022-12-04 04:51:51 +00:00
|
|
|
private static int FuelCost(int weight) => weight / 3 - 2;
|
2022-10-27 15:42:05 +00:00
|
|
|
|
|
|
|
private static int FullCost(int cost)
|
|
|
|
{
|
|
|
|
int total = 0, newCost, tmp = cost;
|
|
|
|
|
|
|
|
while ((newCost = FuelCost(tmp)) >= 0)
|
|
|
|
{
|
|
|
|
total += newCost;
|
|
|
|
tmp = newCost;
|
|
|
|
}
|
|
|
|
|
|
|
|
return total;
|
|
|
|
}
|
|
|
|
|
2022-12-08 19:40:40 +00:00
|
|
|
public override object Part1() => _masses!.Sum(FuelCost);
|
2022-10-27 15:42:05 +00:00
|
|
|
|
2022-12-08 19:40:40 +00:00
|
|
|
public override object Part2() => _masses!.Sum(FullCost);
|
2022-10-27 15:42:05 +00:00
|
|
|
}
|