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/lua/ch-1.lua | 40 ++++++++++++++++++++++++++++++++++++++ challenge-113/abigail/lua/ch-2.lua | 25 ++++++++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 challenge-113/abigail/lua/ch-1.lua create mode 100644 challenge-113/abigail/lua/ch-2.lua (limited to 'challenge-113/abigail/lua') 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 From 2eedf165fbeb5289fea6c43fbe55b47a0d1ec8f8 Mon Sep 17 00:00:00 2001 From: Abigail Date: Wed, 19 May 2021 19:30:25 +0200 Subject: Allow negative numbers for week 113, part 2 (Perl, Lua) --- challenge-113/abigail/lua/ch-2.lua | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'challenge-113/abigail/lua') diff --git a/challenge-113/abigail/lua/ch-2.lua b/challenge-113/abigail/lua/ch-2.lua index 15aac1ac2d..5ba4c04ed9 100644 --- a/challenge-113/abigail/lua/ch-2.lua +++ b/challenge-113/abigail/lua/ch-2.lua @@ -10,16 +10,16 @@ for line in io . lines () do local sum = 0 - for n in line : gmatch ("[0-9]+") + for n in line : gmatch ("-?[0-9]+") do sum = sum + tonumber (n) end local c = 0 - for n in line : gmatch ("[0-9]+") + for n in line : gmatch ("-?[0-9]+") do if c > 0 then io . write (" ") end c = c + 1 - io . write (sum - n) + io . write (sum - tonumber (n)) end io . write ("\n") end -- cgit From 9043a1b2b6b9d2a1ca0bcf814210f84b0655aa45 Mon Sep 17 00:00:00 2001 From: Abigail Date: Sat, 22 May 2021 18:19:05 +0200 Subject: Slightly improved algorithm for week 113, part 1. Also added a reference to a page with a proof of the algorithm. --- challenge-113/abigail/lua/ch-1.lua | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) (limited to 'challenge-113/abigail/lua') diff --git a/challenge-113/abigail/lua/ch-1.lua b/challenge-113/abigail/lua/ch-1.lua index 36bd8e8afd..4de70d106c 100644 --- a/challenge-113/abigail/lua/ch-1.lua +++ b/challenge-113/abigail/lua/ch-1.lua @@ -8,29 +8,35 @@ -- Run as: lua ch-1.lua < input-file -- -local tens = {0, 1, 2, 1, 0, 2, 6, 3, 8} +-- +-- For a description of the algorithm, and the proof why this is correct: +-- https://abigail.github.io/HTML/Perl-Weekly-Challenge/week-113-1.html +-- + +local gcds = {1, 2, 1, 2, 5, 2, 1, 2, 1} 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 + then if N >= 100 or N % 10 == 0 + then print (1) + else print (0) + end + goto end_loop end - if (N >= D10) or (D == 0 and N % 10 == 0) - or (D > 0 and N % D == 0) + + if N >= D * 10 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 + for i = 0, D / gcds [D] - 1 + do local T = N - 10 * i - D + if T >= 0 and T % D == 0 + then print (1) + goto end_loop end end -- cgit