about summary refs log tree commit diff
path: root/aoc/2022/05/part-two.rb
blob: 1cabf1bfaac4931fdf365d5cc13f55caa083a8ef (plain) (blame)
1
2
3
4
5
6
7
8
9
10
#!/usr/bin/env ruby
drawing, procedure = ARGF.read.split /\n.*\n\n/
first, *rest = (drawing.split "\n").map { |line| (line.scan /.(.). ?/) }
stacks = (first.zip *rest).map { |stack| stack.join.strip.reverse }
for line in procedure.split "\n"
  n, from, to = (line.scan /\d+/).map &:to_i
  stacks[to - 1] << stacks[from - 1][-n..]
  stacks[from - 1].slice! -n..
end
puts (stacks.map { |stack| stack[-1] }).join