about summary refs log tree commit diff
diff options
context:
space:
mode:
authorNguyễn Gia Phong <mcsinyx@disroot.org>2022-12-05 15:38:01 +0900
committerNguyễn Gia Phong <mcsinyx@disroot.org>2022-12-05 15:39:26 +0900
commitaa7616140a8b81d18201a658e4986050d67ee2e3 (patch)
tree72ca9b447b2d43df1390667561c0dce1b0345c40
parent8941f621840f756c1c7dabfedb27f7452c7360e1 (diff)
downloadcp-aa7616140a8b81d18201a658e4986050d67ee2e3.tar.gz
[aoc/2022] Finish day 5
-rwxr-xr-xaoc/2022/05/part-one.rb10
-rwxr-xr-xaoc/2022/05/part-two.rb10
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