diff options
| author | Abigail <abigail@abigail.be> | 2021-07-01 16:05:09 +0200 |
|---|---|---|
| committer | Abigail <abigail@abigail.be> | 2021-07-01 16:05:09 +0200 |
| commit | 2ab88a15e2e63d52dd5c90a558d673774fe3bdf5 (patch) | |
| tree | 88ad04f1c7396a5b976853549716ee85a94f2881 /challenge-119/abigail/lua | |
| parent | 20c14072c2b9a491fd414d275b6c64c6fec8e62f (diff) | |
| parent | e5714129b83b9d88dbbe7b9bd58a7738228f7e2f (diff) | |
| download | perlweeklychallenge-club-2ab88a15e2e63d52dd5c90a558d673774fe3bdf5.tar.gz perlweeklychallenge-club-2ab88a15e2e63d52dd5c90a558d673774fe3bdf5.tar.bz2 perlweeklychallenge-club-2ab88a15e2e63d52dd5c90a558d673774fe3bdf5.zip | |
Merge branch 'master' of https://github.com/manwar/perlweeklychallenge-club
Diffstat (limited to 'challenge-119/abigail/lua')
| -rw-r--r-- | challenge-119/abigail/lua/ch-2.lua | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/challenge-119/abigail/lua/ch-2.lua b/challenge-119/abigail/lua/ch-2.lua new file mode 100644 index 0000000000..e0c940767d --- /dev/null +++ b/challenge-119/abigail/lua/ch-2.lua @@ -0,0 +1,36 @@ +#!/opt/local/bin/lua + +-- +-- See ../README.md +-- + +-- +-- Run as: lua ch-2.lua < input-file +-- + +function next_number (prev_num) + -- + -- Get the trailing 3s (tail), and the number before (num) + -- and anything before it (prefix) + -- + local _, _, prefix, num, tail = + ("0" .. prev_num) : find ("(.*)([012])(3*)$") + + -- + -- Combine the prefix, num (with 1 added) and the tail (with + -- its 3s replaced by 1s), then in the result, replace each '11' + -- with '12', and remove the leading 0 (if any). + -- + return (prefix .. tostring (tonumber (num) + 1) + .. tail : gsub ("3", "1")) : gsub ("11", "12") + : gsub ("^0", "") +end + + +for num in io . lines () do + local number = "0" + for _ = 1, tonumber (num) do + number = next_number (number) + end + print (number) +end |
