diff options
| author | Abigail <abigail@abigail.be> | 2021-03-03 01:03:09 +0100 |
|---|---|---|
| committer | Abigail <abigail@abigail.be> | 2021-03-03 01:03:09 +0100 |
| commit | 8f07b40ae63f584a00ed8a3d60df728aaa534f46 (patch) | |
| tree | 35172444c127f554bf9f83c5916a64cbcbe52882 | |
| parent | e123f116fe82c5aceeb268b9ad5e8995d042982c (diff) | |
| download | perlweeklychallenge-club-8f07b40ae63f584a00ed8a3d60df728aaa534f46.tar.gz perlweeklychallenge-club-8f07b40ae63f584a00ed8a3d60df728aaa534f46.tar.bz2 perlweeklychallenge-club-8f07b40ae63f584a00ed8a3d60df728aaa534f46.zip | |
Lua solution for week 102, part 2
| -rw-r--r-- | challenge-102/abigail/README.md | 1 | ||||
| -rw-r--r-- | challenge-102/abigail/lua/ch-2.lua | 36 |
2 files changed, 37 insertions, 0 deletions
diff --git a/challenge-102/abigail/README.md b/challenge-102/abigail/README.md index b54125f5aa..06252fc7d3 100644 --- a/challenge-102/abigail/README.md +++ b/challenge-102/abigail/README.md @@ -88,4 +88,5 @@ such length-`N` string. * [AWK](awk/ch-2.awk) * [Bash](bash/ch-2.sh) * [C](c/ch-2.c) +* [Lua](lua/ch-2.lua) * [Perl](perl/ch-2.pl) diff --git a/challenge-102/abigail/lua/ch-2.lua b/challenge-102/abigail/lua/ch-2.lua new file mode 100644 index 0000000000..bbeb2a13ae --- /dev/null +++ b/challenge-102/abigail/lua/ch-2.lua @@ -0,0 +1,36 @@ +#!/opt/local/bin/lua + +-- +-- See ../README.md +-- + +-- +-- Run as: lua ch-2.lua < 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. +-- +for index in io . lines () do + index = tonumber (index) + out = {} + hash = false + i = 0 + while index > 0 do + hash = not hash + i = i + 1 + if hash then + out [i] = "#" + index = index - 1 + else + out [i] = index + 1 + index = index - tostring (index + 1) : len () + end + end + for j = i, 1, -1 do + io . write (out [j]) + end + io . write ("\n") +end |
