2020-12-05 11:16:58 +00:00
|
|
|
import std.algorithm;
|
|
|
|
import std.array;
|
|
|
|
import std.conv;
|
|
|
|
import std.range;
|
|
|
|
import std.stdio;
|
|
|
|
import std.string;
|
|
|
|
import std.variant;
|
|
|
|
|
|
|
|
import dayutil;
|
|
|
|
|
2020-12-07 05:05:35 +00:00
|
|
|
Variant run(int part, File file, bool bigboy, string[] args) {
|
2020-12-05 11:16:58 +00:00
|
|
|
auto lines = file.byLine;
|
2020-12-05 11:49:58 +00:00
|
|
|
auto seats = lines.map!(x => determineSeat(to!string(x.array)));
|
2020-12-05 11:16:58 +00:00
|
|
|
Variant result = parts!int(part,
|
2020-12-05 11:49:58 +00:00
|
|
|
() => part1(seats),
|
|
|
|
() => part2(seats.array));
|
2020-12-05 11:16:58 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
int part1(Range)(Range range) if (isInputRange!Range) {
|
2020-12-05 11:49:58 +00:00
|
|
|
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;
|
2020-12-05 11:16:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int determineSeat(string code) {
|
|
|
|
int result = 0;
|
|
|
|
int marker = 0b1000000000;
|
|
|
|
foreach(char c; code[0..7]) {
|
|
|
|
if (c == 'B') result |= marker;
|
|
|
|
marker >>= 1;
|
|
|
|
}
|
|
|
|
//writeln("ROW: ", result >> 3);
|
|
|
|
foreach(char c; code[7..10]) {
|
|
|
|
if (c == 'R') result |= marker;
|
|
|
|
marker >>= 1;
|
|
|
|
}
|
|
|
|
//writeln("COL: ", result & 0x7);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
unittest {
|
|
|
|
assert(determineSeat("BFFFBBFRRR") == 567);
|
|
|
|
assert(determineSeat("FFFBBBFRRR") == 119);
|
|
|
|
assert(determineSeat("BBFFBBFRLL") == 820);
|
|
|
|
}
|