From c3cced9c4d7132b148e2ac473773899edeae4ae8 Mon Sep 17 00:00:00 2001 From: Stefan Harmuth Date: Fri, 11 Dec 2020 11:58:35 +0100 Subject: [PATCH] day11: down to ~2s (and found the flaw in p2) --- day11.py | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/day11.py b/day11.py index 31698f6..d202087 100644 --- a/day11.py +++ b/day11.py @@ -28,7 +28,6 @@ def prepareLayout(raw_input): def simulate(layout, max_x, max_y, wide=False): max_seats_occupied = (5 if wide else 4) - max_xy = max(max_x, max_y) new_layout = defaultdict(lambda: None) for x in range(0, max_x): for y in range(0, max_y): @@ -41,15 +40,16 @@ def simulate(layout, max_x, max_y, wide=False): if cx == 0 and cy == 0: continue - if not wide: - if layout[(x + cx, y + cy)]: - occupied_seats += 1 - else: - for d in range(1, max_xy): - test_x = d * cx + x - test_y = d * cy + y - if test_x >= max_x or test_y >= max_y: - break + test_x = x + cx + test_y = y + cy + check_value = layout[(test_x, test_y)] + + if check_value is not None: + occupied_seats += check_value + elif wide: + 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 else: @@ -73,6 +73,9 @@ def part1(test_mode=False, wide=False): curr_occupied = 0 last_occupied = -1 generation_counter = 0 + + # Yes, this is relying on the assumption that there are no + # two different patterns with the same number of occupants. while curr_occupied != last_occupied: last_occupied = curr_occupied generation_counter += 1