aboutsummaryrefslogtreecommitdiff
path: root/day05/part1.rb
diff options
context:
space:
mode:
authorNat Lasseter <nat.lasseter@york.ac.uk>2022-12-06 11:52:09 +0000
committerNat Lasseter <nat.lasseter@york.ac.uk>2022-12-06 11:52:09 +0000
commitd2c32dcd6889a83a6327304e89cabcbbdad253fb (patch)
treef5d25a8074517704ee3738bc28147a8e8a87ccbe /day05/part1.rb
parent8079694bd5ab49d1a75247185ff2f6cea9f370c7 (diff)
Day 05
Diffstat (limited to 'day05/part1.rb')
-rw-r--r--day05/part1.rb28
1 files changed, 28 insertions, 0 deletions
diff --git a/day05/part1.rb b/day05/part1.rb
new file mode 100644
index 0000000..d582dce
--- /dev/null
+++ b/day05/part1.rb
@@ -0,0 +1,28 @@
+lines = $stdin.readlines
+
+brk = lines.index(?\n)
+stackin = lines[0..(brk-2)]
+movein = lines[(brk+1)..-1]
+
+numstacks = lines[brk-1].split[-1].to_i
+stacks = Array.new(numstacks) { [] }
+
+stackin.reverse.each do |rank|
+ stack = 0
+ until rank.empty? do
+ stacks[stack] << rank[1] if rank[0] == ?[
+ rank = rank[4..-1]
+ stack += 1
+ end
+end
+
+movein.each do |istr|
+ n, f, t = istr.scan(/\Amove (\d+) from (\d+) to (\d+)\Z/)[0].map(&:to_i)
+ f -= 1
+ t -= 1
+ n.times do
+ stacks[t].push(stacks[f].pop)
+ end
+end
+
+puts stacks.map(&:pop).join