tadd day 5 part 2 - aoc22 - advent of code 2022 solutions | |
git clone git://src.adamsgaard.dk/aoc22 | |
Log | |
Files | |
Refs | |
README | |
--- | |
commit ffafc777abe869365b6e3ee8f0d43ef7ca89ac06 | |
parent 713caeb982b2db352653cc09b41261cdc20810d9 | |
Author: Anders Damsgaard <[email protected]> | |
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(-) | |
--- | |
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)) | |
+} | |
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 |