27 lines
661 B
D
27 lines
661 B
D
|
import std;
|
||
|
|
||
|
void main()
|
||
|
{
|
||
|
File("input", "r").byLine.findMissingSeatID.writeln;
|
||
|
}
|
||
|
|
||
|
auto calculateSeatID(const char[] seat)
|
||
|
{
|
||
|
return seat.retro.enumerate.map!(it => "BR".canFind(it.value) ? 2 ^^ it.index : 0).sum;
|
||
|
}
|
||
|
|
||
|
unittest
|
||
|
{
|
||
|
assert("FBFBBFFRLR".calculateSeatID == 357);
|
||
|
assert("BFFFBBFRRR".calculateSeatID == 567);
|
||
|
assert("FFFBBBFRRR".calculateSeatID == 119);
|
||
|
assert("BBFFBBFRLL".calculateSeatID == 820);
|
||
|
}
|
||
|
|
||
|
auto findMissingSeatID(T)(T seats)
|
||
|
{
|
||
|
auto sortedSeatIDs = seats.map!calculateSeatID.array.sort;
|
||
|
return sortedSeatIDs.zip(sortedSeatIDs.drop(1))
|
||
|
.find!(seatPair => seatPair[1] - seatPair[0] != 1).front[0] + 1;
|
||
|
}
|