123456789101112131415161718192021222324252627 |
- @input = File.read("5.input").split("\n")
- @instrs = @input.filter{|line| line.include?("move")}.map{|line| line.split(/[a-z ]+/)}
- def run_part
- stacks = @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}
- @instrs.each do |inst|
- (_, cnt, from, to) = inst.map{|i| i.to_i}
- yield stacks, cnt, from - 1, to - 1
- end
- puts stacks.map{|s| s[-1]}.join()
- end
- # Part 1
- run_part do |stacks, cnt, from, to|
- (1..cnt).each do |_|
- box = stacks[from].pop()
- stacks[to].append(box)
- end
- end
- # Part 2
- run_part do |stacks, cnt, from, to|
- boxes = stacks[from].pop(cnt)
- stacks[to] = stacks[to].concat(boxes)
- end
|