From 44bcd3ae0c35b887bd579e60c17198360f3497c3 Mon Sep 17 00:00:00 2001 From: Abigail Date: Tue, 30 Nov 2021 20:04:35 +0100 Subject: Solutions week 141 --- challenge-141/abigail/lua/ch-2.lua | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 challenge-141/abigail/lua/ch-2.lua (limited to 'challenge-141/abigail/lua/ch-2.lua') diff --git a/challenge-141/abigail/lua/ch-2.lua b/challenge-141/abigail/lua/ch-2.lua new file mode 100644 index 0000000000..b7f87027f4 --- /dev/null +++ b/challenge-141/abigail/lua/ch-2.lua @@ -0,0 +1,37 @@ +#!/opt/local/bin/lua + +-- +-- See ../README.md +-- + +-- +-- Run as: lua ch-2.lua < input-file +-- + +function substrings (n, m, prefix, max) + if n : len () == 0 then + if prefix > -1 and prefix < max and prefix % m == 0 then + return 1 + else + return 0 + end + end + + local fc = tonumber (n : sub (1, 1)) + local tail = n : sub (2) + local n_prefix + if prefix == -1 then + n_prefix = fc + else + n_prefix = 10 * prefix + fc + end + + return substrings (tail, m, n_prefix, max) + + substrings (tail, m, prefix, max) +end + + +for line in io . lines () do + local _, _, n, m = line : find ("([0-9]+)%s+([0-9]+)") + print (substrings (n, tonumber (m), -1, tonumber (n))) +end -- cgit