aoc2019/Day1.cs

33 lines
804 B
C#
Raw Normal View History

2019-12-01 07:35:17 +00:00
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace aoc2019
{
2019-12-03 03:10:05 +00:00
public class Day1 : Day
2019-12-01 07:35:17 +00:00
{
2019-12-05 06:19:39 +00:00
public override int DayNumber => 1;
2019-12-01 07:35:17 +00:00
2019-12-05 06:44:55 +00:00
private readonly IEnumerable<int> masses =
2019-12-05 06:19:39 +00:00
File.ReadLines("input/day1.in").Select(int.Parse);
2019-12-01 07:35:17 +00:00
2019-12-05 06:19:39 +00:00
private static int FuelCost(int weight) => weight / 3 - 2;
2019-12-01 07:35:17 +00:00
private static int FullCost(int cost)
{
2019-12-03 03:10:05 +00:00
int total = 0, newcost, tmp = cost;
2019-12-01 07:35:17 +00:00
while ((newcost = FuelCost(tmp)) >= 0)
{
total += newcost;
tmp = newcost;
}
return total;
}
2019-12-05 06:19:39 +00:00
public override string Part1() => $"{masses.Select(FuelCost).Sum()}";
public override string Part2() => $"{masses.Select(FullCost).Sum()}";
2019-12-01 07:35:17 +00:00
}
}