diff options
author | Thomas Voss <thomasvoss@live.com> | 2021-12-12 06:56:03 +0100 |
---|---|---|
committer | Thomas Voss <thomasvoss@live.com> | 2021-12-12 06:56:03 +0100 |
commit | b4195baae3b36bd997a97d24fd5e0ab38dcd5595 (patch) | |
tree | eca4ea75f16be761da19409b83a45ce88dd1b0c5 /2021/09/puzzle-1.py | |
parent | c93fc1b9a11c96a4554ec47029bf085597fdef60 (diff) |
Add libaoc
Diffstat (limited to '2021/09/puzzle-1.py')
-rwxr-xr-x | 2021/09/puzzle-1.py | 42 |
1 files changed, 23 insertions, 19 deletions
diff --git a/2021/09/puzzle-1.py b/2021/09/puzzle-1.py index 5ab2f45..be333a5 100755 --- a/2021/09/puzzle-1.py +++ b/2021/09/puzzle-1.py @@ -1,29 +1,33 @@ #!/usr/bin/env python3 +from itertools import product + +from libaoc import read_int_matrix + + def main() -> None: with open("input", "r", encoding="utf-8") as f: - data = list(map(lambda l: [int(n) for n in l.strip()], f.readlines())) + data = read_int_matrix(f) acc = 0 - rows = len(data[0]) - cols = len(data) - - for i in range(cols): - for j in range(rows): - if ( - i != 0 - and data[i - 1][j] <= data[i][j] - or i != cols - 1 - and data[i + 1][j] <= data[i][j] - or j != 0 - and data[i][j - 1] <= data[i][j] - or j != rows - 1 - and data[i][j + 1] <= data[i][j] - ): - continue - - acc += data[i][j] + 1 + rows = len(data) + cols = len(data[0]) + + for i, j in product(range(rows), range(cols)): + if ( + i != 0 + and data[i - 1][j] <= data[i][j] + or i != cols - 1 + and data[i + 1][j] <= data[i][j] + or j != 0 + and data[i][j - 1] <= data[i][j] + or j != rows - 1 + and data[i][j + 1] <= data[i][j] + ): + continue + + acc += data[i][j] + 1 print(acc) |