From 6df1f468d33841a0bb5c5ab26025c046c555a984 Mon Sep 17 00:00:00 2001 From: Stefan Harmuth Date: Fri, 11 Dec 2020 12:35:36 +0100 Subject: [PATCH] day11: ever so slightly faster - still 2s +/- 0.2 --- day11.py | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/day11.py b/day11.py index d202087..7b48fae 100644 --- a/day11.py +++ b/day11.py @@ -35,23 +35,22 @@ def simulate(layout, max_x, max_y, wide=False): continue occupied_seats = 0 - for cx in [-1, 0, 1]: - for cy in [-1, 0, 1]: - if cx == 0 and cy == 0: + for cx in [x-1, x, x+1]: + for cy in [y-1, y, y+1]: + if cx == x and cy == y: continue - test_x = x + cx - test_y = y + cy - check_value = layout[(test_x, test_y)] + check_value = layout[(cx, cy)] if check_value is not None: occupied_seats += check_value elif wide: + test_x = cx + (cx - x) + test_y = cy + (cy - y) while 0 <= test_x <= max_x and 0 <= test_y <= max_y: - test_x += cx - test_y += cy if layout[(test_x, test_y)] is None: - continue + test_x += (cx - x) + test_y += (cy - y) else: occupied_seats += layout[(test_x, test_y)] break