return (functionlocal builders = local function register(name, f) builders[name] = fendregister('llpeg.lpegrex', function return require end)
register('day4', function(myrequire)--DAY 4 --
local lpegrex = myrequire('llpeg.lpegrex')
--PARSING --local patt = lpegrex.compile(nl* Card (nl+ Card)* nl* |}Card <
function parse(source) --print(inspect(source)) local ast, errlabel, pos = patt:match(source) if not ast then local lineno, colno, line = lpegrex.calcline(source, pos) local colhelp = string.rep(' ', colno-1)..'^' error('syntax error: '..lineno..':'..colno..': '..errlabel.. '\n'..line..'\n'..colhelp) end --print('Parsed with success!') --print(inspect(ast)) return astend
--PART 1 --
function winning_numbers(card) -- in place sort table.sort(card.have) table.sort(card.winning) local my_winning = local i,j = 1,1 while i <= #card.have and j <= #card.winning do if card.have[i]
function score_card(card) local w = winning_numbers(card) if #w
function sum_points(source) local cards = parse(source) local sum = 0 for _,v in pairs(cards) do sum = sum + score_card(v) end return sumend
--PART 2 --
function naive(source) local cards = parse(source) -- preprocess the winning numbers & create initial to-do list local winning = local todo = for _,v in pairs(cards) do local id = v.id table.insert(todo, id) winning[id] = local next_id = id + 1 for _,_ in pairs(winning_numbers(v)) do table.insert(winning[id], next_id) next_id = next_id + 1 end end -- okay, naively take stuff off the to-do list and process it. local i = 1 while i <= #todo do local id = todo[i] print("Looking at", id) for _,v in pairs(winning[id]) do table.insert(todo, v) end i = i + 1 end --print(inspect(todo)) return #todoend
function smarter(source) local cards = parse(source) -- preprocess the winning numbers & create initial to-do list local winning = local copies = for _,v in pairs(cards) do copies[v.id] = 1 end -- process in order for _,v in pairs(cards) do local id = v.id local next_id = id + 1 for _,_ in pairs(winning_numbers(v)) do -- ignore the actual values copies[next_id] = copies[next_id] + copies[id] next_id = next_id + 1 end end -- okay, sum up all the copies --print(inspect(copies)) local sum = 0 for _,v in pairs(copies) do sum = sum + v end return sumend
--CLI start ]--local source = io.input("day4.input"):read("a")print("Sum:", sum_points(source))print("Total:", smarter(source))--[[ CLI end ]]--
return
end)
local modules = modules['table'] = require('table')modules['string'] = require('string')modules['strict'] = local function myrequire(name) if modules[name]