day11: down to ~2s (and found the flaw in p2)

This commit is contained in:
Stefan Harmuth 2020-12-11 11:58:35 +01:00
parent 4dc83760b7
commit c3cced9c4d

View File

@ -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