-
Notifications
You must be signed in to change notification settings - Fork 0
/
day13_mineCartMadness.py
72 lines (67 loc) · 2.7 KB
/
day13_mineCartMadness.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
from time import time
t0 = time()
puzzle = [line[:-1] for line in open("puzzle.txt")]
def subterraneanSustainability(grid):
# Simulation of the entire process
# O(n^2 * k), n being the number of carts on the map and k being the number of rounds simulated.
# Time: ~120ms
def build(track):
carts = []
grid = [list(i) for i in track]
cartID = 0
for x, row in enumerate(track):
for y, col in enumerate(row):
if col in ">v^<":
carts.append([x, y, col, str(cartID)])
cartID += 1
if col in "><":
d = "-"
elif col in "^v":
d = "|"
grid[x][y] = d
return carts, grid
carts, grid = build(grid)
directions = {cart[-1]: 0 for cart in carts}
removed = set()
firstCollision = True
DIRECTION = {"^": [-1, 0],
"v": [1, 0],
"<": [0, -1],
">": [0, 1]}
while len(carts) > 1:
newCarts = []
currentCarts = carts[:]
cartPosition = 0
for x, y, d, cartID in carts:
# Update coordinates
mx, my = DIRECTION[d]
x += mx
y += my
# Change directions
if grid[x][y] == "\\":
d = dict([["v", ">"], ["^", "<"], [">", "v"], ["<", "^"]])[d]
elif grid[x][y] == "/":
d = dict([["v", "<"], ["^", ">"], [">", "^"], ["<", "v"]])[d]
elif grid[x][y] == "+":
left = dict([["v", ">"], ["^", "<"], [">", "^"], ["<", "v"]])[d]
right = dict([["v", "<"], ["^", ">"], [">", "v"], ["<", "^"]])[d]
d = (left + d + right)[directions[cartID] % 3]
directions[cartID] += 1
# Check for collisions
for cartB in currentCarts:
if cartB[-1] in removed or cartID in removed or cartB[-1] == cartID:
continue
if cartB[:2] == [x, y]:
removed |= {cartID, cartB[-1]}
if firstCollision:
firstCollision = False
print("Part A: %d,%d" % (y, x))
if cartID not in removed:
newCarts.append([x, y, d, cartID])
currentCarts[cartPosition] = [x, y, d, cartID]
cartPosition += 1
# Update the carts to next round, sort to keep intended processing order
carts = sorted(newCarts)
return [[cart[1], cart[0]] for cart in newCarts if cart[-1] not in removed][0]
print("Part B: %d,%d" % tuple(subterraneanSustainability(puzzle)))
print("Time: %d ms" % (1000 * (time() - t0)))