aboutsummaryrefslogtreecommitdiff
path: root/challenge-112
diff options
context:
space:
mode:
authorAbigail <abigail@abigail.be>2021-05-10 21:06:57 +0200
committerAbigail <abigail@abigail.be>2021-05-10 21:06:57 +0200
commit5af854f4892557e1f1de653b46402cbfd7baaff9 (patch)
tree709ab0a16ef2b3cb040602b46b9165fe68868c22 /challenge-112
parent7b5597d51092301df5e79f1104bb1403e42abd65 (diff)
downloadperlweeklychallenge-club-5af854f4892557e1f1de653b46402cbfd7baaff9.tar.gz
perlweeklychallenge-club-5af854f4892557e1f1de653b46402cbfd7baaff9.tar.bz2
perlweeklychallenge-club-5af854f4892557e1f1de653b46402cbfd7baaff9.zip
Lua solutions for week 112
Diffstat (limited to 'challenge-112')
-rw-r--r--challenge-112/abigail/README.md2
-rw-r--r--challenge-112/abigail/lua/ch-1.lua35
-rw-r--r--challenge-112/abigail/lua/ch-2.lua24
3 files changed, 61 insertions, 0 deletions
diff --git a/challenge-112/abigail/README.md b/challenge-112/abigail/README.md
index c619dd35d2..83b5465420 100644
--- a/challenge-112/abigail/README.md
+++ b/challenge-112/abigail/README.md
@@ -36,6 +36,7 @@ Output: "/a"
* [AWK](awk/ch-1.awk)
* [Bash](bash/ch-1.sh)
* [C](c/ch-1.c)
+* [Lua](lua/ch-1.lua)
* [Perl](perl/ch-1.pl)
### Blog
@@ -55,6 +56,7 @@ This is just finding the `$n + 1` Fibonacci number.
* [AWK](awk/ch-2.awk)
* [Bash](bash/ch-2.sh)
* [C](c/ch-2.c)
+* [Lua](lua/ch-2.lua)
* [Perl](perl/ch-2.pl)
### Blog
diff --git a/challenge-112/abigail/lua/ch-1.lua b/challenge-112/abigail/lua/ch-1.lua
new file mode 100644
index 0000000000..2d866dd9bf
--- /dev/null
+++ b/challenge-112/abigail/lua/ch-1.lua
@@ -0,0 +1,35 @@
+#!/opt/local/bin/lua
+
+--
+-- See ../README.md
+--
+
+--
+-- Run as: lua ch-1.lua < input-file
+--
+
+for line in io . lines () do
+ --
+ -- Split into parts
+ --
+ local parts = {}
+ for part in line : gmatch ("[^/]+") do
+ table . insert (parts, part)
+ end
+ --
+ -- Copy to new structure
+ --
+ local parts2 = {}
+ for index, part in ipairs (parts) do
+ if part == "." then -- Current directory -> skip
+ goto continue
+ end
+ if part == ".." then -- Parent direction -> pop from new structure
+ table . remove (parts2)
+ goto continue
+ end
+ table . insert (parts2, part) -- Else, copy
+ ::continue::
+ end
+ print ("/" .. table . concat (parts2, "/")) -- And print
+end
diff --git a/challenge-112/abigail/lua/ch-2.lua b/challenge-112/abigail/lua/ch-2.lua
new file mode 100644
index 0000000000..db76016a8e
--- /dev/null
+++ b/challenge-112/abigail/lua/ch-2.lua
@@ -0,0 +1,24 @@
+#!/opt/local/bin/lua
+
+--
+-- See ../README.md
+--
+
+--
+-- Run as: lua ch-1.lua < input-file
+--
+
+local cache = {}
+cache [0] = 1
+cache [1] = 1
+
+function fib (n)
+ if cache [n] == nil
+ then cache [n] = fib (n - 1) + fib (n - 2)
+ end
+ return cache [n]
+end
+
+for line in io . lines () do
+ print (fib (tonumber (line)))
+end