-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.py
280 lines (221 loc) · 7.73 KB
/
util.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
from __future__ import annotations
import contextlib
import functools
import itertools
import operator
import os.path
import sys
import typing
from pprint import pprint # noqa
if typing.TYPE_CHECKING:
from collections.abc import Callable, Generator, Iterable, Sequence
from typing import TextIO
@contextlib.contextmanager
def inputfile() -> Generator[TextIO]:
if len(sys.argv) > 1:
# Use the provided argument (useful with sample data)
inputpath = sys.argv[1]
else:
if sys.argv and sys.argv[0]:
# Find inputfile based on executing script
dirpart, filepart = os.path.split(sys.argv[0])
else:
# Find inputfile based on the module calling us (useful with REPL)
import inspect
for frame in reversed(inspect.stack()):
if frame.filename != __file__ and frame.filename != "<stdin>":
dirpart, filepart = os.path.split(frame.filename)
break
else:
raise RuntimeError("No executing script")
day, _ = os.path.splitext(filepart)
day = day.rstrip("abcde")
inputpath = os.path.join(dirpart, "input", day)
with open(inputpath, "r") as f:
yield f
def readlines() -> Generator[str]:
with inputfile() as f:
return (line.strip() for line in f.readlines())
def read() -> str:
with inputfile() as f:
return f.read()
def readchunks() -> Generator[str]:
with inputfile() as f:
chunk = ""
for line in f.readlines():
if line != "\n":
chunk += line
elif chunk:
yield chunk
chunk = ""
if chunk:
yield chunk
def readlinegroups(lines_per_group) -> Generator[str]:
group = []
for line in readlines():
group.append(line)
if len(group) >= lines_per_group:
yield group
group = []
def readints() -> Generator[int]:
for line in readlines():
for n in line.split(","):
yield int(n)
def readintlines(delimiter=None) -> Generator[list[int]]:
for line in readlines():
yield [int(n) for n in line.split(delimiter)]
def prod(numbers: Iterable[int]) -> int:
return functools.reduce(operator.mul, numbers)
def count(validator: Callable, iterable: Iterable) -> int:
return sum(1 for item in iterable if item and validator(item))
def unique_product(args: Sequence[Sequence], index: int = 0) -> Generator[tuple]:
if index >= len(args):
yield tuple()
else:
for suffix in unique_product(args, index + 1):
for arg in args[index]:
if arg not in suffix:
yield (arg,) + suffix
def pairwise(iterable):
# pairwise('ABCDEFG') --> AB BC CD DE EF FG
a, b = itertools.tee(iterable)
next(b, None)
return zip(a, b)
def batched(iterable, n):
# batched('ABCDEFG', 3) --> ABC DEF G
if n < 1:
raise ValueError("n must be at least one")
it = iter(iterable)
while batch := tuple(itertools.islice(it, n)):
yield batch
class Vector(tuple):
def __new__(cls, *args):
return super().__new__(cls, args)
def __add__(self, other: tuple | int) -> Vector:
if isinstance(other, int):
return self.__class__(*(a + other for a in self))
return self.__class__(*(a + b for a, b in zip(self, other)))
def __sub__(self, other: tuple | int) -> Vector:
if isinstance(other, int):
return self.__class__(*(a - other for a in self))
return self.__class__(*(a - b for a, b in zip(self, other)))
def __mul__(self, other: tuple | int) -> Vector:
if isinstance(other, int):
return self.__class__(*(a * other for a in self))
return self.__class__(*(a * b for a, b in zip(self, other)))
class Direction(Vector):
@property
def x(self) -> int:
return self[0]
@property
def y(self) -> int:
return self[1]
@property
def z(self) -> int:
return self[2]
def rotate(self, rotation: int = 1) -> Direction:
return self.CARDINALS[
(self.CARDINALS.index(self) + rotation) % len(self.CARDINALS)
]
def rotation(self, other: Direction) -> int:
delta = self.CARDINALS.index(other) - self.CARDINALS.index(self)
if delta > 2:
delta -= 4
elif delta < -2:
delta += 4
return delta
Direction.EAST = Direction(1, 0, 0)
Direction.SOUTH = Direction(0, 1, 0)
Direction.WEST = Direction(-1, 0, 0)
Direction.NORTH = Direction(0, -1, 0)
Direction.UP = Direction(0, 0, 1)
Direction.DOWN = Direction(0, 0, -1)
Direction.NORTHEAST = Direction.NORTH + Direction.EAST
Direction.SOUTHEAST = Direction.SOUTH + Direction.EAST
Direction.SOUTHWEST = Direction.SOUTH + Direction.WEST
Direction.NORTHWEST = Direction.NORTH + Direction.WEST
Direction.CARDINALS = (Direction.EAST, Direction.SOUTH, Direction.WEST, Direction.NORTH)
Direction.ORDINALS = (
Direction.SOUTHEAST,
Direction.SOUTHWEST,
Direction.NORTHWEST,
Direction.NORTHEAST,
)
Direction.CARDINALS_3D = Direction.CARDINALS + (Direction.UP, Direction.DOWN)
class Position(Direction):
def __sub__(self, other: tuple | int) -> Vector:
if isinstance(other, Position):
return Direction(self.x - other.x, self.y - other.y, 0)
return super().__sub__(other)
@property
def cardinals(self) -> Generator[Position]:
for direction in self.CARDINALS:
yield self + direction
@property
def ordinals(self) -> Generator[Position]:
for direction in self.ORDINALS:
yield self + direction
@property
def neighbours(self) -> Generator[Position]:
for direction in itertools.product(*((0, 1, -1),) * 2):
if direction != (0, 0):
yield self + direction
@property
def cardinals_3d(self) -> Generator[Position]:
for pos in self.cardinals:
yield pos
yield self + self.UP
yield self + self.DOWN
@property
def neighbours_3d(self) -> Generator[Position]:
for direction in itertools.product(*((0, 1, -1),) * 3):
if direction != (0, 0, 0):
yield self + direction
def manhattan_distance(self, other: Position) -> int:
return abs(self.x - other.x) + abs(self.y - other.y)
class Grid(dict):
height = None
width = None
@classmethod
def from_iterable(cls, rows, cast=None, ignore=None):
grid = cls()
for y, row in enumerate(rows):
for x, value in enumerate(row):
if ignore and value in ignore:
continue
if cast:
value = cast(value)
if value is None:
continue
grid[Position(x, y)] = value
grid.width = x + 1
grid.height = y + 1
return grid
def index(self, other):
for pos, value in self.items():
if value == other:
return pos
raise ValueError(f"{other} is not in Grid")
def is_inside(self, pos: Position):
return 0 <= pos.x < self.width and 0 <= pos.y < self.height
@property
def inner(self):
return (
Position(x, y)
for y in range(1, self.height - 1)
for x in range(1, self.width - 1)
)
def rotate(self):
copy = self.copy()
self.clear()
for pos, value in copy.items():
self[Position(self.height - 1 - pos.y, pos.x)] = value
self.height, self.width = self.width, self.height
OPERATORS = {
"+": operator.add,
"-": operator.sub,
"*": operator.mul,
"/": operator.floordiv,
"<": operator.lt,
">": operator.gt,
}