diff options
| author | Simon Green <mail@simon.green> | 2025-09-07 21:12:46 +1000 |
|---|---|---|
| committer | Simon Green <mail@simon.green> | 2025-09-07 21:12:46 +1000 |
| commit | a044dc87aa5e75e5510f78ad13e643b04ed4713b (patch) | |
| tree | e3e966a1072d453807f51d34cb79ff7692a36501 /challenge-337/sgreen/python/ch-2.py | |
| parent | e80c93c27044ee16a834a9ee64a0087c1c7d0b1d (diff) | |
| download | perlweeklychallenge-club-a044dc87aa5e75e5510f78ad13e643b04ed4713b.tar.gz perlweeklychallenge-club-a044dc87aa5e75e5510f78ad13e643b04ed4713b.tar.bz2 perlweeklychallenge-club-a044dc87aa5e75e5510f78ad13e643b04ed4713b.zip | |
sgreen solutions to challenge 337
Diffstat (limited to 'challenge-337/sgreen/python/ch-2.py')
| -rwxr-xr-x | challenge-337/sgreen/python/ch-2.py | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/challenge-337/sgreen/python/ch-2.py b/challenge-337/sgreen/python/ch-2.py new file mode 100755 index 0000000000..a6fb4e597c --- /dev/null +++ b/challenge-337/sgreen/python/ch-2.py @@ -0,0 +1,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() |
