day 8 part 1
This commit is contained in:
parent
602bc416d8
commit
0416db0542
21
day08/part1.lua
Executable file
21
day08/part1.lua
Executable 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)
|
15
utils.lua
15
utils.lua
@ -23,3 +23,18 @@ function foreach(t, f)
|
||||
t[k] = f(v)
|
||||
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
|
||||
|
Loading…
Reference in New Issue
Block a user