29 lines
675 B
D
Executable file
29 lines
675 B
D
Executable file
#!/usr/bin/env dub
|
|
/+ dub.sdl:
|
|
name "main"
|
|
dependency "mir-algorithm" version="~>3.10.12"
|
|
+/
|
|
|
|
import std;
|
|
import mir.combinatorics;
|
|
|
|
void main()
|
|
{
|
|
File("input", "r").byLine.map!(to!int).array.multiplyNEntriesThatSumTo(2, 2020).writeln;
|
|
}
|
|
|
|
alias product = partial!(reverseArgs!(fold!((a, b) => a * b)), 1);
|
|
|
|
int multiplyNEntriesThatSumTo(int[] input, int n, int requiredSum)
|
|
{
|
|
return input.combinations(n).filter!(combination => combination.sum == requiredSum)
|
|
.map!(combination => combination.product)
|
|
.front;
|
|
}
|
|
|
|
unittest
|
|
{
|
|
auto input = [1721, 979, 366, 299, 675, 1456];
|
|
|
|
assert(input.multiplyNEntriesThatSumTo(2, 2020) == 514_579);
|
|
}
|