-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.py
72 lines (51 loc) · 1.88 KB
/
template.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
from collections import defaultdict
from itertools import permutations, combinations
from copy import deepcopy
# region utility
def group(seq, sep):
ret = []
for el in seq:
if el == sep:
yield ret
ret = []
else:
ret.append(el)
yield ret
def chunks(lst: list, n: int):
"""source: https://stackoverflow.com/questions/312443"""
for i in range(0, len(lst), n):
yield lst[i:i + n]
def neighbors4raw(r: int, c: int) -> list[tuple[int, int]]:
return [(r + 1, c), (r - 1, c), (r, c + 1), (r, c - 1)]
def neighbors4(r: int, c: int, r_max: int, c_max: int) -> list[tuple[int, int]]:
return [p for p in [
(r + 1, c), (r - 1, c), (r, c + 1), (r, c - 1),
] if 0 <= p[0] < r_max and 0 <= p[1] < c_max]
def neighbors8raw(r: int, c: int) -> list[tuple[int, int]]:
return [
(r + 1, c), (r - 1, c), (r, c + 1), (r, c - 1),
(r + 1, c + 1), (r - 1, c - 1), (r - 1, c + 1), (r + 1, c - 1),
]
def neighbors8(r: int, c: int, r_max: int, c_max: int) -> list[tuple[int, int]]:
return [p for p in [
(r + 1, c), (r - 1, c), (r, c + 1), (r, c - 1),
(r + 1, c + 1), (r - 1, c - 1), (r - 1, c + 1), (r + 1, c - 1),
] if 0 <= p[0] < r_max and 0 <= p[1] < c_max]
def dist(p1: tuple[int, int], p2: tuple[int, int]) -> int:
return abs(p1[0] - p2[0]) + abs(p1[1] - p2[1])
def inter(a_start: int, a_end: int, b_start: int, b_end: int) -> tuple[int, int] | None:
"""intersection of [a_start, a_end] and [b_start, b_end]"""
start = max(a_start, b_start)
end = min(a_end, b_end)
return None if start > end else (start, end)
def sign(n: int) -> int:
if n == 0:
return 0
return -1 if n < 0 else 1
# endregion
with open("input.txt") as read:
arr = []
for v, g in enumerate(group(read.readlines(), "\n")):
for i in g:
pass
print(arr)