aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorchirvasitua <chirvasitua@gmail.com>2021-07-10 21:43:59 -0400
committerchirvasitua <chirvasitua@gmail.com>2021-07-10 21:43:59 -0400
commitccf2cb84cb57a94b14a25cff162fd20cbb413b2e (patch)
treed9489384eca8dd72e09c2c3231b67c9551b202e8
parent091011ae56b19db7de27d84018d2c1cb015e3b4e (diff)
downloadperlweeklychallenge-club-ccf2cb84cb57a94b14a25cff162fd20cbb413b2e.tar.gz
perlweeklychallenge-club-ccf2cb84cb57a94b14a25cff162fd20cbb413b2e.tar.bz2
perlweeklychallenge-club-ccf2cb84cb57a94b14a25cff162fd20cbb413b2e.zip
1st commit on 112_lua
-rwxr-xr-xchallenge-112/stuart-little/lua/ch-1.lua28
-rwxr-xr-xchallenge-112/stuart-little/lua/ch-2.lua25
2 files changed, 53 insertions, 0 deletions
diff --git a/challenge-112/stuart-little/lua/ch-1.lua b/challenge-112/stuart-little/lua/ch-1.lua
new file mode 100755
index 0000000000..6e4d951111
--- /dev/null
+++ b/challenge-112/stuart-little/lua/ch-1.lua
@@ -0,0 +1,28 @@
+#!/usr/bin/env lua
+
+-- run <script> <path>
+
+function deSlash(s)
+ local news = s:gsub("/+","/"):gsub("(.+)/$","%1")
+ return news
+end
+
+function deDot(s)
+ local news = s:gsub("/%./","/"):gsub("/%.$","")
+ return news
+end
+
+function deDblDotMid(s)
+ local news=s:gsub("/%.%.$","/../")
+ local fst = news:gmatch("([^/]*)/%.%./")()
+ if not fst then return news end
+ if fst == ".." or fst=="" then error("Too many double dots!") end
+ local sml = news:gsub(fst.."/%.%./","/",1)
+ return deDblDotMid(deSlash(sml))
+end
+
+function norm(s)
+ return deSlash(deDblDotMid(deDot(s)))
+end
+
+print(norm(arg[1]))
diff --git a/challenge-112/stuart-little/lua/ch-2.lua b/challenge-112/stuart-little/lua/ch-2.lua
new file mode 100755
index 0000000000..2c5374463b
--- /dev/null
+++ b/challenge-112/stuart-little/lua/ch-2.lua
@@ -0,0 +1,25 @@
+#!/usr/bin/env lua
+
+-- run <script> <number>
+
+local memo={[0]={{}},{{1}},{{1,1},{2}}}
+
+function pths(n)
+ if n >= #memo then
+ table.insert(memo,{})
+ for i = 1,2 do
+ for _,pth in ipairs(memo[n-i]) do
+ table.insert(memo[n],table.pack(i,table.unpack(pth)))
+ end
+ end
+ end
+ return memo[n]
+end
+
+for i = 3,arg[1] do
+ pths(i)
+end
+
+for _,pth in ipairs(memo[tonumber(arg[1])]) do
+ print(table.unpack(pth))
+end