--- Day 5: Binary Boarding ---
parsing is straightforward, considering that encoding is basically binary
type
Seat = int16
proc parseSeat(s: string): Seat =
discard parseBin[int16](s.multiReplace(
[("F", "0"), ("B", "1"), ("R", "1"), ("L", "0")]), result)
chk parseSeat("FBFBBFFRLR"), 357
[OK] parseSeat("FBFBBFFRLR") = 357
if I want to get back row and col (I will use it for visualization at the end)
proc row(x: Seat): int =
x shr 3
proc col(x: Seat): int =
result = x.int
result.mask(0b00000000000000000000000000000111)
chk parseSeat("FBFBBFFRLR").row, 44
chk parseSeat("FBFBBFFRLR").col, 5
[OK] parseSeat("FBFBBFFRLR").row = 44 [OK] parseSeat("FBFBBFFRLR").col = 5
part1 is solved during parsing input file
var
maxSeat = 0
seats: seq[Seat]
for line in "2020/input05.txt".lines:
seats.add parseSeat(line)
if seats[^1] > maxSeat:
maxSeat = seats[^1]
echo "part1: ", maxSeat
chk(maxSeat, 947)
part1: 947 [OK] maxSeat = 947
That's the right answer! You are one gold star closer to saving your vacation.
part 2 is solved by sorting and finding the gap
var mySeat: Seat
seats.sort()
for x in 1 ..< seats.len:
if seats[x] != seats[x - 1] + 1:
mySeat = seats[x] - 1 ## first try I forgot the "- 1!
break
echo "part2: ", mySeat
chk(maxSeat, 636)
part2: 636 [KO] maxSeat = 947; expected = 636
That's the right answer! You are one gold star closer to saving your vacation.
what do we need to visualize the plane? I will convert Seat in (row, col)
type
RowCol = tuple[row, col: int]
proc toPasses(s: seq[Seat]): HashSet[RowCol] =
for x in s:
result.incl (x.row, x.col).RowCol
proc showAndTell(s: HashSet[RowCol]): RowCol =
var text = "____01234567\n"
for row in 0 .. 127:
text.add &"{row:03} "
for col in 0 .. 7:
if (row, col) in s:
text.add "X"
elif row < 30 or row > 100:
text.add "."
else:
text.add "<em>O</em>"
result = (row, col)
text.add "\n"
text.add "\n"
echo text
let myRowCol = seats.toPasses.showAndTell
____01234567 000 ........ 001 .......X 002 XXXXXXXX 003 XXXXXXXX 004 XXXXXXXX 005 XXXXXXXX 006 XXXXXXXX 007 XXXXXXXX 008 XXXXXXXX 009 XXXXXXXX 010 XXXXXXXX 011 XXXXXXXX 012 XXXXXXXX 013 XXXXXXXX 014 XXXXXXXX 015 XXXXXXXX 016 XXXXXXXX 017 XXXXXXXX 018 XXXXXXXX 019 XXXXXXXX 020 XXXXXXXX 021 XXXXXXXX 022 XXXXXXXX 023 XXXXXXXX 024 XXXXXXXX 025 XXXXXXXX 026 XXXXXXXX 027 XXXXXXXX 028 XXXXXXXX 029 XXXXXXXX 030 XXXXXXXX 031 XXXXXXXX 032 XXXXXXXX 033 XXXXXXXX 034 XXXXXXXX 035 XXXXXXXX 036 XXXXXXXX 037 XXXXXXXX 038 XXXXXXXX 039 XXXXXXXX 040 XXXXXXXX 041 XXXXXXXX 042 XXXXXXXX 043 XXXXXXXX 044 XXXXXXXX 045 XXXXXXXX 046 XXXXXXXX 047 XXXXXXXX 048 XXXXXXXX 049 XXXXXXXX 050 XXXXXXXX 051 XXXXXXXX 052 XXXXXXXX 053 XXXXXXXX 054 XXXXXXXX 055 XXXXXXXX 056 XXXXXXXX 057 XXXXXXXX 058 XXXXXXXX 059 XXXXXXXX 060 XXXXXXXX 061 XXXXXXXX 062 XXXXXXXX 063 XXXXXXXX 064 XXXXXXXX 065 XXXXXXXX 066 XXXXXXXX 067 XXXXXXXX 068 XXXXXXXX 069 XXXXXXXX 070 XXXXXXXX 071 XXXXXXXX 072 XXXXXXXX 073 XXXXXXXX 074 XXXXXXXX 075 XXXXXXXX 076 XXXXXXXX 077 XXXXXXXX 078 XXXXXXXX 079 XXXXOXXX 080 XXXXXXXX 081 XXXXXXXX 082 XXXXXXXX 083 XXXXXXXX 084 XXXXXXXX 085 XXXXXXXX 086 XXXXXXXX 087 XXXXXXXX 088 XXXXXXXX 089 XXXXXXXX 090 XXXXXXXX 091 XXXXXXXX 092 XXXXXXXX 093 XXXXXXXX 094 XXXXXXXX 095 XXXXXXXX 096 XXXXXXXX 097 XXXXXXXX 098 XXXXXXXX 099 XXXXXXXX 100 XXXXXXXX 101 XXXXXXXX 102 XXXXXXXX 103 XXXXXXXX 104 XXXXXXXX 105 XXXXXXXX 106 XXXXXXXX 107 XXXXXXXX 108 XXXXXXXX 109 XXXXXXXX 110 XXXXXXXX 111 XXXXXXXX 112 XXXXXXXX 113 XXXXXXXX 114 XXXXXXXX 115 XXXXXXXX 116 XXXXXXXX 117 XXXXXXXX 118 XXXX.... 119 ........ 120 ........ 121 ........ 122 ........ 123 ........ 124 ........ 125 ........ 126 ........ 127 ........
I can check back id
proc id(x: RowCol): int =
x.col + 8 * x.row
chk id(myRowCol), mySeat
[OK] id(myRowCol) = 636