Module:User:Cscott/Advent Of Code/2024/Day 5
Appearance
return (function()
local builders = {}
local function register(name, f)
builders[name] = f
end
register('advent.compat', function() return require [[Module:User:Cscott/compat]] end)
register('llpeg.lpegrex', function() return require [[Module:User:Cscott/lpegrex]] end)
register('util', function(myrequire)
local function read_wiki_input(func)
return function (frame, ...)
if type(frame)=='string' then
frame = { args = { frame, ... } }
end
local title = mw.title.new(frame.args[1])
local source = title:getContent()
if source == nil then
error("Can't find title " .. tostring(title))
end
source = source:gsub("^%s*<syntaxhighlight[^>]*>\n?", "", 1)
source = source:gsub("</syntaxhighlight[^>]*>%s*$", "", 1)
return func(source, frame.args[2], frame.args[3])
end
end
return {
read_wiki_input = read_wiki_input,
}
end)
register('day5', function(myrequire)
--[[
Day 5, first part; advent of code 2024
]]--
local compat = myrequire('advent.compat')
local lpegrex = myrequire('llpeg.lpegrex')
local read_wiki_input = myrequire('util').read_wiki_input
--[[ PARSING ]]--
local patt = lpegrex.compile([[
Puzzle <== nl* {:rule: Rules :} nl+ {:pagelist: PageLists :} nl*
Rules <== (Rule nl)+
Rule <== {:before: Number :} `|` {:after: Number :}
PageLists <== PageList (nl PageList)*
PageList <== Number (`,` Number)*
Number <-- %d+ -> tonumber
nl <- %nl
SKIP <- [ ]*
NAME_SUFFIX <- [_%w]+
]])
local Puzzle = {}
Puzzle.__index = Puzzle
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))
-- turn this into a datastructure
setmetatable(ast, Puzzle)
ast:computeIndex()
return ast
end
--[[
Infrastructure
]]--
function part1(s)
return day5a(parse(s))
end
function part2(s)
return day5b(parse(s))
end
--[[
Part 1
]]--
function Puzzle:computeIndex()
self.before = {}
for i=1,#self.rule do
local r = self.rule[i]
if self.before[r.before] == nil then
self.before[r.before] = {}
end
table.insert(self.before[r.before], r.after)
end
for i=1,#self.pagelist do
local l = self.pagelist[i]
l.ordinal = {}
for j=1,#l do
l.ordinal[l[j]] = j
end
end
end
function Puzzle:checkList(i)
local list = self.pagelist[i]
for i=1,#list do
-- is there a before constraint for this?
local val = list[i]
for _,after in ipairs(self.before[val] or {}) do
local b = list.ordinal[val]
local a = list.ordinal[after]
if a ~= nil and b >= a then
return false
end
end
end
return true
end
function middleList(l)
local mid = 1 + math.floor(#l / 2)
return l[mid]
end
function day5a(puzzle)
local sum = 0
for i=1,#puzzle.pagelist do
if puzzle:checkList(i) then
-- this is in the correct order. find the middle page number
sum = sum + middleList(puzzle.pagelist[i])
end
end
return sum
end
--[[
Part 2!
]]--
function Puzzle:reorder(i)
-- clean up list
local l = {}
for _,v in ipairs(self.pagelist[i]) do
table.insert(l, v)
end
-- sort list
local f = function(a,b)
for _,after in ipairs(self.before[a] or {}) do
if after == b then
return true
end
end
return false
end
table.sort(l, f)
return l
end
function day5b(puzzle)
local sum = 0
for i=1,#puzzle.pagelist do
if not puzzle:checkList(i) then
local newList = puzzle:reorder(i)
-- this is in the correct order. find the middle page number
sum = sum + middleList(newList)
end
end
return sum
end
--[[
Testing
]]--
--[[
io.write("Sum: ", day5a(parse(io.input("day5.example"):read("a"))), "\n")
io.write("Sum: ", day5a(parse(io.input("day5.input"):read("a"))), "\n")
io.write("Sum: ", day5b(parse(io.input("day5.example"):read("a"))), "\n")
io.write("Sum: ", day5b(parse(io.input("day5.input"):read("a"))), "\n")
]]--
return {
part1 = read_wiki_input(part1),
part2 = read_wiki_input(part2),
}
end)
local modules = {}
modules['bit32'] = require('bit32')
modules['string'] = require('string')
modules['strict'] = {}
modules['table'] = require('table')
local function myrequire(name)
if modules[name] == nil then
modules[name] = true
modules[name] = (builders[name])(myrequire)
end
return modules[name]
end
return myrequire('day5')
end)()