summaryrefslogtreecommitdiff
path: root/05/1.lua
blob: e4eaa6a4d889454fe66605638b149b0857674799 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#!/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 -- end of crates
	for i = 1, #line, 4 do
		local letter = line:sub(i+1,i+1)
		if letter ~= " " then
			local stacknum = math.ceil(i/4)
			table.insert(stacks[stacknum],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)
	for i = 1, amt do
		table.insert(stacks[to],table.remove(stacks[from]))
	end
end
table.insert(stacks,{"\n"})
for _,c in ipairs(stacks) do io.write(c[#c]) end