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): def simulate(layout, max_x, max_y, wide=False):
max_seats_occupied = (5 if wide else 4) max_seats_occupied = (5 if wide else 4)
max_xy = max(max_x, max_y)
new_layout = defaultdict(lambda: None) new_layout = defaultdict(lambda: None)
for x in range(0, max_x): for x in range(0, max_x):
for y in range(0, max_y): 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: if cx == 0 and cy == 0:
continue continue
if not wide: test_x = x + cx
if layout[(x + cx, y + cy)]: test_y = y + cy
occupied_seats += 1 check_value = layout[(test_x, test_y)]
else:
for d in range(1, max_xy): if check_value is not None:
test_x = d * cx + x occupied_seats += check_value
test_y = d * cy + y elif wide:
if test_x >= max_x or test_y >= max_y: while 0 <= test_x <= max_x and 0 <= test_y <= max_y:
break test_x += cx
test_y += cy
if layout[(test_x, test_y)] is None: if layout[(test_x, test_y)] is None:
continue continue
else: else:
@ -73,6 +73,9 @@ def part1(test_mode=False, wide=False):
curr_occupied = 0 curr_occupied = 0
last_occupied = -1 last_occupied = -1
generation_counter = 0 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: while curr_occupied != last_occupied:
last_occupied = curr_occupied last_occupied = curr_occupied
generation_counter += 1 generation_counter += 1