aoc2020/day1/part1/main.d

33 lines
589 B
D
Raw Normal View History

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