diff options
author | Nguyễn Gia Phong <mcsinyx@disroot.org> | 2022-12-05 15:38:01 +0900 |
---|---|---|
committer | Nguyễn Gia Phong <mcsinyx@disroot.org> | 2022-12-05 15:39:26 +0900 |
commit | aa7616140a8b81d18201a658e4986050d67ee2e3 (patch) | |
tree | 72ca9b447b2d43df1390667561c0dce1b0345c40 /aoc | |
parent | 8941f621840f756c1c7dabfedb27f7452c7360e1 (diff) | |
download | cp-aa7616140a8b81d18201a658e4986050d67ee2e3.tar.gz |
[aoc/2022] Finish day 5
Diffstat (limited to 'aoc')
-rwxr-xr-x | aoc/2022/05/part-one.rb | 10 | ||||
-rwxr-xr-x | aoc/2022/05/part-two.rb | 10 |
2 files changed, 20 insertions, 0 deletions
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 |