aoc2020/day5/part2/main.d

27 lines
661 B
D
Raw Permalink Normal View History

2020-12-08 07:26:29 +01:00
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;
}