aboutsummaryrefslogtreecommitdiff
path: root/challenge-343/roger-bell-west/lua/ch-2.lua
blob: cf91282acf9e7d640866a20e9cc61c4df92877dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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("")