aboutsummaryrefslogtreecommitdiff
path: root/challenge-091/tyler-wardhaugh/lua
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2020-12-18 22:39:33 +0000
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2020-12-18 22:39:33 +0000
commit511f2e58d09f8d26f251a95c282da9d7b4be9b39 (patch)
tree023570136fcefd5d76e168195971ad7afebeccd8 /challenge-091/tyler-wardhaugh/lua
parent42bbd7ccecb01b8550e34d68fd3af05fc7bdc26a (diff)
parenta395742fbe5e2605110c5fa4ee1f8a83e5c8173b (diff)
downloadperlweeklychallenge-club-511f2e58d09f8d26f251a95c282da9d7b4be9b39.tar.gz
perlweeklychallenge-club-511f2e58d09f8d26f251a95c282da9d7b4be9b39.tar.bz2
perlweeklychallenge-club-511f2e58d09f8d26f251a95c282da9d7b4be9b39.zip
Merge branch 'master' of https://github.com/manwar/perlweeklychallenge-club
Diffstat (limited to 'challenge-091/tyler-wardhaugh/lua')
-rw-r--r--challenge-091/tyler-wardhaugh/lua/README.md6
-rwxr-xr-xchallenge-091/tyler-wardhaugh/lua/ch-1.lua26
-rwxr-xr-xchallenge-091/tyler-wardhaugh/lua/ch-2.lua35
-rwxr-xr-xchallenge-091/tyler-wardhaugh/lua/run.lua9
-rwxr-xr-xchallenge-091/tyler-wardhaugh/lua/test.lua20
5 files changed, 93 insertions, 3 deletions
diff --git a/challenge-091/tyler-wardhaugh/lua/README.md b/challenge-091/tyler-wardhaugh/lua/README.md
index 8772dac9c1..2e1e278b45 100644
--- a/challenge-091/tyler-wardhaugh/lua/README.md
+++ b/challenge-091/tyler-wardhaugh/lua/README.md
@@ -1,17 +1,17 @@
# The Weekly Challenge
-The Weekly Challenge - #090 - Tyler Wardhaugh
+The Weekly Challenge - #091 - Tyler Wardhaugh
## Usage
Run Task 1:
- $ ./run.lua ch-1 DNA
+ $ ./run.lua ch-1 N
Run Task 2:
- $ ./run.lua ch-2 A B
+ $ ./run.lua ch-2 N1 N2 N3...
Run the project's tests (all the samples from the task descriptions plus some others):
diff --git a/challenge-091/tyler-wardhaugh/lua/ch-1.lua b/challenge-091/tyler-wardhaugh/lua/ch-1.lua
new file mode 100755
index 0000000000..844ba9b32e
--- /dev/null
+++ b/challenge-091/tyler-wardhaugh/lua/ch-1.lua
@@ -0,0 +1,26 @@
+#!/usr/bin/env lua
+
+local t1 = {}
+t1.DEFAULT_INPUT = 1122234
+
+function t1.count_numbers(n)
+ --[[
+ Lua's standard pattern matching does not support back-references, so
+ we need to do some trickery.
+
+ Idea from:
+ https://stackoverflow.com/questions/48541679/lua-pattern-matching-series-of-same-character
+ ]]--
+ local result = (tostring(n) .. '\0')
+ :gsub('%d', '\0%0%0')
+ :gsub('(.)%z%1', '%1')
+ :gsub('%d+%z', function(run) return run:len() - 2 .. run:sub(1, 1) end)
+ return tonumber(result:sub(2))
+end
+
+function t1.run(args)
+ local n = args[1] or t1.DEFAULT_INPUT
+ print(t1.count_numbers(n))
+end
+
+return t1
diff --git a/challenge-091/tyler-wardhaugh/lua/ch-2.lua b/challenge-091/tyler-wardhaugh/lua/ch-2.lua
new file mode 100755
index 0000000000..768ced2596
--- /dev/null
+++ b/challenge-091/tyler-wardhaugh/lua/ch-2.lua
@@ -0,0 +1,35 @@
+#!/usr/bin/env lua
+
+local t2 = {}
+t2.DEFAULT_INPUT = {1, 2, 1, 2}
+
+function t2.jump_game(coll)
+ local i,j = 1
+ while true do
+ if i == #coll then return true end
+
+ j = i + coll[i]
+ if i == j or j > #coll then
+ return false
+ end
+
+ i = j
+ end
+end
+
+function t2.run(args)
+ local coll
+ if #args > 0 then
+ coll = {}
+ for _, v in ipairs(args) do
+ table.insert(coll, tonumber(v))
+ end
+ else
+ coll = t2.DEFAULT_INPUT
+ end
+
+ local result = t2.jump_game(coll)
+ if result then print(1) else print(0) end
+end
+
+return t2
diff --git a/challenge-091/tyler-wardhaugh/lua/run.lua b/challenge-091/tyler-wardhaugh/lua/run.lua
new file mode 100755
index 0000000000..c6e7473bee
--- /dev/null
+++ b/challenge-091/tyler-wardhaugh/lua/run.lua
@@ -0,0 +1,9 @@
+#!/usr/bin/env lua
+
+local filename = arg[1]
+local run_args = table.move(arg, 2, #arg, 1, {})
+
+io.write(string.format("Running task from '%s' with {%s}:\n",
+ filename, table.concat(run_args, ", ")))
+
+require(filename).run(run_args)
diff --git a/challenge-091/tyler-wardhaugh/lua/test.lua b/challenge-091/tyler-wardhaugh/lua/test.lua
new file mode 100755
index 0000000000..a96d8fa68e
--- /dev/null
+++ b/challenge-091/tyler-wardhaugh/lua/test.lua
@@ -0,0 +1,20 @@
+#!/usr/bin/env lua
+
+require 'busted.runner'()
+
+describe("Task 1, Count Number", function()
+ local t1 = require'ch-1'
+ it("produces correct results for the examples", function()
+ assert.are.same(21321314, t1.count_numbers(1122234))
+ assert.are.same(12332415, t1.count_numbers(2333445))
+ assert.are.same(1112131415, t1.count_numbers(12345))
+ end)
+end)
+
+describe("Task 2, Jump Game", function()
+ local t2 = require'ch-2'
+ it("produces correct results for the examples", function()
+ assert.truthy(t2.jump_game{1, 2, 1, 2})
+ assert.falsy(t2.jump_game{2, 1, 1, 0, 2})
+ end)
+end)