~aperezdc/aoc2022

bb0bbac471d29212f108902b348cb3e023fb6524 — Adrian Perez de Castro 2 years ago 78faead
Day 5, refactor parser
3 files changed, 55 insertions(+), 80 deletions(-)

M day05/day05a.lua
M day05/day05b.lua
A day05/parse.lua
M day05/day05a.lua => day05/day05a.lua +2 -40
@@ 6,45 6,7 @@
-- Distributed under terms of the MIT license.
--

-- Contains the lines of input describing the initial contents of the
-- stacks, to be parsed *after* knowing how many stacks are there.
local stacks_lines = {}

while true do
   local line = io.read()
   if line == "" then
      break
   end

   stacks_lines[#stacks_lines+1] = line
end

-- Last read line contains the numbered stacks, the last number will be
-- the same as the amount of stacks being handled. Also remove that last
-- line from the pending-to-parse input.
local nstacks = 0
for n in stacks_lines[#stacks_lines]:gmatch("%s*(%d+)%s*") do
   nstacks = tonumber(n)
end
table.remove(stacks_lines)

-- Table where each element is a stack, initially empty.
local stacks = {}
for i = 1, nstacks do
   stacks[i] = {}
end

-- Parse the stack contents lines backwards, so we get to pile up the
-- items from the bottom upwards. We can iterate over each found "["
-- character, and from their position in the line determine in which
-- stack they are: positions for the opening bracket are always multiples
-- of four.
for i = #stacks_lines, 1, -1 do
   for bracketpos, label in stacks_lines[i]:gmatch("()%[(%a+)%]") do
      local stacknum = (bracketpos // 4) + 1
      table.insert(stacks[stacknum], label)
   end
end
local stacks = require "parse" ()

for line in io.lines() do
   local _, _, n, from, to = line:find("move (%d+) from (%d+) to (%d+)")


@@ 55,7 17,7 @@ for line in io.lines() do
end

local result = {}
for i = 1, nstacks do
for i = 1, #stacks do
   local thisstack = stacks[i]
   if #thisstack > 0 then
      result[#result+1] = thisstack[#thisstack]

M day05/day05b.lua => day05/day05b.lua +2 -40
@@ 6,45 6,7 @@
-- Distributed under terms of the MIT license.
--

-- Contains the lines of input describing the initial contents of the
-- stacks, to be parsed *after* knowing how many stacks are there.
local stacks_lines = {}

while true do
   local line = io.read()
   if line == "" then
      break
   end

   stacks_lines[#stacks_lines+1] = line
end

-- Last read line contains the numbered stacks, the last number will be
-- the same as the amount of stacks being handled. Also remove that last
-- line from the pending-to-parse input.
local nstacks = 0
for n in stacks_lines[#stacks_lines]:gmatch("%s*(%d+)%s*") do
   nstacks = tonumber(n)
end
table.remove(stacks_lines)

-- Table where each element is a stack, initially empty.
local stacks = {}
for i = 1, nstacks do
   stacks[i] = {}
end

-- Parse the stack contents lines backwards, so we get to pile up the
-- items from the bottom upwards. We can iterate over each found "["
-- character, and from their position in the line determine in which
-- stack they are: positions for the opening bracket are always multiples
-- of four.
for i = #stacks_lines, 1, -1 do
   for bracketpos, label in stacks_lines[i]:gmatch("()%[(%a+)%]") do
      local stacknum = (bracketpos // 4) + 1
      table.insert(stacks[stacknum], label)
   end
end
local stacks = require "parse" ()

for line in io.lines() do
   local _, _, n, from, to = line:find("move (%d+) from (%d+) to (%d+)")


@@ 62,7 24,7 @@ for line in io.lines() do
end

local result = {}
for i = 1, nstacks do
for i = 1, #stacks do
   local thisstack = stacks[i]
   if #thisstack > 0 then
      result[#result+1] = thisstack[#thisstack]

A day05/parse.lua => day05/parse.lua +51 -0
@@ 0,0 1,51 @@
#! /usr/bin/env lua
--
-- common.lua
-- Copyright (C) 2022 Adrian Perez de Castro <aperez@igalia.com>
--
-- Distributed under terms of the MIT license.
--

return function ()
   -- Contains the lines of input describing the initial contents of the
   -- stacks, to be parsed *after* knowing how many stacks are there.
   local stacks_lines = {}

   while true do
      local line = io.read()
      if line == "" then
         break
      end

      stacks_lines[#stacks_lines+1] = line
   end

   -- Last read line contains the numbered stacks, the last number will be
   -- the same as the amount of stacks being handled. Also remove that last
   -- line from the pending-to-parse input.
   local nstacks = 0
   for n in stacks_lines[#stacks_lines]:gmatch("%s*(%d+)%s*") do
      nstacks = tonumber(n)
   end
   table.remove(stacks_lines)

   -- Table where each element is a stack, initially empty.
   local stacks = {}
   for i = 1, nstacks do
      stacks[i] = {}
   end

   -- Parse the stack contents lines backwards, so we get to pile up the
   -- items from the bottom upwards. We can iterate over each found "["
   -- character, and from their position in the line determine in which
   -- stack they are: positions for the opening bracket are always multiples
   -- of four.
   for i = #stacks_lines, 1, -1 do
      for bracketpos, label in stacks_lines[i]:gmatch("()%[(%a+)%]") do
         local stacknum = (bracketpos // 4) + 1
         table.insert(stacks[stacknum], label)
      end
   end

   return stacks
end