about summary refs log tree commit diff
path: root/Day1.cs
blob: dd576452b0dbdb8cc956afa274fa13d400c206d9 (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
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace aoc2019
{
    public class Day1 : Day
    {
        public override int DayNumber => 1;

        private readonly IEnumerable<int> masses =
            File.ReadLines("input/day1.in").Select(int.Parse);

        private static int FuelCost(int weight) => 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() => $"{masses.Select(FuelCost).Sum()}";

        public override string Part2() => $"{masses.Select(FullCost).Sum()}";
    }
}