1
0
Fork 0
mirror of https://github.com/HenkKalkwater/aoc-2020 synced 2024-11-22 11:05:18 +00:00

Add day 5, part 2

This commit is contained in:
Chris Josten 2020-12-05 12:49:58 +01:00
parent 73aac14025
commit 8ca791d3d0
Signed by: chris
GPG key ID: 1795A594046530AB

View file

@ -10,13 +10,27 @@ import dayutil;
Variant run(int part, File file, string[] args) { Variant run(int part, File file, string[] args) {
auto lines = file.byLine; auto lines = file.byLine;
auto seats = lines.map!(x => determineSeat(to!string(x.array)));
Variant result = parts!int(part, Variant result = parts!int(part,
() => part1(lines)); () => part1(seats),
() => part2(seats.array));
return result; return result;
} }
int part1(Range)(Range range) if (isInputRange!Range) { int part1(Range)(Range range) if (isInputRange!Range) {
return range.map!(x => determineSeat(to!string(x.array))).maxElement; return range.maxElement;
}
int part2(Range)(Range range) if (isRandomAccessRange!Range && is(ElementType!Range == int)) {
auto window = range.sort[8..$-8].slide(2);
int missing = -1;
foreach(seat; window) {
if (seat[0] + 1 != seat[1]) {
missing = seat[0] + 1;
break;
}
}
return missing;
} }
int determineSeat(string code) { int determineSeat(string code) {