From f61b65581c30879a9bfa43ffafb99d4ba160cd32 Mon Sep 17 00:00:00 2001 From: Thomas Voss Date: Fri, 29 Oct 2021 23:17:45 +0200 Subject: Squash if/elif chains into a single if check --- 2015/18/puzzle-1.py | 7 ++++--- 2015/18/puzzle-2.py | 11 ++++++----- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/2015/18/puzzle-1.py b/2015/18/puzzle-1.py index ef6d36e..3fe9c8a 100755 --- a/2015/18/puzzle-1.py +++ b/2015/18/puzzle-1.py @@ -28,13 +28,14 @@ def simulate(data: list[list[str]]) -> list[list[str]]: for i in range(100): for j in range(100): - if data[i][j] == "#" and neighbours(data, i, j) in [2, 3]: - ndata[i][j] = "#" - elif data[i][j] == "." and neighbours(data, i, j) == 3: + if (data[i][j] == "#" and neighbours(data, i, j) in [2, 3]) or ( + data[i][j] == "." and neighbours(data, i, j) == 3 + ): ndata[i][j] = "#" return ndata + def main() -> None: with open("input", "r", encoding="utf-8") as f: data = [list(l.strip()) for l in f.readlines()] diff --git a/2015/18/puzzle-2.py b/2015/18/puzzle-2.py index 9a998b8..b57edb9 100755 --- a/2015/18/puzzle-2.py +++ b/2015/18/puzzle-2.py @@ -28,15 +28,16 @@ def simulate(data: list[list[str]]) -> list[list[str]]: for i in range(100): for j in range(100): - if (i, j) in [(0, 0), (0, 99), (99, 0), (99, 99)]: - ndata[i][j] = "#" - elif data[i][j] == "#" and neighbours(data, i, j) in [2, 3]: - ndata[i][j] = "#" - elif data[i][j] == "." and neighbours(data, i, j) == 3: + if ( + ((i, j) in [(0, 0), (0, 99), (99, 0), (99, 99)]) + or (data[i][j] == "#" and neighbours(data, i, j) in [2, 3]) + or (data[i][j] == "." and neighbours(data, i, j) == 3) + ): ndata[i][j] = "#" return ndata + def main() -> None: with open("input", "r", encoding="utf-8") as f: data = [list(l.strip()) for l in f.readlines()] -- cgit v1.2.3