aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAbigail <abigail@abigail.be>2021-03-02 19:08:13 +0100
committerAbigail <abigail@abigail.be>2021-03-02 19:08:13 +0100
commitbdffbc626516f9aa9ee656ab7e5ac4f5e964307a (patch)
tree0fce99c3bd9328b35ae8046cb1c77e3d18d9376c
parent33cf436bf8f996b78f94605ae2335f4bde37bbc4 (diff)
downloadperlweeklychallenge-club-bdffbc626516f9aa9ee656ab7e5ac4f5e964307a.tar.gz
perlweeklychallenge-club-bdffbc626516f9aa9ee656ab7e5ac4f5e964307a.tar.bz2
perlweeklychallenge-club-bdffbc626516f9aa9ee656ab7e5ac4f5e964307a.zip
AWK solution for week 102, part 2
-rw-r--r--challenge-102/abigail/README.md1
-rw-r--r--challenge-102/abigail/awk/ch-2.awk36
2 files changed, 37 insertions, 0 deletions
diff --git a/challenge-102/abigail/README.md b/challenge-102/abigail/README.md
index 4bd71dcf9b..33608bff29 100644
--- a/challenge-102/abigail/README.md
+++ b/challenge-102/abigail/README.md
@@ -85,4 +85,5 @@ such length-`N` string.
~~~~
### Solutions
+* [AWK](awk/ch-2.awk)
* [Perl](perl/ch-2.pl)
diff --git a/challenge-102/abigail/awk/ch-2.awk b/challenge-102/abigail/awk/ch-2.awk
new file mode 100644
index 0000000000..e673437860
--- /dev/null
+++ b/challenge-102/abigail/awk/ch-2.awk
@@ -0,0 +1,36 @@
+#!/usr/bin/awk
+
+#
+# See ../README.md
+#
+
+#
+# Run as: awk -f ch-2.awk < input-file
+#
+
+#
+# Working from the end of the required string backwards, we alternate
+# placing a hash, and placing a number. We place them in an array out,
+# and at the end, print out said array in reverse order.
+#
+
+{
+ idx = $1
+ hash = 0
+ i = 0
+ while (idx) {
+ i ++
+ if (hash = !hash) {
+ out [i] = "#"
+ idx --
+ }
+ else {
+ out [i] = idx + 1
+ idx -= length (idx + 1)
+ }
+ }
+ for (; i; i --) {
+ printf "%s", out [i]
+ }
+ printf "\n"
+}