aboutsummaryrefslogtreecommitdiff
path: root/challenge-251/roger-bell-west/lua/ch-2.lua
blob: b6f31930c004c032e28209d93c6aaecacc174d9f (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
#! /usr/bin/lua

function luckynumbers(a)
   local maxs = {}
   for x = 1, #(a[1]) do
      local max = {0, 0, 0}
      for y = 1, #a do
         if a[y][x] > max[1] then
            max = {a[y][x], y, x}
         end
      end
      table.insert(maxs, max)
   end
   local mins = {}
   for y = 1, #a do
      local min = {a[y][1], y, 1}
      for x = 1, #(a[1]) do
         if a[y][x] < min[1] then
            min = {a[y][x], y, x}
         end
      end
      table.insert(mins, min)
   end
   for i, v in ipairs(mins) do
      for j, w in ipairs(maxs) do
         if v[1] == w[1] and v[2] == w[2] and v[3] == w[3] then
            return v[1]
         end
      end
   end
   return -1
end

if luckynumbers({{3, 7, 9}, {9, 11, 13}, {15, 16, 17}}) == 15 then
  io.write("Pass")
else
  io.write("FAIL")
end
io.write(" ")

if luckynumbers({{1, 10, 4, 2}, {9, 3, 8, 7}, {15, 16, 17, 12}}) == 12 then
  io.write("Pass")
else
  io.write("FAIL")
end
io.write(" ")

if luckynumbers({{7, 8}, {1, 2}}) == 7 then
  io.write("Pass")
else
  io.write("FAIL")
end
print("")