From 3555be54c2abb8d5ece008a60dbdfbde0ffbddd7 Mon Sep 17 00:00:00 2001 From: Alex Pickering Date: Fri, 7 Feb 2025 12:49:48 -0600 Subject: inital commit --- 05/2.lua | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 05/2.lua (limited to '05/2.lua') diff --git a/05/2.lua b/05/2.lua new file mode 100644 index 0000000..a132630 --- /dev/null +++ b/05/2.lua @@ -0,0 +1,38 @@ +#!/usr/bin/env lua + +local stacks +for line in io.lines() do + if not stacks then -- first line + stacks = {} + local num_stacks = math.ceil(#line / 4) + for i = 1, num_stacks do + stacks[i] = {} + end + end + if line:sub(2,2) == "1" then + break -- end of crates + end + for i = 1, #line, 4 do + local letter = line:sub(i+1,i+1) + if letter ~= " " then + table.insert(stacks[math.ceil(i/4)],1,letter) + end + end +end +for line in io.lines() do break end -- empty line between crates and orders +for line in io.lines() do + local amt, from, to = line:match("move (%d+) from (%d+) to (%d+)") + amt, from, to = tonumber(amt), tonumber(from), tonumber(to) + local crates = {} + for i = 1, amt do + table.insert(crates, table.remove(stacks[from])) + end + for i = 1, #crates do + table.insert(stacks[to], table.remove(crates)) + end +end +local msg = {} +for _,c in pairs(stacks) do + table.insert(msg,c[#c]) +end +print(table.concat(msg)) -- cgit v1.2.3-70-g09d2