aoc2020/day9/part1/source/app.d

28 lines
707 B
D

import std;
import mir.combinatorics;
void main()
{
File("input", "r").byLine.map!(to!long)
.array.findFirstNumberNotSumOf2PreviousNumbers(25).writeln;
}
auto findFirstNumberNotSumOf2PreviousNumbers(long[] numbers, size_t windowSize)
in(numbers.length > windowSize)
{
return numbers.slide(windowSize + 1)
.map!(window => tuple(window[0 .. $ - 1].combinations(2).map!sum, window[windowSize]))
.find!(pair => !pair[0].canFind(pair[1]))
.front[1];
}
unittest
{
long[] input = [
35, 20, 15, 25, 47, 40, 62, 55, 65, 95, 102, 117, 150, 182, 127, 219, 299,
277, 309, 576
];
assert(input.findFirstNumberNotSumOf2PreviousNumbers(5) == 127);
}