aboutsummaryrefslogtreecommitdiff
path: root/2022/05/puzzles.awk
diff options
context:
space:
mode:
authorThomas Voss <mail@thomasvoss.com> 2022-12-05 17:00:09 +0100
committerThomas Voss <mail@thomasvoss.com> 2022-12-05 17:00:09 +0100
commit5da8c3550eada8ec36b53d87e557cbc113a23faa (patch)
tree535c695b8e1750fd470da7cbe5b79b57935dcc79 /2022/05/puzzles.awk
parent30fb572a31f7a71b7111a9e2645423ee49a09baa (diff)
Add 2022 day 5 solutions
Diffstat (limited to '2022/05/puzzles.awk')
-rw-r--r--2022/05/puzzles.awk42
1 files changed, 42 insertions, 0 deletions
diff --git a/2022/05/puzzles.awk b/2022/05/puzzles.awk
new file mode 100644
index 0000000..33aad8d
--- /dev/null
+++ b/2022/05/puzzles.awk
@@ -0,0 +1,42 @@
+#!/usr/bin/gawk -f
+
+BEGIN {
+ FIELDWIDTHS = "1:1 3:1 3:1 3:1 3:1 3:1 3:1 3:1 3:1"
+ while (data[n] !~ /[0-9]/)
+ getline data[++n]
+
+ j = 1
+ for (i = n - 1; i > 0; i--) {
+ $0 = data[i]
+ for (k = 1; k <= NF; k++) {
+ if ($k != " ")
+ s[k][j] = $k
+ }
+ j++
+ }
+
+ getline # Ignore blank line
+ FPAT = "[0-9]+"
+}
+
+{
+ # START PART 2
+ l = length(s[$2])
+ # END PART 2
+
+ for (i = 1; i <= $1; i++) {
+ # START PART 1
+ s[$3][length(s[$3]) + 1] = s[$2][length(s[$2])]
+ delete s[$2][length(s[$2])]
+ # END PART 1 START PART 2
+ s[$3][length(s[$3]) + 1] = s[$2][l - $1 + i]
+ delete s[$2][l - $1 + i]
+ # END PART 2
+ }
+}
+
+END {
+ for (i = 1; i <= 9; i++)
+ printf s[i][length(s[i])]
+ print ""
+}