-
Notifications
You must be signed in to change notification settings - Fork 0
/
part_b.py
executable file
·206 lines (187 loc) · 8.39 KB
/
part_b.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#!/usr/bin/env python3
from dataclasses import dataclass, field
from typing import Optional, Set, Tuple, Iterable
from utils import BaseChallenge, Point2D
from year_2016.day_01 import part_a
class Challenge(BaseChallenge):
part_a_for_testing = part_a
def solve(self, _input, debug=False):
"""
>>> Challenge().default_solve()
111
"""
return WalkerExtended.get_first_duplicate_from_text(_input)\
.manhattan_length()
@dataclass
class WalkerExtended(part_a.Walker['InstructionSetExtended']):
history: Set[Point2D] = field(default_factory=set)
duplicates: Set[Point2D] = field(default_factory=set)
@classmethod
def get_first_duplicate_from_text(cls, instructions_text: str) \
-> Optional[Point2D]:
"""
>>> WalkerExtended.get_first_duplicate_from_text('R2, L3')
>>> WalkerExtended.get_first_duplicate_from_text('R2, R2, R2')
>>> WalkerExtended.get_first_duplicate_from_text('R5, L5, R5, R3')
>>> WalkerExtended.get_first_duplicate_from_text('R8, R4, R4, R8')
Point2D(x=4, y=0)
>>> WalkerExtended.get_first_duplicate_from_text('R8, R4, R4, R8')\\
... .manhattan_length()
4
"""
instruction_set = cls.get_instruction_set_class()\
.from_instructions_text(instructions_text)
return cls().get_first_duplicate(instruction_set)
def __post_init__(self):
self.history.add(self.position)
def get_first_duplicate(
self, instruction_set: 'InstructionSetExtended') \
-> Optional[Point2D]:
"""
>>> WalkerExtended(Point2D(2, 3), part_a.Instruction.Direction.Right)\\
... .get_first_duplicate(InstructionSetExtended([
... InstructionExtended(InstructionExtended.Turn.Right, 10),
... InstructionExtended(InstructionExtended.Turn.Left, 10),
... ]))
>>> WalkerExtended(Point2D(2, 3), InstructionExtended.Direction.Right)\\
... .get_first_duplicate(InstructionSetExtended([
... InstructionExtended(InstructionExtended.Turn.Right, 10),
... InstructionExtended(InstructionExtended.Turn.Right, 0),
... InstructionExtended(InstructionExtended.Turn.Right, 10),
... ]))
Point2D(x=2, y=12)
"""
cls = type(self)
# noinspection PyArgumentList
other_walker = \
cls(self.position, self.direction, {self.position}, set())
other_walker.move(instruction_set, stop_on_duplicate=True)
if not other_walker.duplicates:
return None
duplicate, = other_walker.duplicates
return duplicate
def move(self, instruction_set: 'InstructionSetExtended',
stop_on_duplicate: bool = False):
"""
>>> walker = WalkerExtended(Point2D(2, 3), InstructionExtended.Direction.Right)\\
... .move(InstructionSetExtended([
... InstructionExtended(InstructionExtended.Turn.Right, 2),
... InstructionExtended(InstructionExtended.Turn.Right, 0),
... InstructionExtended(InstructionExtended.Turn.Left, 2),
... ]), stop_on_duplicate=True)
>>> walker
WalkerExtended(position=Point2D(x=2, y=7),
direction=Direction.Down, history={...},
duplicates=set())
>>> sorted(walker.history)
[Point2D(x=2, y=3), Point2D(x=2, y=4), Point2D(x=2, y=5),
Point2D(x=2, y=6), Point2D(x=2, y=7)]
>>> walker = WalkerExtended(Point2D(2, 3), InstructionExtended.Direction.Right)\\
... .move(InstructionSetExtended([
... InstructionExtended(InstructionExtended.Turn.Right, 2),
... InstructionExtended(InstructionExtended.Turn.Right, 0),
... InstructionExtended(InstructionExtended.Turn.Right, 2),
... ]), stop_on_duplicate=True)
>>> walker
WalkerExtended(position=Point2D(x=2, y=4),
direction=Direction.Up, history={...},
duplicates={Point2D(x=2, y=4)})
>>> sorted(walker.history)
[Point2D(x=2, y=3), Point2D(x=2, y=4), Point2D(x=2, y=5)]
"""
for position, direction \
in instruction_set.get_moves(self.position, self.direction):
if position is None:
self.direction = direction
else:
self.position, self.direction = position, direction
if self.add_position(self.position):
break
return self
def add_position(self, position: Point2D) -> bool:
if position in self.history:
self.duplicates.add(position)
return True
else:
self.history.add(position)
return False
class InstructionSetExtended(part_a.InstructionSet['InstructionExtended']):
def get_moves(
self, position: Point2D, direction: part_a.Instruction.Direction
) -> Iterable[Tuple[Optional[Point2D], part_a.Instruction.Direction]]:
"""
>>> list(InstructionSetExtended([
... InstructionExtended(InstructionExtended.Turn.Right, 2),
... InstructionExtended(InstructionExtended.Turn.Right, 0),
... InstructionExtended(InstructionExtended.Turn.Left, 2),
... ]).get_moves(Point2D(2, 3), InstructionExtended.Direction.Right))
[(Point2D(x=2, y=4), Direction.Down),
(Point2D(x=2, y=5), Direction.Down),
(None, Direction.Left),
...]
"""
for instruction in self.instructions:
for new_position, new_direction \
in instruction.get_moves(position, direction):
yield new_position, new_direction
if new_position is None:
direction = new_direction
else:
position, direction = new_position, new_direction
class InstructionExtended(part_a.Instruction):
def get_moves(
self, position: Point2D, direction: part_a.Instruction.Direction
) -> Iterable[Tuple[Optional[Point2D], part_a.Instruction.Direction]]:
"""
>>> list(InstructionExtended(
... InstructionExtended.Turn.Left, 10).get_moves(
... Point2D(2, 3), InstructionExtended.Direction.Up))
[(Point2D(x=1, y=3), Direction.Left),
(Point2D(x=0, y=3), Direction.Left),
...,
(Point2D(x=-7, y=3), Direction.Left),
(Point2D(x=-8, y=3), Direction.Left)]
>>> list(InstructionExtended(
... InstructionExtended.Turn.Left, 10).get_moves(
... Point2D(2, 3), InstructionExtended.Direction.Down))
[(Point2D(x=3, y=3), Direction.Right),
(Point2D(x=4, y=3), Direction.Right),
...,
(Point2D(x=11, y=3), Direction.Right),
(Point2D(x=12, y=3), Direction.Right)]
>>> list(InstructionExtended(
... InstructionExtended.Turn.Right, 10).get_moves(
... Point2D(2, 3), InstructionExtended.Direction.Left))
[(Point2D(x=2, y=2), Direction.Up),
(Point2D(x=2, y=1), Direction.Up),
...,
(Point2D(x=2, y=-6), Direction.Up),
(Point2D(x=2, y=-7), Direction.Up)]
>>> list(InstructionExtended(
... InstructionExtended.Turn.Right, 10).get_moves(
... Point2D(2, 3), InstructionExtended.Direction.Right))
[(Point2D(x=2, y=4), Direction.Down),
(Point2D(x=2, y=5), Direction.Down),
...,
(Point2D(x=2, y=12), Direction.Down),
(Point2D(x=2, y=13), Direction.Down)]
>>> list(InstructionExtended(
... InstructionExtended.Turn.Right, 0).get_moves(
... Point2D(2, 5), InstructionExtended.Direction.Down))
[(None, Direction.Left)]
"""
direction_index = self.LEFT_ROTATION.index(direction)
if self.turn == self.Turn.Left:
direction_index += 1
else:
direction_index -= 1
final_direction = \
self.LEFT_ROTATION[direction_index % len(self.LEFT_ROTATION)]
if not self.move_amount:
yield None, final_direction
else:
for move_amount in range(1, self.move_amount + 1):
position = position.offset(self.OFFSETS[final_direction])
yield position, final_direction
Challenge.main()
challenge = Challenge()