Small 2018 Day 13 cleanup

This commit is contained in:
Luke Hubmayer-Werner 2018-12-13 21:19:56 +10:30
parent 8e721059e4
commit ba7f08b6ca
1 changed files with 9 additions and 24 deletions

View File

@ -32,41 +32,26 @@ for y, line in enumerate(data):
minecarts.append([x, y, carts[char][0], 0]) minecarts.append([x, y, carts[char][0], 0])
while len(minecarts) > 1: while len(minecarts) > 1:
minecarts = sorted(minecarts, key=lambda c: c[1]*200+c[0]) minecarts = sorted(minecarts, key=lambda c: (c[1], c[0]))
i = 0 i = 0
while i < len(minecarts): while i < len(minecarts):
mc = minecarts[i] mc = minecarts[i]
next_index = list(mc[0:2]+directions[mc[2]]) mc[:2] = list(mc[:2]+directions[mc[2]])
g = grid[tuple(next_index)] g = grid[tuple(mc[:2])]
mc[0:2] = next_index if g in (1, 2):
if g == 1 or g == 2:
pass pass
elif g == 4: elif g == 4:
if mc[2] == 0: mc[2] = (3, 2, 1, 0)[mc[2]]
mc[2] = 3
elif mc[2] == 1:
mc[2] = 2
elif mc[2] == 2:
mc[2] = 1
elif mc[2] == 3:
mc[2] = 0
elif g == 5: elif g == 5:
if mc[2] == 0: mc[2] = (1, 0, 3, 2)[mc[2]]
mc[2] = 1
elif mc[2] == 1:
mc[2] = 0
elif mc[2] == 2:
mc[2] = 3
elif mc[2] == 3:
mc[2] = 2
elif g == 3: elif g == 3:
mc[2] = (mc[2]+turns[mc[3]]) % 4 mc[2] = (mc[2]+turns[mc[3]]) % 4
mc[3] = (mc[3]+1) % 3 mc[3] = (mc[3]+1) % 3
for j, mc2 in enumerate(minecarts): for j, mc2 in enumerate(minecarts):
if mc2 == mc: if mc2 == mc:
continue continue
if np.array_equal(mc[0:2], mc2[0:2]): if np.array_equal(mc[:2], mc2[:2]):
print('Crash at', mc[0:2]) print('Crash at', mc[:2])
if j > i: if j > i:
minecarts.pop(j) minecarts.pop(j)
minecarts.pop(i) minecarts.pop(i)