~aperezdc/aoc2022

276fc7fd16b24a49156054b239468e336ddcef80 — Adrian Perez de Castro 2 years ago 213adc5
Day 5, first part
2 files changed, 74 insertions(+), 0 deletions(-)

A day05/day05.lua
A day05/day05.test
A day05/day05.lua => day05/day05.lua +65 -0
@@ 0,0 1,65 @@
#! /usr/bin/env lua
--
-- day05.lua
-- Copyright (C) 2022 Adrian Perez de Castro <aperez@igalia.com>
--
-- 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

for line in io.lines() do
   local _, _, n, from, to = line:find("move (%d+) from (%d+) to (%d+)")
   for _ = 1, n do
      from, to = tonumber(from), tonumber(to)
      table.insert(stacks[to], table.remove(stacks[from]))
   end
end

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

print(table.concat(result, ""))

A day05/day05.test => day05/day05.test +9 -0
@@ 0,0 1,9 @@
    [D]    
[N] [C]    
[Z] [M] [P]
 1   2   3 

move 1 from 2 to 1
move 3 from 1 to 3
move 2 from 2 to 1
move 1 from 1 to 2