2020-12-01 12:51:39 +01:00
|
|
|
#!/usr/bin/env dub
|
|
|
|
/+ dub.sdl:
|
2020-12-01 13:03:13 +01:00
|
|
|
name "main"
|
2020-12-01 12:51:39 +01:00
|
|
|
dependency "mir-algorithm" version="~>3.10.12"
|
|
|
|
+/
|
|
|
|
|
|
|
|
import std;
|
|
|
|
import mir.combinatorics;
|
|
|
|
|
|
|
|
void main()
|
|
|
|
{
|
|
|
|
File("input", "r").byLine.map!(to!int).array.multiplyNEntriesThatSumTo(3, 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(3, 2020) == 241_861_950);
|
|
|
|
}
|