@input = File.read("5.input").split("\n") def stacks() return @input.filter {|line| line.include?("[")}.map{|line| line.chars.each_slice(4).to_a.map{|block| block[1]}}.transpose.map{|stack| stack.filter{|box| box != " "}.reverse} end @instrs = @input.filter{|line| line.include?("move")}.map{|line| line.split(/[a-z ]+/)} def part1() stacks = stacks() @instrs.each do |inst| (_, cnt, from, to) = inst.map{|i| i.to_i} (1..cnt).each do |_| box = stacks[from - 1].pop() stacks[to - 1].append(box) end end puts stacks.map{|s| s[-1]}.join() end def part2() stacks = stacks() @instrs.each do |inst| (_, cnt, from, to) = inst.map{|i| i.to_i} boxes = stacks[from - 1].pop(cnt) stacks[to - 1] = stacks[to - 1].concat(boxes) end puts stacks.map{|s| s[-1]}.join() end part1() part2()