|
@@ -1,38 +1,27 @@
|
|
|
@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()
|
|
|
+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}
|
|
|
-
|
|
|
- (1..cnt).each do |_|
|
|
|
- box = stacks[from - 1].pop()
|
|
|
- stacks[to - 1].append(box)
|
|
|
- end
|
|
|
+ yield stacks, cnt, from - 1, to - 1
|
|
|
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)
|
|
|
+# Part 1
|
|
|
+run_part do |stacks, cnt, from, to|
|
|
|
+ (1..cnt).each do |_|
|
|
|
+ box = stacks[from].pop()
|
|
|
+ stacks[to].append(box)
|
|
|
end
|
|
|
-
|
|
|
- puts stacks.map{|s| s[-1]}.join()
|
|
|
end
|
|
|
|
|
|
-part1()
|
|
|
-part2()
|
|
|
+# Part 2
|
|
|
+run_part do |stacks, cnt, from, to|
|
|
|
+ boxes = stacks[from].pop(cnt)
|
|
|
+ stacks[to] = stacks[to].concat(boxes)
|
|
|
+end
|