day 8 part 1

This commit is contained in:
Felix Van der Jeugt 2021-12-08 08:37:36 +01:00
parent 602bc416d8
commit 0416db0542
No known key found for this signature in database
GPG Key ID: 58B209295023754D
2 changed files with 36 additions and 0 deletions

21
day08/part1.lua Executable file
View File

@ -0,0 +1,21 @@
#!/usr/bin/env luajit
require("utils")
function isunique(v)
return #v == 2 or #v == 3 or #v == 4 or #v == 7
end
local entries = {}
local uniques = 0
for line in io.lines(arg[3]) do
local words = split(line, " ")
local patterns = { unpack(words, 1, 10) }
local values = { unpack(words, 12, #words) }
table.insert(entries, {
patterns = patterns,
values = values,
})
uniques = uniques + count(values, isunique)
end
print(uniques)

View File

@ -23,3 +23,18 @@ function foreach(t, f)
t[k] = f(v) t[k] = f(v)
end end
end end
function printtable(t)
for k, v in pairs(t) do
io.write(k..":"..v.." ")
end
io.write("\n")
end
function count(t, pred)
local count = 0
for k, v in pairs(t) do
if pred(v) then count = count + 1 end
end
return count
end