aboutsummaryrefslogtreecommitdiff
path: root/challenge-343/roger-bell-west/lua/ch-2.lua
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-343/roger-bell-west/lua/ch-2.lua')
-rwxr-xr-xchallenge-343/roger-bell-west/lua/ch-2.lua71
1 files changed, 71 insertions, 0 deletions
diff --git a/challenge-343/roger-bell-west/lua/ch-2.lua b/challenge-343/roger-bell-west/lua/ch-2.lua
new file mode 100755
index 0000000000..cf91282acf
--- /dev/null
+++ b/challenge-343/roger-bell-west/lua/ch-2.lua
@@ -0,0 +1,71 @@
+#! /usr/bin/lua
+
+function reduce(seq, func)
+ local out = seq[1]
+ for i = 2, #seq do
+ out = func(out, seq[i])
+ end
+ return out
+end
+
+function championteam(a)
+ local maxw = 0
+ local teamw = {}
+ for i, w in ipairs(a) do
+ local wins = reduce(w, function(a, b) return a + b end)
+ if wins > maxw then
+ teamw = {}
+ maxw = wins
+ end
+ if wins == maxw then
+ table.insert(teamw, i)
+ end
+ end
+ if #teamw == 1 then
+ return teamw[1] - 1
+ end
+ local bestt = teamw[1]
+ for _, rt in ipairs(teamw) do
+ if a[rt][bestt] == 1 then
+ bestt = rt
+ end
+ end
+ return bestt - 1
+end
+
+
+if championteam({{0, 1, 1}, {0, 0, 1}, {0, 0, 0}}) == 0 then
+ io.write("Pass")
+else
+ io.write("FAIL")
+end
+io.write(" ")
+
+if championteam({{0, 1, 0, 0}, {0, 0, 0, 0}, {1, 1, 0, 0}, {1, 1, 1, 0}}) == 3 then
+ io.write("Pass")
+else
+ io.write("FAIL")
+end
+io.write(" ")
+
+if championteam({{0, 1, 0, 1}, {0, 0, 1, 1}, {1, 0, 0, 0}, {0, 0, 1, 0}}) == 0 then
+ io.write("Pass")
+else
+ io.write("FAIL")
+end
+io.write(" ")
+
+if championteam({{0, 1, 1}, {0, 0, 0}, {0, 1, 0}}) == 0 then
+ io.write("Pass")
+else
+ io.write("FAIL")
+end
+io.write(" ")
+
+if championteam({{0, 0, 0, 0, 0}, {1, 0, 0, 0, 0}, {1, 1, 0, 1, 1}, {1, 1, 0, 0, 0}, {1, 1, 0, 1, 0}}) == 2 then
+ io.write("Pass")
+else
+ io.write("FAIL")
+end
+print("")
+