Module:User:Cscott/Advent Of Code 2023/Day 2 Explained

return (functionlocal builders = local function register(name, f) builders[name] = fendregister('llpeg.lpegrex', function return require end)

register('day2', function(myrequire)--DAY 2 --

local lpegrex = myrequire('llpeg.lpegrex')

--PARSING --

local patt = lpegrex.compile

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 find(limit, color) for j = 1, #limit do local draw = limit[j] if draw.color

color then return draw end end return nilend

function possible(game, limit) for i = 1, #game.turns do local turn = game.turns[i] for j = 1, #turn do local draw = turn[j] local match = find(limit, draw.color) if match

nil then return false -- impossible, no matching color elseif match.count < draw.count then return false -- impossible, not enough of this color end end end return true -- this game is possibleend

function sum_possible(source, limit) local games = parse(source) -- print(#games, "games\n")

local limit = parse(limit)[1].turns[1] --print(inspect(limit)) local sum = 0 for i = 1, #games do local game = games[i] if possible(game, limit) then -- print("Game " .. game.id .. " is possible!") sum = sum + game.id end end -- print("Sum", sum) return sumend

--PART 2 --

function min_cubes(game) local red, green, blue = 0,0,0 for i = 1, #game.turns do local turn = game.turns[i] for j = 1, #turn do local draw = turn[j] if draw.color

'red' then red = math.max(red, draw.count) elseif draw.color

'green' then green = math.max(green, draw.count) elseif draw.color

'blue' then blue = math.max(blue, draw.count) else error("what color is this?") end end end return red, green, blueend

function power(game) local red, green, blue = min_cubes(game) return red * green * blueend

function sum_powers(source) local games = parse(source) -- print(#games, "games\n")

local sum = 0 for i = 1, #games do local game = games[i] sum = sum + power(game) end -- print("Sum", sum) return sumend

function part1(source, limit) return sum_possible(source, limit)end

function part2(source) return sum_powers(source)end

--CLI start ]---- local source = "Game 1: 1 blue\nGame 2: 2 red"--local source = io.input("day2.example"):read("a")--local limit = "Game 0: 12 red, 13 green, 14 blue"

local source = io.input("day2.input"):read("*a")local limit = "Game 0: 12 red, 13 green, 14 blue"

print("Sum", sum_possible(source, limit))print("Power", sum_powers(source))--[[ CLI end ]]--

return

end)

local modules = modules['table'] = require('table')modules['string'] = require('string')modules['strict'] = local function myrequire(name) if modules[name]