aboutsummaryrefslogtreecommitdiff
path: root/challenge-083/tyler-wardhaugh/lua
diff options
context:
space:
mode:
authorSteve Rogerson <steve.git@yewtc.demon.co.uk>2020-10-23 12:27:13 +0100
committerSteve Rogerson <steve.git@yewtc.demon.co.uk>2020-10-23 12:27:13 +0100
commit44c2bbf87ac613a2a442cc4e54d810c20a042ff2 (patch)
tree3af39af1f773a9e6b44e91a3852a7553dae7c986 /challenge-083/tyler-wardhaugh/lua
parentff3c07c3e4409c8d507b3e69496c690b58de524d (diff)
parent89421f14095148aefcd254da3d728b6150b22cc3 (diff)
downloadperlweeklychallenge-club-44c2bbf87ac613a2a442cc4e54d810c20a042ff2.tar.gz
perlweeklychallenge-club-44c2bbf87ac613a2a442cc4e54d810c20a042ff2.tar.bz2
perlweeklychallenge-club-44c2bbf87ac613a2a442cc4e54d810c20a042ff2.zip
Merge branch 'master' of https://github.com/manwar/perlweeklychallenge-club
Diffstat (limited to 'challenge-083/tyler-wardhaugh/lua')
-rw-r--r--challenge-083/tyler-wardhaugh/lua/README.md6
-rwxr-xr-xchallenge-083/tyler-wardhaugh/lua/ch-1.lua20
-rwxr-xr-xchallenge-083/tyler-wardhaugh/lua/ch-2.lua34
-rwxr-xr-xchallenge-083/tyler-wardhaugh/lua/run.lua9
-rwxr-xr-xchallenge-083/tyler-wardhaugh/lua/test.lua20
5 files changed, 86 insertions, 3 deletions
diff --git a/challenge-083/tyler-wardhaugh/lua/README.md b/challenge-083/tyler-wardhaugh/lua/README.md
index 8247d79487..35e8606521 100644
--- a/challenge-083/tyler-wardhaugh/lua/README.md
+++ b/challenge-083/tyler-wardhaugh/lua/README.md
@@ -1,17 +1,17 @@
# The Weekly Challenge
-The Weekly Challenge - #081 - Tyler Wardhaugh
+The Weekly Challenge - #083 - Tyler Wardhaugh
## Usage
Run Task 1:
- $ ./run.lua ch-1 M N
+ $ ./run.lua ch-1 S
Run Task 2:
- $ ./run.lua ch-2 A B C
+ $ ./run.lua ch-2 A1 A2 A3...
Run the project's tests (all the samples from the task descriptions plus some others):
diff --git a/challenge-083/tyler-wardhaugh/lua/ch-1.lua b/challenge-083/tyler-wardhaugh/lua/ch-1.lua
new file mode 100755
index 0000000000..cb4f35d915
--- /dev/null
+++ b/challenge-083/tyler-wardhaugh/lua/ch-1.lua
@@ -0,0 +1,20 @@
+#!/usr/bin/env lua
+
+local t1 = {}
+
+function t1.inner_words_length(s)
+ local inner = s:match("^%S+%s+%f[%S](.*)%f[%s]%s+%S+$")
+ if inner then
+ return inner:gsub("%s+", ""):len()
+ else
+ return 0
+ end
+end
+
+function t1.run(args)
+ local s = args[1]
+ local len = t1.inner_words_length(s)
+ print(len)
+end
+
+return t1
diff --git a/challenge-083/tyler-wardhaugh/lua/ch-2.lua b/challenge-083/tyler-wardhaugh/lua/ch-2.lua
new file mode 100755
index 0000000000..bf900a864e
--- /dev/null
+++ b/challenge-083/tyler-wardhaugh/lua/ch-2.lua
@@ -0,0 +1,34 @@
+#!/usr/bin/env lua
+
+local t2 = {}
+
+function t2.flip_array(coll)
+ local min_flips = math.maxinteger
+ local min_sum = math.maxinteger
+ local max_bits = 2^#coll - 1
+
+ local cur_num, cur_sum, is_neg
+ for bits = 1, max_bits do
+ cur_num, cur_sum, is_neg = 0, 0, 0
+ for i, v in ipairs(coll) do
+ is_neg = bits & 2^(i-1) == 0
+ cur_num = cur_num + (is_neg and 1 or 0)
+ cur_sum = cur_sum + v * (is_neg and -1 or 1)
+ end
+
+ if 0 <= cur_sum and cur_sum <= min_sum then
+ min_flips = math.min(cur_num, min_flips)
+ min_sum = cur_sum
+ end
+ end
+
+ return min_flips
+end
+
+
+function t2.run(args)
+ local minimum = t2.flip_array(args)
+ print(minimum)
+end
+
+return t2
diff --git a/challenge-083/tyler-wardhaugh/lua/run.lua b/challenge-083/tyler-wardhaugh/lua/run.lua
new file mode 100755
index 0000000000..c6e7473bee
--- /dev/null
+++ b/challenge-083/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-083/tyler-wardhaugh/lua/test.lua b/challenge-083/tyler-wardhaugh/lua/test.lua
new file mode 100755
index 0000000000..7fa7effa5f
--- /dev/null
+++ b/challenge-083/tyler-wardhaugh/lua/test.lua
@@ -0,0 +1,20 @@
+#!/usr/bin/env lua
+
+require 'busted.runner'()
+
+describe("Task 1, Words Length", function()
+ local t1 = require'ch-1'
+ it("produces correct results for the examples", function()
+ assert.are.same(t1.inner_words_length"The Weekly Challenge", 6)
+ assert.are.same(t1.inner_words_length"The purpose of our lives is to be happy", 23)
+ assert.are.same(t1.inner_words_length"Zero when-no-inner-words-exist!", 0)
+ end)
+end)
+
+describe("Task 2, Flip Array", function()
+ local t2 = require'ch-2'
+ it("produces correct results for the examples", function()
+ assert.are.same(t2.flip_array({3, 10, 8}), 1)
+ assert.are.same(t2.flip_array({12, 2, 10}), 1)
+ end)
+end)