-
Notifications
You must be signed in to change notification settings - Fork 0
/
aoc.py
146 lines (98 loc) · 2.91 KB
/
aoc.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
import pathlib
import re
from pprint import pprint
from termcolor import colored
PREVIEW_LIMIT = 1021
PREVIEW_LIMIT_LIST = 8
## Print Helper
def red(args):
return colored(args, "red")
def green(args):
return colored(args, "green")
def blue(args):
return colored(args, "blue")
def preview_input(data, shorten = True):
print("Input:")
if isinstance(data, list):
if shorten:
pprint(data[:PREVIEW_LIMIT_LIST])
if len(data) > PREVIEW_LIMIT_LIST:
print(red("!!!"), len(data) - 8, red("entries redacted !!!"))
else:
print()
pprint(data)
else:
print(f"{data[:min(len(data), PREVIEW_LIMIT)]}", red("...") if len(data) > PREVIEW_LIMIT else "", sep = "")
print("-" * 64)
## IO Helper
def input_from_file(root_file):
directory = pathlib.Path(root_file).parent.resolve()
try:
with open(f"{directory}/input") as file:
raw = file.readlines()
except Exception as e:
return str(e)
p_newline = re.compile(r"[\n\r]")
return [p_newline.sub("", x) for x in raw]
def string_to_list(s, regex = r"[\n\r]"):
p_newline = re.compile(regex)
return p_newline.split(s)
## Converison
def map2(type, ls):
return [type(x) for x in ls]
def entries_as_tuples(ls, types = None):
if types:
for item in ls:
for i, t in enumerate(types):
item[i] = t(item[i])
return [tuple(x) for x in ls]
def entries_as_dicts(ls, regex = r":", types = (str, str)):
out = []
pattern_split = re.compile(regex)
for entry in ls:
d = dict()
for raw in entry:
k, v = pattern_split.split(raw)
if types:
k = types[0](k)
v = types[1](v)
d[k] = v
out.append(d)
return out
def map_split(ls, regex):
pattern = re.compile(regex)
return [pattern.split(x) for x in ls]
def map_sub(ls, regex, target):
pattern = re.compile(regex)
return [pattern.sub(target, x) for x in ls]
def map_join(ls, join = " "):
return [join.join(x) for x in ls]
def map_inner2(type, ls):
return [[type(x) for x in l] for l in ls]
def group_contents(ls, sep = None):
out = []
current = []
for item in ls:
if sep is not None and item == sep:
out.append(current)
current = []
else:
current.append(item)
out.append(current)
return out
def split(s, regex):
p_split = re.compile(regex)
return p_split.split(s)
def foldr(f, out, xs):
for x in xs:
out = f(x, out)
return out
def map_split_to_tuple(xs, regex, types = (str,str)):
pattern = re.compile(regex)
for i,x in enumerate(xs):
m = pattern.match(x)
values = list(m.groups())
for j,t in enumerate(types):
values[j] = t(values[j])
xs[i] = tuple(values)
return xs