2020 day 5
This commit is contained in:
parent
2a3fe2951b
commit
47a5c60f13
|
@ -0,0 +1,28 @@
|
|||
#!/usr/bin/env lua
|
||||
local seat_ids = {}
|
||||
|
||||
local function parse(line)
|
||||
local word = tonumber(line:gsub("[FBLR]", {F="0", B="1", L="0", R="1"}), 2)
|
||||
local row, column = word >> 3, word & 7
|
||||
return row * 8 + column
|
||||
end
|
||||
|
||||
for line in io.lines() do
|
||||
table.insert(seat_ids, parse(line))
|
||||
end
|
||||
|
||||
local lowest_id, highest_id = math.min(table.unpack(seat_ids)), math.max(table.unpack(seat_ids))
|
||||
print(highest_id)
|
||||
|
||||
-- Build an equivalent of a set for faster access
|
||||
local seat_ids_set = {}
|
||||
for _, id in ipairs(seat_ids) do
|
||||
seat_ids_set[id] = true
|
||||
end
|
||||
|
||||
for id = lowest_id, highest_id do
|
||||
if not seat_ids_set[id] and seat_ids_set[id - 1] and seat_ids_set[id + 1] then
|
||||
print(id)
|
||||
return
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue