-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day01Solver.cs
48 lines (41 loc) · 967 Bytes
/
Day01Solver.cs
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
40
41
42
43
44
45
46
47
48
using System;
using System.IO;
using System.Linq;
using AdventOfCode.Abstractions;
namespace AdventOfCode.Year2019.Day01;
public class Day01Solver : DaySolver
{
private readonly int[] _inputNumbers;
public Day01Solver(string inputFilePath) : base(inputFilePath)
{
_inputNumbers = InputLines
.Where(s => !string.IsNullOrWhiteSpace(s))
.Select(s => int.Parse(s))
.ToArray();
}
private int GetFuel(int mass)
{
return Math.Max((mass / 3) - 2, 0);
}
private int GetWholeFuel(int mass)
{
int result = 0;
int additionalMass = GetFuel(mass);
while (additionalMass > 0)
{
result += additionalMass;
additionalMass = GetFuel(additionalMass);
}
return result;
}
public override string SolvePart1()
{
int result = _inputNumbers.Select(n => GetFuel(n)).Sum();
return result.ToString();
}
public override string SolvePart2()
{
int result = _inputNumbers.Select(n => GetWholeFuel(n)).Sum();
return result.ToString();
}
}