-
Notifications
You must be signed in to change notification settings - Fork 0
/
part_b.py
executable file
·153 lines (122 loc) · 4.03 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
#!/usr/bin/env python3
import re
from abc import ABC
from dataclasses import dataclass
from typing import ClassVar
from aox.challenge import Debugger
from utils import BaseChallenge, Point3D, PolymorphicParser, Point2D
from year_2021.day_02.part_a import InstructionSet, Position
class Challenge(BaseChallenge):
def solve(self, _input, debugger: Debugger):
"""
>>> Challenge().default_solve()
1599311480
"""
position = InstructionSetExtended\
.from_instructions_text(_input)\
.travel()
return position.x * position.y
class InstructionSetExtended(InstructionSet["InstructionExtended"]):
def travel(self) -> "Position":
return self.travel_from(Position.get_zero_point())
def travel_from(self, position: "Position") -> "Position":
"""
>>> InstructionSetExtended.from_instructions_text(
... "forward 5\\n"
... "down 5\\n"
... "forward 8\\n"
... "up 3\\n"
... "down 8\\n"
... "forward 2\\n"
... ).travel_from(Point2D.get_zero_point())
Point2D(x=15, y=60)
"""
current = AimedPosition(position.x, position.y, 0)
for instruction in self.instructions:
current = instruction.travel(current)
return Position(current.x, current.y)
AimedPosition = Point3D
class InstructionExtended(PolymorphicParser, ABC, root=True):
"""
>>> InstructionExtended.parse("forward 5")
ForwardInstructionExtended(offset=5)
>>> InstructionExtended.parse("down 5")
DownInstruction(aim_offset=5)
>>> InstructionExtended.parse("up 5")
UpInstruction(aim_offset=-5)
"""
def travel(self, position: AimedPosition) -> AimedPosition:
raise NotImplementedError()
@InstructionExtended.register
@dataclass
class ForwardInstructionExtended(InstructionExtended):
"""
>>> ForwardInstructionExtended.try_parse("forward 5")
ForwardInstructionExtended(offset=5)
"""
name = "forward"
offset: int
re_instruction = re.compile(r"^([^\s]+) (\d+)$")
@classmethod
def try_parse(cls, text: str):
match = cls.re_instruction.match(text)
if not match:
return None
direction, amount_str = match.groups()
if direction != cls.name:
return None
amount = int(amount_str)
# noinspection PyArgumentList
return cls(offset=amount)
def travel(self, position: AimedPosition) -> AimedPosition:
"""
>>> ForwardInstructionExtended(5).travel(Point3D(0, 0, 0))
Point3D(x=5, y=0, z=0)
>>> ForwardInstructionExtended(5).travel(Point3D(0, 0, 3))
Point3D(x=5, y=15, z=3)
"""
return AimedPosition(
x=position.x + self.offset,
y=position.y + position.z * self.offset,
z=position.z,
)
@dataclass
class AimInstruction(InstructionExtended, ABC):
unit_aim_offset: ClassVar[int]
aim_offset: int
re_instruction = re.compile(r"^([^\s]+) (\d+)$")
@classmethod
def try_parse(cls, text: str):
match = cls.re_instruction.match(text)
if not match:
return None
direction, amount_str = match.groups()
if direction != cls.name:
return None
amount = int(amount_str)
# noinspection PyArgumentList
return cls(aim_offset=cls.unit_aim_offset * amount)
def travel(self, position: AimedPosition) -> AimedPosition:
return AimedPosition(
x=position.x,
y=position.y,
z=position.z + self.aim_offset,
)
@InstructionExtended.register
class DownInstruction(AimInstruction):
"""
>>> DownInstruction.try_parse("down 5")
DownInstruction(aim_offset=5)
"""
name = "down"
unit_aim_offset = 1
@InstructionExtended.register
class UpInstruction(AimInstruction):
"""
>>> UpInstruction.try_parse("up 5")
UpInstruction(aim_offset=-5)
"""
name = "up"
unit_aim_offset = -1
Challenge.main()
challenge = Challenge()