aoc2019/Day1.cs

45 lines
917 B
C#
Raw Normal View History

2019-12-01 07:35:17 +00:00
using System.Collections.Generic;
using System.Linq;
namespace aoc2019
{
2020-12-02 04:50:35 +00:00
internal sealed class Day1 : Day
2019-12-01 07:35:17 +00:00
{
private readonly IEnumerable<int> masses;
public Day1()
{
masses = Input.Select(int.Parse);
}
2019-12-01 07:35:17 +00:00
public override int DayNumber => 1;
private static int FuelCost(int weight)
{
return 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;
}
2020-12-02 06:58:54 +00:00
protected override string Part1()
{
return $"{masses.Sum(FuelCost)}";
}
2019-12-05 06:19:39 +00:00
2020-12-02 06:58:54 +00:00
protected override string Part2()
{
return $"{masses.Sum(FullCost)}";
}
2019-12-01 07:35:17 +00:00
}
}