day11: ever so slightly faster - still 2s +/- 0.2

This commit is contained in:
Stefan Harmuth 2020-12-11 12:35:36 +01:00
parent c3cced9c4d
commit 6df1f468d3

View File

@ -35,23 +35,22 @@ def simulate(layout, max_x, max_y, wide=False):
continue continue
occupied_seats = 0 occupied_seats = 0
for cx in [-1, 0, 1]: for cx in [x-1, x, x+1]:
for cy in [-1, 0, 1]: for cy in [y-1, y, y+1]:
if cx == 0 and cy == 0: if cx == x and cy == y:
continue continue
test_x = x + cx check_value = layout[(cx, cy)]
test_y = y + cy
check_value = layout[(test_x, test_y)]
if check_value is not None: if check_value is not None:
occupied_seats += check_value occupied_seats += check_value
elif wide: elif wide:
test_x = cx + (cx - x)
test_y = cy + (cy - y)
while 0 <= test_x <= max_x and 0 <= test_y <= max_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: if layout[(test_x, test_y)] is None:
continue test_x += (cx - x)
test_y += (cy - y)
else: else:
occupied_seats += layout[(test_x, test_y)] occupied_seats += layout[(test_x, test_y)]
break break