1
0
Fork 0

2020 day 5

This commit is contained in:
Lucidiot 2020-12-07 02:10:24 +01:00
parent 2a3fe2951b
commit 47a5c60f13
Signed by: lucidiot
GPG Key ID: 3358C1CA6906FB8D
2 changed files with 29 additions and 1 deletions

28
2020/5/day5.lua Normal file
View File

@ -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

View File

@ -10,7 +10,7 @@ My solutions to the Advent of Code puzzles.
2 ██ ██ ██ ██ ██
3 ██ ██ ██ ██ ██
4 ██ ██ ██ ██
5 ██ ██ ██
5 ██ ██ ██ ██
6 ██ ██
7 ██ ██
8 ██ ██