aboutsummaryrefslogtreecommitdiff
path: root/challenge-251/sgreen/python/ch-2.py
blob: 643760029c06117ff2da1c59d043d29331c414ba (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
#!/usr/bin/env python3

import json
import sys


def main(matrix):
    # Calculate the size of the matrix
    rows = len(matrix)
    cols = len(matrix[0])

    # Check that all columns are of equal length
    for r in range(1, rows):
        if len(matrix[r]) != cols:
            raise ValueError(f'Row {r} has different number of columns')

    # Calculate the column maximums
    column_max = []
    for c in range(cols):
        column_max.append(max(map(lambda i: i[c], matrix)))

    solution = -1

    # Go through each row
    for r in matrix:
        # Get the value of all columns that are the minimum value for this row
        #  and the maximum for the column
        row_min = min(r)
        if any(True for i, value in enumerate(r)
               if value == row_min and column_max[i] == value):
            # We have a lucky number (row_min)
            solution = row_min
            break

    print(solution)


if __name__ == '__main__':
    # Read the input as JSON
    main(json.loads(sys.argv[1]))