-
Notifications
You must be signed in to change notification settings - Fork 142
/
Copy pathd13.py
146 lines (118 loc) · 3.63 KB
/
d13.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
from dataclasses import dataclass
from typing import Optional, List, Any
with open('./13/input') as f:
lines = [list(row.rstrip('\n')) for row in f]
@dataclass
class Vector:
x: int
y: int
def __add__(self, other: 'Vector') -> 'Vector':
return Vector(self.x + other.x, self.y + other.y)
def __hash__(self):
return hash((self.x, self.y))
def __eq__(self, other):
return (self.x, self.y) == (other.x, other.y)
@dataclass
class Direction:
vector: Vector
next: Optional['Direction'] = None
def __hash__(self):
return hash(self.vector)
def __eq__(self, other):
return self.vector == other.vector
def __repr__(self):
return str(self.vector)
left = Direction(Vector(-1, 0))
right = Direction(Vector(1, 0))
up = Direction(Vector(0, -1))
down = Direction(Vector(0, 1))
left.next = up
up.next = right
right.next = down
down.next = left
@dataclass
class Cart:
id: int
position: Vector
direction: Direction
crashed = False
_turns = 0
def turn(self, rail):
if rail not in rail_turns:
return
if rail == '+':
turns = self._turns % 3
if turns == 0:
self.direction = self.direction.next.next.next # turn left
if turns == 1:
pass # continue
if turns == 2:
self.direction = self.direction.next # turn right
self._turns += 1
else:
self.direction = corner_turns[(self.direction, rail)]
def move(self):
self.position = self.position + self.direction.vector
arrows_to_directions = {
'v': down,
'^': up,
'<': left,
'>': right,
}
directions_to_arrows = {v: k for k, v in arrows_to_directions.items()}
arrows = set(arrows_to_directions.keys())
track_beneath_cart: Any = {
'v': '|',
'^': '|',
'<': '-',
'>': '-',
}
corner_turns = {
(left, '\\'): up,
(left, '/'): down,
(right, '\\'): down,
(right, '/'): up,
(up, '\\'): left,
(up, '/'): right,
(down, '\\'): right,
(down, '/'): left,
}
# track_beneath_cart = {
# arrows_to_directions[k]: v for k, v in track_beneath_cart.items()}
rail_turns = set(['\\', '/', '+'])
class Mine:
def __init__(self, track: List[List[str]]):
id = 1
self.track = [[t for t in row] for row in track]
carts: List[Cart] = []
for y, row in enumerate(self.track):
for x, val in enumerate(row):
if val in arrows:
carts.append(
Cart(id, Vector(x, y), arrows_to_directions[val]))
id += 1
self.track[y][x] = track_beneath_cart[val]
self.carts = carts
def __repr__(self):
return '\n'.join(''.join(t) for t in self.track)
def run(self):
while True:
if len(self.carts) == 1:
print('last cart at: ', self.carts[0].position)
return
for cart in self.carts:
if cart.crashed:
continue
cart.move()
nextpos = cart.position
for k in self.carts:
if k.position == nextpos and k.id != cart.id:
print(f'{k.id} has crashed with {cart.id} at {nextpos}')
k.crashed = True
cart.crashed = True
rail = self.track[nextpos.y][nextpos.x]
cart.turn(rail)
self.carts = sorted([k for k in self.carts if k.crashed is False], key=lambda c: (
c.position.y, c.position.x))
mine = Mine(lines)
mine.run()