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

from dataclasses import dataclass
import sys


@dataclass
class Point:
    """A point in the matrix."""
    x: int
    y: int

    def score(self, row:int, col:int) -> int:
        """Calculate the score for this point at the given row and column."""
        if row == self.x and col == self.y:
            return 2
        if row == self.x or col == self.y:
            return 1

        return 0

def odd_matrix(rows:int, cols:int, points_list: list) -> int:
    """Calculate the number of cells in the matrix with an odd score.

    Args:
        rows: Number of rows in the matrix.
        col: Number of columns in the matrix.
        points_list: List of (x, y) tuples representing points in the matrix.

    Returns:
        The number of cells in the matrix with an odd score.
    """
    # Convert the list of points into Point objects
    points = [Point(x, y) for x, y in points_list]
    odd_cells = 0

    for row in range(rows):
        for col in range(cols):
            # Calculate the score for this cell
            score = 0
            for point in points:
                score += point.score(row, col)

            # If the score is odd, increment the count of odd cells
            if score % 2 == 1:
                odd_cells += 1

    return odd_cells


def main():
    # Convert input into integers
    array = [int(n) for n in sys.argv[1:]]
    row = array.pop(0)
    col = array.pop(0)
    points = [(array[i], array[i + 1]) for i in range(0, len(array), 2)]

    result = odd_matrix(row, col, points)
    print(result)


if __name__ == '__main__':
    main()