aboutsummaryrefslogtreecommitdiff
path: root/challenge-099/abigail/lua/ch-1.lua
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-099/abigail/lua/ch-1.lua')
-rw-r--r--challenge-099/abigail/lua/ch-1.lua47
1 files changed, 47 insertions, 0 deletions
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