-
Notifications
You must be signed in to change notification settings - Fork 1
/
Program.cs
48 lines (40 loc) · 1.2 KB
/
Program.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 AdventOfCode.Common;
var extrapolatedNumbers = Resources.GetInputFileLines()
.Select(Resources.ParseLongNumbersOut)
.Select(Extrapolate)
.Aggregate((Start: 0L, End: 0L), (acc, v) => (acc.Start + v.Start, acc.End + v.End));
Console.WriteLine($"Part 1: {extrapolatedNumbers.End}");
Console.WriteLine($"Part 2: {extrapolatedNumbers.Start}");
static (long Start, long End) Extrapolate(List<long> numbers)
{
var sequences = new List<List<long>>(numbers.Count - 1)
{
numbers
};
bool allZeros = false;
do
{
allZeros = true;
var last = sequences.Last();
if (last.Count == 1)
{
break;
}
var bottom = new List<long>(last.Count - 1);
for (int i = 1; i < last.Count; i++)
{
var n = last[i] - last[i - 1];
bottom.Add(n);
allZeros &= n == 0;
}
sequences.Add(bottom);
} while (!allZeros);
var start = sequences.Last().First();
var end = sequences.Last().Last();
for (int i = sequences.Count - 1; i >= 0; --i)
{
end = sequences[i].Last() + end;
start = sequences[i].First() - start;
}
return (start, end);
}