• API
• FAQ
• Tools
• Archive
daily pastebin goal
61%
SHARE
TWEET

# Untitled

a guest Dec 13th, 2018 67 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. raw = r"""/->-\
2. |   |  /----\
3. | /-+--+-\  |
4. | | |  | v  |
5. \-+-/  \-+--/
6.   \------/  """
7.
8. raw = r"""/>-<\
9. |   |
10. | /<+-\
11. | | | v
12. \>+</ |
13.   |   ^
14.   \<->/"""
15.
17.
18.
19. DOWN, UP, LEFT, RIGHT = list("v^<>")
20. G_LEFT, G_STRAIGHT, G_RIGHT = range(3)
21. DELTA = {DOWN: (0, 1), UP: (0, -1), LEFT: (-1, 0), RIGHT: (1, 0)}
22.
23.
25.     track = []
26.     cars = {}
27.     for y, line in enumerate(raw.split("\n")):
28.         row = list(line)
29.         for x, direction in enumerate(row):
30.             if direction in (DOWN, UP, LEFT, RIGHT):
31.                 cars[len(cars)] = [x, y, direction, G_LEFT]
32.                 if direction in (LEFT, RIGHT):
33.                     row[x] = "-"
34.                 else:
35.                     row[x] = "|"
36.         track.append(row)
37.     return track, cars
38.
39.
40. def turn(street, direction, turning):
41.     if street == "+":
42.         if turning == G_LEFT:
43.             if direction == LEFT:
44.                 direction = DOWN
45.             elif direction == RIGHT:
46.                 direction = UP
47.             elif direction == DOWN:
48.                 direction = RIGHT
49.             else:
50.                 direction = LEFT
51.         elif turning == G_RIGHT:
52.             if direction == LEFT:
53.                 direction = UP
54.             elif direction == RIGHT:
55.                 direction = DOWN
56.             elif direction == DOWN:
57.                 direction = LEFT
58.             else:
59.                 direction = RIGHT
60.         turning = (turning + 1) % 3
61.     elif street == "/":
62.         if direction == LEFT:
63.             direction = DOWN
64.         elif direction == RIGHT:
65.             direction = UP
66.         elif direction == DOWN:
67.             direction = LEFT
68.         else:
69.             direction = RIGHT
70.     elif street == "\\":
71.         if direction == LEFT:
72.             direction = UP
73.         elif direction == RIGHT:
74.             direction = DOWN
75.         elif direction == DOWN:
76.             direction = RIGHT
77.         else:
78.             direction = LEFT
79.     return direction, turning
80.
81.
82. def move(car, track):
83.     x, y, direction, turning = car
84.     delta = DELTA.get(direction, (0, 0))
85.     x, y = x + delta[0], y + delta[1]
86.     street = track[y][x]
87.     if street in "+/\\":
88.         direction, turning = turn(street, direction, turning)
89.     car[0:4] = x, y, direction, turning
90.
91.
92. def draw(track, cars):
93.     print("=" * 10)
94.     for y, row in enumerate(track):
95.         line = ""
96.         for x, c in enumerate(row):
97.             for c_x, c_y, direction, turning in cars.values():
98.                 if (c_x, c_y) == (x, y):
99.                     c = direction
100.                     break
101.             line += c
102.         print("{} {}".format(str(y).zfill(3), line))
103.     print("=" * 10)
104.
105.
107. tick, first_crash = 0, True
108.
109. while True:
110.     tick += 1
111.     positions = dict((((x, y), car) for car, (x, y, direction, turning) in cars.items()))
112.     to_remove = set()
113.     for car, (x, y, direction, turning) in cars.copy().items():
114.         move(cars[car], track)
115.         position = (cars[car][0], cars[car][1])
116.         if position in positions:
117.             if first_crash:
118.                 print("{},{}".format(*position))
119.                 first_crash = False