aboutsummaryrefslogtreecommitdiff
path: root/challenge-099/abigail
diff options
context:
space:
mode:
authorAbigail <abigail@abigail.be>2021-02-10 19:55:55 +0100
committerAbigail <abigail@abigail.be>2021-02-10 19:55:55 +0100
commitd185f096ff8d9843c6ba48484c6dcc2c075866b3 (patch)
tree82e34cd81a5a7c767de4492a48103d6ee84db8be /challenge-099/abigail
parentdeda23c261ee75dab3b3713ad21e7e4aa4205a3e (diff)
downloadperlweeklychallenge-club-d185f096ff8d9843c6ba48484c6dcc2c075866b3.tar.gz
perlweeklychallenge-club-d185f096ff8d9843c6ba48484c6dcc2c075866b3.tar.bz2
perlweeklychallenge-club-d185f096ff8d9843c6ba48484c6dcc2c075866b3.zip
Lua solution for week 99, part 1
Diffstat (limited to 'challenge-099/abigail')
-rw-r--r--challenge-099/abigail/README.md1
-rw-r--r--challenge-099/abigail/lua/ch-1.lua47
2 files changed, 48 insertions, 0 deletions
diff --git a/challenge-099/abigail/README.md b/challenge-099/abigail/README.md
index 3e98f336de..159a51bf25 100644
--- a/challenge-099/abigail/README.md
+++ b/challenge-099/abigail/README.md
@@ -42,6 +42,7 @@ Output: 1
### Solutions
* [AWK](awk/ch-1.awk)
* [C](c/ch-1.c)
+* [Lua](lua/ch-1.lua)
* Perl
* [Using regular expressions](perl/ch-1.pl)
* [Recursion](perl/ch-1a.pl)
diff --git a/challenge-099/abigail/lua/ch-1.lua b/challenge-099/abigail/lua/ch-1.lua
new file mode 100644
index 0000000000..fc40672471
--- /dev/null
+++ b/challenge-099/abigail/lua/ch-1.lua
@@ -0,0 +1,47 @@
+#!/opt/local/bin/lua
+
+--
+-- See ../README.md
+--
+
+--
+-- Run as: lua ch-1.lua < input-file
+--
+
+for line in io . lines () do
+ --
+ -- Extract the string and pattern
+ --
+ local str, pattern = line : match ("(%S+) +(%S+)")
+
+ --
+ -- Turn the pattern into a Lua pattern, replace any
+ -- character c:
+ -- if c == "?" -> .
+ -- if c == "*" -> .*
+ -- if c is not an alpanum -> %c
+ -- otherwise, keep c as is
+ -- And then add anchors
+ --
+ pattern = pattern : gsub (".", function (c)
+ local result = c
+ if c == "?" then
+ result = "."
+ elseif c == "*" then
+ result = ".*"
+ elseif c : match ("%W") then
+ result = "%" .. c
+ end
+ return result
+ end)
+ pattern = "^" .. pattern .. "$"
+
+ --
+ -- Print 1/0 depending whether we have a match or not.
+ --
+ if (str : match (pattern)) then
+ print (1)
+ else
+ print (0)
+ end
+end