From 5d118fdb4afb3974bed49d8bad17c4d9822ef5af Mon Sep 17 00:00:00 2001 From: Maciej Jur Date: Sat, 4 Dec 2021 11:12:43 +0100 Subject: [PATCH] [2021] day 4 python numpy --- 2021/Python/day04.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/2021/Python/day04.py b/2021/Python/day04.py index cc0fb9e..f394051 100644 --- a/2021/Python/day04.py +++ b/2021/Python/day04.py @@ -20,9 +20,19 @@ def solve1() -> int: for matrix in tensor: mask = np.isin(matrix, vector[:scale]) if mask.all(axis=0).any() or mask.all(axis=1).any(): - return (matrix * ~mask).sum() * vector[:scale][-1] + return (matrix * ~mask).sum() * vector[scale-1] +def solve2() -> int: + vector, tensor = loader_np() + for scale in range(len(vector)+1, 0, -1): + for matrix in tensor: + mask = np.isin(matrix, vector[:scale]) + if not mask.all(axis=0).any() and not mask.all(axis=1).any(): + prev_mask = np.isin(matrix, vector[:scale+1]) + return (matrix * ~prev_mask).sum() * vector[scale] + if __name__ == '__main__': - print(solve1()) \ No newline at end of file + print(solve1()) # 67716 + print(solve2()) # 1830