aoc2020/day1/part2/main.d

40 lines
802 B
D
Raw Permalink Normal View History

2020-12-01 10:43:51 +01:00
import std;
void main()
{
File("input", "r").byLine.map!(to!int).array.multiply3EntriesThatSumTo(2020).writeln;
}
int multiply3EntriesThatSumTo(int[] input, int sum)
{
foreach (key1; input)
{
foreach (key2; input)
{
if (key2 == key1)
{
continue;
}
foreach (key3; input)
{
if (key3 == key1 || key3 == key2)
{
continue;
}
if (key1 + key2 + key3 == sum)
{
return key1 * key2 * key3;
}
}
}
}
return 0;
}
unittest
{
auto input = [1721, 979, 366, 299, 675, 1456];
assert(input.multiply3EntriesThatSumTo(2020) == 241_861_950);
}