about summary refs log tree commit diff
diff options
context:
space:
mode:
-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