Repository navigation
Expand file tree
/
Copy pathutils.py
More file actions
60 lines (51 loc) · 1.54 KB
/
Copy pathutils.py
File metadata and controls
60 lines (51 loc) · 1.54 KB
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
import re
import math
#File input helpers
def pretty_print_answer(part, answer):
print('Answer to part {num}: {ans}'.format(num=part, ans=answer))
def read_lines(file_name, is_strip=True):
file = open(file_name)
lines = [s for s in file.readlines()]
file.close()
if is_strip:
lines = [s.strip() for s in lines]
return lines
def read_groups(file_name):
lines = read_lines(file_name)
result = []
current_group = []
while lines:
line = lines.pop(0)
if line:
current_group.append(line)
else:
result.append(current_group)
current_group = []
result.append(current_group)
return result
def read_matrix(file_name):
lines = read_lines(file_name)
result = []
for line in lines:
result.append(list(line.strip()))
return result
def split_line(regex, line):
assert re.match(regex, line), f'No match for "{regex}" in "{line}"'.format(regex=regex, line=line)
return re.match(regex, line).groups()
#Vector analysis
def manhattan_distance(loc1, loc2):
dist = 0
for dim in enumerate(loc1):
dist += abs(dim[1]-loc2[dim[0]])
return dist
def linear_distance(vec1, vec2):
assert len(vec1) == len(vec2), 'Unmatched tuple lengths'
distance = 0
for dim, val1 in enumerate(vec1):
distance += (val1 - vec2[dim])^2
return math.sqrt(distance)
#Character manipulation
def ascii_to_string(ascii_vals):
return [chr(i) for i in ascii_vals]
def string_to_ascii(string):
return [ord(c) for c in string]