blob: d582dce5b36cd7658ec27bcf2075d0b5f0942c0d (
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
|
lines = $stdin.readlines
brk = lines.index(?\n)
stackin = lines[0..(brk-2)]
movein = lines[(brk+1)..-1]
numstacks = lines[brk-1].split[-1].to_i
stacks = Array.new(numstacks) { [] }
stackin.reverse.each do |rank|
stack = 0
until rank.empty? do
stacks[stack] << rank[1] if rank[0] == ?[
rank = rank[4..-1]
stack += 1
end
end
movein.each do |istr|
n, f, t = istr.scan(/\Amove (\d+) from (\d+) to (\d+)\Z/)[0].map(&:to_i)
f -= 1
t -= 1
n.times do
stacks[t].push(stacks[f].pop)
end
end
puts stacks.map(&:pop).join
|