aoc2020/day10/part1/main.d

40 lines
990 B
D

import std;
void main()
{
File("input", "r").byLine.map!(to!int)
.array.calculateJoltDifferences.multiply1And3Differences.writeln;
}
auto multiply1And3Differences(int[int] differences)
{
return differences[1] * differences[3];
}
int[int] calculateJoltDifferences(int[] input)
{
input.sort();
int[int] differenceCounts;
chain([0], input, [input.back + 3]).slide(2).map!(pair => pair[1] - pair[0])
.each!(difference => differenceCounts[difference]++);
return differenceCounts;
}
unittest
{
auto input = [16, 10, 15, 5, 1, 11, 7, 19, 6, 12, 4];
auto differences = input.calculateJoltDifferences;
assert(differences[1] == 7 && differences[3] == 5);
}
unittest
{
auto input = [
28, 33, 18, 42, 31, 14, 46, 20, 48, 47, 24, 23, 49, 45, 19, 38, 39, 11,
1, 32, 25, 35, 8, 17, 7, 9, 4, 2, 34, 10, 3
];
auto differences = input.calculateJoltDifferences;
assert(differences[1] == 22 && differences[3] == 10);
}