From 23b2fd7bb31c97cd99ae62dc522a7607fcc2a297 Mon Sep 17 00:00:00 2001 From: Abigail Date: Wed, 19 May 2021 18:44:56 +0200 Subject: Lua solutions for week 113 --- challenge-113/abigail/README.md | 4 +++- challenge-113/abigail/lua/ch-1.lua | 40 ++++++++++++++++++++++++++++++++++++++ challenge-113/abigail/lua/ch-2.lua | 25 ++++++++++++++++++++++++ 3 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 challenge-113/abigail/lua/ch-1.lua create mode 100644 challenge-113/abigail/lua/ch-2.lua diff --git a/challenge-113/abigail/README.md b/challenge-113/abigail/README.md index a1e156b3c0..3d3183a0d1 100644 --- a/challenge-113/abigail/README.md +++ b/challenge-113/abigail/README.md @@ -21,6 +21,7 @@ Output: 1 * [AWK](awk/ch-1.awk) * [Bash](bash/ch-1.sh) * [C](c/ch-1.c) +* [Lua](lua/ch-1.lua) * [Perl](perl/ch-1.pl) ### Blog @@ -58,7 +59,8 @@ Output: 1 ### Solutions * [AWK](awk/ch-2.awk) * [Bash](bash/ch-2.sh) -* [C](c/ch-1.c) +* [C](c/ch-2.c) +* [Lua](lua/ch-2.lua) * [Perl](perl/ch-2.pl) ### Blog diff --git a/challenge-113/abigail/lua/ch-1.lua b/challenge-113/abigail/lua/ch-1.lua new file mode 100644 index 0000000000..36bd8e8afd --- /dev/null +++ b/challenge-113/abigail/lua/ch-1.lua @@ -0,0 +1,40 @@ +#!/opt/local/bin/lua + +-- +-- See ../README.md +-- + +-- +-- Run as: lua ch-1.lua < input-file +-- + +local tens = {0, 1, 2, 1, 0, 2, 6, 3, 8} + +for line in io . lines () do + local _, _, N, D = line : find ("([0-9]+)%s+([0-9])") + N = tonumber (N) + D = tonumber (D) + local D10 = 10 * D + if D == 0 + then D10 = 100 + end + if (N >= D10) or (D == 0 and N % 10 == 0) + or (D > 0 and N % D == 0) + then print (1) + goto end_loop + end + + if D > 0 + then for i = 1, tens [D] + do local T = N - 10 * i - D + if T >= 0 and T % D == 0 + then print (1) + goto end_loop + end + end + end + + print (0) + + ::end_loop:: +end diff --git a/challenge-113/abigail/lua/ch-2.lua b/challenge-113/abigail/lua/ch-2.lua new file mode 100644 index 0000000000..15aac1ac2d --- /dev/null +++ b/challenge-113/abigail/lua/ch-2.lua @@ -0,0 +1,25 @@ +#!/opt/local/bin/lua + +-- +-- See ../README.md +-- + +-- +-- Run as: lua ch-2.lua < input-file +-- + +for line in io . lines () do + local sum = 0 + for n in line : gmatch ("[0-9]+") + do sum = sum + tonumber (n) + end + local c = 0 + for n in line : gmatch ("[0-9]+") + do if c > 0 + then io . write (" ") + end + c = c + 1 + io . write (sum - n) + end + io . write ("\n") +end -- cgit