about summary refs log tree commit diff
path: root/aoc2019/Day01.cs
blob: aa2bc8e5d3e7b3a47a4c02dc5fcb6f69a5f1c0e5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
namespace aoc2019;

public sealed class Day01 : Day
{
    private readonly IEnumerable<int> masses;

    public Day01() : base(1, "The Tyranny of the Rocket Equation")
    {
        masses = Input.Select(int.Parse);
    }

    private static int FuelCost(int weight)
    {
        return weight / 3 - 2;
    }

    private static int FullCost(int cost)
    {
        int total = 0, newcost, tmp = cost;

        while ((newcost = FuelCost(tmp)) >= 0)
        {
            total += newcost;
            tmp = newcost;
        }

        return total;
    }

    public override string Part1()
    {
        return $"{masses.Sum(FuelCost)}";
    }

    public override string Part2()
    {
        return $"{masses.Sum(FullCost)}";
    }
}