From aa7616140a8b81d18201a658e4986050d67ee2e3 Mon Sep 17 00:00:00 2001 From: Nguyễn Gia Phong Date: Mon, 5 Dec 2022 15:38:01 +0900 Subject: [aoc/2022] Finish day 5 --- aoc/2022/05/part-one.rb | 10 ++++++++++ aoc/2022/05/part-two.rb | 10 ++++++++++ 2 files changed, 20 insertions(+) create mode 100755 aoc/2022/05/part-one.rb create mode 100755 aoc/2022/05/part-two.rb diff --git a/aoc/2022/05/part-one.rb b/aoc/2022/05/part-one.rb new file mode 100755 index 0000000..4e11ce9 --- /dev/null +++ b/aoc/2022/05/part-one.rb @@ -0,0 +1,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..].reverse + stacks[from - 1].slice! -n.. +end +puts (stacks.map { |stack| stack[-1] }).join diff --git a/aoc/2022/05/part-two.rb b/aoc/2022/05/part-two.rb new file mode 100755 index 0000000..1cabf1b --- /dev/null +++ b/aoc/2022/05/part-two.rb @@ -0,0 +1,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 -- cgit 1.4.1