tadd day 5 part 2 - aoc22 - advent of code 2022 solutions
(HTM) git clone git://src.adamsgaard.dk/aoc22
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) README
---
(DIR) commit ffafc777abe869365b6e3ee8f0d43ef7ca89ac06
(DIR) parent 713caeb982b2db352653cc09b41261cdc20810d9
(HTM) Author: Anders Damsgaard <anders@adamsgaard.dk>
Date: Mon, 5 Dec 2022 21:05:56 +0000
add day 5 part 2
Diffstat:
A 5/aoc5b | 43 ++++++++++++++++++++++++++++++
M 5/mkfile | 5 +++++
2 files changed, 48 insertions(+), 0 deletions(-)
---
(DIR) diff --git a/5/aoc5b b/5/aoc5b
t@@ -0,0 +1,43 @@
+#!/bin/awk -f
+
+function addstart(i, char) {
+ pile[i] = sprintf("%s%s", char, pile[i])
+}
+
+function append(i, char) {
+ pile[i] = sprintf("%s%s", pile[i], char)
+}
+
+function pop(i, n) {
+ len = length(pile[i])
+ if (len && len - n >= 0) {
+ char = substr(pile[i], len - (n-1), len+1)
+ pile[i] = substr(pile[i], 1, len - n)
+ return char
+ }
+}
+
+NR == 1 { n = (length+1)/4 }
+
+$1 ~ /1/ { move=1; getline }
+
+move == 0 {
+ for (i=1; i<=n; i++) {
+ field = substr($0, (i-1)*4 + 2, 1)
+ if (sub(/ /, "", field) == 0)
+ addstart(i, field)
+ }
+}
+
+move == 1 {
+ nmove = $2
+ from = $4
+ to = $6
+ append(to, pop(from, nmove))
+}
+
+END{
+ for (i=1; i<=n; i++)
+ printf("%s", substr(pile[i], length(pile[i]), 1))
+ print
+}
(DIR) diff --git a/5/mkfile b/5/mkfile
t@@ -1,5 +1,6 @@
run:V:
./aoc5 <input
+ ./aoc5b <input
test:V:
# correct result: CMZ
t@@ -9,3 +10,7 @@ test:V:
# [C] [M] [P]
# 1 2 3
./aoc5 <testinput
+
+test2:V:
+ # correct result: MCD
+ ./aoc5b <testinput