aboutsummaryrefslogtreecommitdiff
path: root/challenge-101
diff options
context:
space:
mode:
authorAbigail <abigail@abigail.be>2021-02-25 14:20:07 +0100
committerAbigail <abigail@abigail.be>2021-02-25 14:20:07 +0100
commitf38de5ef6d8c6711e71e78ad912ebcf4ddbcc844 (patch)
tree42a74048abd5678a949fca09ab94b9980b8a7679 /challenge-101
parentda2f7e48f7f8c0ecc2653bf233f2c724f35e48f9 (diff)
downloadperlweeklychallenge-club-f38de5ef6d8c6711e71e78ad912ebcf4ddbcc844.tar.gz
perlweeklychallenge-club-f38de5ef6d8c6711e71e78ad912ebcf4ddbcc844.tar.bz2
perlweeklychallenge-club-f38de5ef6d8c6711e71e78ad912ebcf4ddbcc844.zip
AWK solution for week 101, part 1
Diffstat (limited to 'challenge-101')
-rw-r--r--challenge-101/abigail/README.md1
-rw-r--r--challenge-101/abigail/awk/ch-1.awk107
2 files changed, 108 insertions, 0 deletions
diff --git a/challenge-101/abigail/README.md b/challenge-101/abigail/README.md
index 48c4c6976e..24167388cf 100644
--- a/challenge-101/abigail/README.md
+++ b/challenge-101/abigail/README.md
@@ -62,6 +62,7 @@ or
~~~~
### Solutions
+* [AWK](awk/ch-1.awk)
* [Perl](perl/ch-1.pl)
### Blog
diff --git a/challenge-101/abigail/awk/ch-1.awk b/challenge-101/abigail/awk/ch-1.awk
new file mode 100644
index 0000000000..203b902a18
--- /dev/null
+++ b/challenge-101/abigail/awk/ch-1.awk
@@ -0,0 +1,107 @@
+#!/usr/bin/awk
+
+#
+# See ../README.md
+#
+
+#
+# Run as: awk -f ch-1.awk < input-file
+#
+
+
+#
+# Map 2-D coordinates to 1-D coordinates, as AWK doesn't
+# have multidimensional arrays.
+#
+function idx (x, y, h, w) {
+ return x * w + y + 1
+}
+
+
+BEGIN {
+ RIGHT = 0
+ UP = 1
+ LEFT = 2
+ DOWN = 3
+}
+
+{
+ #
+ # Get the data; we're assuming it's one a single line,
+ # seperated by white space.
+ #
+ split ($0, elements)
+
+ #
+ # Find optimal sizes: width w and height h
+ # We start at the square root, and count downwards till we
+ # have a divider.
+ #
+ n = length (elements)
+ h = int (sqrt (n))
+ for (;n % h;) {
+ h --
+ }
+ w = n / h
+
+ #
+ # Fill the matrix, starting from the bottom left
+ #
+ min_x = 0
+ max_x = h - 1
+ min_y = 0
+ max_y = w - 1
+ x = max_x
+ y = 0
+ direction = RIGHT
+ for (i = 1; i <= length (elements); i ++) {
+ matrix [idx(x, y, h, w)] = elements [i]
+ turn = 0
+ if (direction == RIGHT) {
+ if (y >= max_y) {turn = 1; x --; max_x --}
+ else {y ++}
+ }
+ if (direction == UP) {
+ if (x <= min_x) {turn = 1; y --; max_y --}
+ else {x --}
+ }
+ if (direction == LEFT) {
+ if (y <= min_y) {turn = 1; x ++; min_x ++}
+ else {y --}
+ }
+ if (direction == DOWN) {
+ if (x >= max_x) {turn = 1; y ++; min_y ++}
+ else {x ++}
+ }
+ if (turn) {
+ direction ++
+ direction %= 4
+ }
+ }
+
+ #
+ # Find the max widths for each columns.
+ #
+ for (y = 0; y < w; y ++) {
+ max = 0;
+ for (x = 0; x < h; x ++) {
+ l = length (matrix [idx(x, y, h, w)])
+ if (l > max) {
+ max = l
+ }
+ }
+ widths [y + 1] = max
+ }
+
+ #
+ # Print the matrix, with each column outlined
+ #
+ for (x = 0; x < h; x ++) {
+ for (y = 0; y < w; y ++) {
+ width = widths [y + 1]
+ format = "%s%" width "s"
+ printf (format, y == 0 ? "" : " ", matrix [idx(x, y, h, w)])
+ }
+ printf ("\n")
+ }
+}