about summary refs log blame commit diff
path: root/aoc2019/Day01.cs
blob: 6ebdce5254811683147626df36e2506bc3a28c77 (plain) (tree)
1
2
3
4
5
6
7
8
9
                                 



                  
                                   
     
                                                 
 
                                                                      


                                             
 




                                               

                                             
                                               









                                                  
                                      


                                             
 
                                      


                                             
     
 
using System.Collections.Generic;
using System.Linq;

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)}";
        }
    }
}