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

namespace aoc2019
{
    public class Day1 : Day
    {
        private static readonly IEnumerable<int> lines =
            File.ReadLines("input/day1.in").Select(line => int.Parse(line));

        private static int FuelCost(int weight) => weight / 3 - 2;

        public override void Part1()
        {
            Console.WriteLine(lines.Select(num => FuelCost(num)).Sum());
        }

        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 void Part2()
        {
            Console.WriteLine(lines.Select(cost => FullCost(cost)).Sum());
        }
    }
}