-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
481 lines (370 loc) · 15.1 KB
/
utils.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
import copy
import re
import math
from pathlib import Path
from functools import reduce
from queue import PriorityQueue
from copy import deepcopy
class Grid:
def __init__(self, data=None):
if data is None:
self._data = None
else:
self._data = deepcopy(data)
def __getitem__(self, item):
# Handle single integer indexing
if isinstance(item, int):
return self._data[item]
# Handle slice or integer for rows
y = slice(item[0], item[0] + 1, 1) if isinstance(item[0], int) else item[0]
y = slice(y.start if y.start is not None else 0,
y.stop if y.stop is not None else len(self._data),
y.step if y.step is not None else 1)
# Handle slice or integer for columns
x = slice(item[1], item[1] + 1, 1) if isinstance(item[1], int) else item[1]
x = slice(x.start if x.start is not None else 0,
x.stop if x.stop is not None else len(self._data[0]),
x.step if x.step is not None else 1)
# Extract the values
vals = []
for r in range(y.start, y.stop, y.step):
row = [self._data[r][c] for c in range(x.start, x.stop, x.step)]
vals.append(row)
# Return the result
if len(vals) == 1:
return vals[0] # Return a single list if one row/column
return vals
def __setitem__(self, key, value):
y = slice(key[0], key[0]+1, 1) if isinstance(key[0], int) else key[0]
x = slice(key[1], key[1]+1, 1) if isinstance(key[1], int) else key[1]
for r in range(y.start, y.stop):
for c in range(x.start, x.stop):
self._data[r][c] = value
def __eq__(self, other):
return False not in [self._data[y][x] == other._data[y][x] for y in range(self.shape[0]) for x in range(self.shape[1])]
@property
def width(self):
assert self._data is not None
return len(self._data[0])
@property
def height(self):
assert self._data is not None
return len(self._data)
@property
def shape(self):
assert self._data is not None
return self.height, self.width
def pad_border(self, value, size):
"""Add border padding of size with the given value."""
if isinstance(size, int):
size = [size] * 4
# Pad top and bottom
for _ in range(size[0]): # Top padding
self._data.insert(0, [value] * self.width)
for _ in range(size[1]): # Bottom padding
self._data.append([value] * self.width)
# Pad left and right
self._data = [[value] * size[2] + (list(r) if isinstance(r, str) else r) + [value] * size[3] for r in
self._data]
def transpose(self):
# Convert strings to lists if necessary
grid = [list(row) if isinstance(row, str) else row for row in self._data]
# Transpose the grid
return Grid(list(map(list, zip(*grid))))
def add(self, val):
"""Add value to all elements."""
self._data = [[c + val for c in row] for row in self._data]
def apply_fn(self, fn):
"""Apply the given function element-wise."""
self._data = [[fn(c) for c in r] for r in self._data]
def find(self, val):
"""Return the indices of the given value."""
return [(r, c) for r in range(self.height) for c in range(self.width) if val in self[(r, c)]]
def neighborhood(self, idx: tuple):
"""Return the values of the neighborhood (8-connected) of the given index."""
indices = []
y, x = idx
return Grid(self[max(0, y-1):min(self.height, y+2), max(0, x-1):min(self.width, x+2)])
def flatten(self):
return [self._data[r][c] for r in range(self.height) for c in range(self.width)]
def print(self, file):
for r in self._data:
file.write("".join([str(s) for s in r])+'\n')
file.write('\n')
def __str__(self):
return '\n'.join([("".join([str(s) for s in r])) for r in self._data])
@classmethod
def from_file(cls, file='input.txt', conv_fn=None):
"""Input -> matrice (grid, 2D-list).
To parse a common grid-like input, e.g.:
30373\n
25512\n
65332"""
g = cls()
g._data = parse_input(file=file, convert_fn=list)
if conv_fn is not None:
g.apply_fn(conv_fn)
return g
@classmethod
def create(cls, shape, value=0):
"""Return a Grid of given shape, with all values set to zero."""
g = cls()
if isinstance(shape, int):
shape = (shape, shape)
g._data = [[value] * shape[1] for r in range(shape[0])]
return g
@classmethod
def cross(cls, sz):
assert sz % 2, 'Mask size must be uneven.'
g = cls.create(sz)
g._data[sz // 2] = [1] * sz
for i in range(sz):
g._data[i][sz // 2] = 1
return g
class Graph:
"""A simple implementation of a graph data structure supporting directed graphs with optional weighted edges."""
def __init__(self):
self._edges = {}
def __getitem__(self, item):
"""Enables getting the weight of the edge between two vertices.
Args:
item (tuple): A tuple (u, v) representing an edge from vertex u to vertex v.
Returns:
The weight of the edge from u to v.
"""
return self._edges[item[0]][item[1]]
@property
def n_vertices(self):
"""Returns the number of vertices in the graph.
Returns:
int: The count of vertices.
"""
return len(self._edges)
def neighbors(self, vert_i):
"""Retrieves the neighboring vertices of a given vertex.
Args:
vert_i: The vertex for which neighbors are requested.
Returns:
list: A list of neighboring vertices of vert_i.
"""
return list(self._edges[vert_i])
def add_edge(self, u, v, weight=1):
"""Adds an edge from vertex u to vertex v with an optional weight.
Examples:
g = Graph()
g.add_edge(1, 2) # Adds an edge from vertex 1 to vertex 2
g.add_edge(1, 3) # Adds another edge from vertex 1 to vertex 3
g.add_edge(1, [2, 3, 4]) # Adds edges from vertex 1 to vertices 2, 3, and 4
Args:
u: The starting vertex of the edge.
v: The ending vertex of the edge. Can be a single vertex or a list of vertices.
weight (int, optional): The weight of the edge. Defaults to 1.
"""
if u not in self._edges:
self._edges[u] = {}
if isinstance(v, int):
v = [v]
for k in v:
self._edges[u][k] = weight
def print(self):
"""Prints a representation of the graph. Lists each vertex and its corresponding edges with weights."""
for e, n in self._edges.items():
print(f'{e} -> {n}')
@classmethod
def from_grid(cls, grid: 'Grid', nb_mask: 'Grid', decision_fn):
"""Convert a grid into a Graph, using the supplied neighborhood mask and decision function.
The mask defines the considered local neighborhood of each grid cell with the respective pixel in the middle.
The mask is a binary mask, with 1 stating the element should be considered, and 0 otherwise.
The decision_fn is executed on each potential grid-pair and should return a positive value (an edge weight)
if the two graph nodes are considered neighbors.
"""
new_graph = cls()
# relative neighbor coordinates
nb_coords = [(x - nb_mask.shape[0] // 2, y - nb_mask.shape[1] // 2) for x in range(nb_mask.shape[0])
for y in range(nb_mask.shape[1]) if nb_mask[(x, y)] == 1]
nb_coords.remove((0, 0))
for r in range(grid.height):
for c in range(grid.width):
for nb_coord in nb_coords:
# take care of grid borders
nb = (r+nb_coord[0], c+nb_coord[1])
if min(nb) < 0 or nb[0] >= grid.height or nb[1] >= grid.width:
continue
weight = decision_fn(grid[(r, c)], grid[nb])
if weight >= 0:
new_graph.add_edge((r, c), nb, weight)
return new_graph
class TreeNode:
"""A node in a binary tree."""
def __init__(self, value):
"""
Initialize a tree node.
Args:
value: The value of the node.
"""
self.value = value
self.left = None
self.right = None
class BinaryTree:
"""A simple binary tree."""
def __init__(self):
"""
Initialize an empty binary tree.
"""
self.root = None
self.leafs = {}
def __getitem__(self, item):
if item in self.leafs:
return self.leafs[item]
def add_leaf(self, value, children=(None, None)):
"""
Add a node to the tree with optional left and right children.
Args:
value: The value of the node to add.
children (tuple): A tuple (left_child_value, right_child_value).
Use None for no child.
Returns:
The newly created node.
"""
if value not in self.leafs:
self.leafs[value] = TreeNode(value)
node = self.leafs[value]
if not self.root:
self.root = node
left_value, right_value = children
if left_value is not None:
if left_value not in self.leafs:
self.leafs[left_value] = TreeNode(left_value) if left_value != value else node
node.left = self.leafs[left_value]
if right_value is not None:
if right_value not in self.leafs:
self.leafs[right_value] = TreeNode(right_value) if right_value != value else node
node.right = self.leafs[right_value]
return node
def left(self, parent_value):
"""
Get the left child of a given node.
Args:
parent_value: The value of the parent node.
Returns:
The child node, or None if the child does not exist.
"""
parent_node = self.leafs.get(parent_value)
if parent_node:
return parent_node.left
return None
def right(self, parent_value):
"""
Get the right child of a given node.
Args:
parent_value: The value of the parent node.
Returns:
The child node, or None if the child does not exist.
"""
parent_node = self.leafs.get(parent_value)
if parent_node:
return parent_node.right
return None
def print(self, node, level=0, prefix="Root: ", visited=None):
"""
Print the tree in a hierarchical format.
Args:
node: The current node to print.
level: The current level in the tree.
prefix: The prefix to print before the node value.
"""
if node is None:
return
if visited is None:
visited = set()
# Check for circular dependency
if node in visited:
print(" " * (level * 4) + prefix + str(node.value) + " (circular)")
return
else:
visited.add(node)
print(" " * (level * 4) + prefix + str(node.value))
self.print(node.left, level + 1, "L--- ", visited)
self.print(node.right, level + 1, "R--- ", visited)
def dijkstra(graph: 'Graph', start, end):
"""Solve shortest path using Dijkstra and a priority queue."""
dist = {v: 10000 for v in graph._edges}
dist[start] = 0
previous = {v: None for v in graph._edges}
visited = []
pq = PriorityQueue()
pq.put((0, start))
while not pq.empty():
(_, cur_v) = pq.get()
if end is not None and cur_v == end:
break
visited.append(cur_v)
for nb_i in graph.neighbors(cur_v):
if nb_i not in visited:
cost = dist[cur_v] + 1
if cost < dist[nb_i]:
pq.put((cost, nb_i))
dist[nb_i] = cost
previous[nb_i] = cur_v
return previous
def parse_input(file='input.txt', convert_fn=None, split_on='\n'):
lines = Path(file).read_text()
if split_on:
lines = lines.split(split_on)
if convert_fn:
lines = [convert_fn(x) for x in lines]
return lines
def test_results(solve1, solve2, file='solutions.txt'):
if Path(file).exists():
solutions = parse_input(file, convert_fn=int)
print('\u2705' if solve1 == solutions[0] else '\u274C ', f' {solve1} == {solutions[0]}')
print('\u2705' if solve2 == solutions[1] else '\u274C ', f' {solve2} == {solutions[1]}')
else:
print('Skipping test, no solutions found.')
def get_mask(arr, cond):
"""For a 2-d list return a binary array of the same shape (a mask) according to the given condition"""
mask = copy.deepcopy(arr)
for i in range(len(arr)):
for j in range(len(arr[0])):
mask[i][j] = cond(arr[i][j])
return mask
def l_to_i(li):
"""Map all elements of a list to int"""
return list(map(int, li))
def chunks(xs, n):
"""split list into chunks of size n"""
n = max(1, n)
return (xs[i:i+n] for i in range(0, len(xs), n))
def bits_to_dec(arr):
"""
[False, True, True] --> 3
[1, 0, 1, 0] --> 6
"0111" --> 7
[1, '0', 0, False, '1', True] --> 35
"""
return int("".join([str(int(x)) for x in arr]), 2)
def prod(numbers):
"""Return the product of all numbers in the given list."""
y = 1
for x in numbers:
y *= x
return y
def sign(x):
return -1 if x < 0 else 1
def lcm(a, b):
"""Calculate the least common multiple (LCM) for two numbers."""
return abs(a * b) // math.gcd(a, b)
def find_lcm(numbers: list):
"""Calculate the least common multiple (LCM) for a list of numbers."""
return reduce(lcm, numbers)
def get_nb_indices(y, x):
"""Generate a list of 2d neighborhood indices (8-connected) for a given center coordinate.
The center coordinate itself is excluded."""
return [(j, i) for i in range(x - 1, x + 2) for j in range(y - 1, y + 2) if (j, i) != (y, x)]
def substring_idx(text: str, substring: str):
"""Return the starting indices of all occurrences of substring in text."""
pattern = re.escape(substring)
return [match.start() for match in re.finditer(pattern, text)]
def find_pattern_in_lines(lines: list, pattern: str):
"""Return the matches of all provided regular expressions in all given lines."""
return [list(re.finditer(pattern, line)) for line in lines]