From d01e94705e0b6c140e8219c081fe5c2ca73e4888 Mon Sep 17 00:00:00 2001 From: Alex Karle Date: Sat, 11 Dec 2021 16:04:44 -0500 Subject: [PATCH] day9: Remove "inf" border in favor of extra if's I think this ultimately just reads cleaner than having the surrounding inf boarder! No speedup, really. --- 09/a.py | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/09/a.py b/09/a.py index edb3574..7dad784 100755 --- a/09/a.py +++ b/09/a.py @@ -1,34 +1,28 @@ #!/usr/bin/env python3 import sys -from math import inf - -grid = [[inf] * 102] +grid = [] for l in sys.stdin: - grid.append([inf] + [int(x) for x in l.strip()] + [inf]) + grid.append([int(x) for x in l.strip()]) -grid.append([inf] * 102) def lowest(r, c): pt = grid[r][c] - if grid[r-1][c] <= pt: + if r > 0 and grid[r - 1][c] <= pt: return False - if grid[r][c-1] <= pt: + if c > 0 and grid[r][c - 1] <= pt: return False - if grid[r][c+1] <= pt: + if c < 99 and grid[r][c + 1] <= pt: return False - if grid[r+1][c] <= pt: + if r < 99 and grid[r + 1][c] <= pt: return False return True + risk = 0 -for r in range(1, 101): - # print("") - for c in range(1, 101): +for r in range(100): + for c in range(100): if lowest(r, c): - # print(f"[{grid[r][c]}]", end="") risk += grid[r][c] + 1 - # else: - # print(f" {grid[r][c]} ", end="") print(risk) -- libgit2 1.8.1