-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
50 lines (36 loc) · 1.42 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
from timeit import default_timer as timer
from itertools import tee
from typing import Callable, TypeVar, Any
T = TypeVar('T')
def default_parse_line(line: str) -> list[str]:
return line.strip().split()
def read_input(fname: str, separator: str = '\n', parse_chunk: Callable[[str], T] = default_parse_line) -> list[T]:
with open(fname) as f:
return [parse_chunk(line) for line in f.read().rstrip().split(separator)]
def timed(f, *args, **kwargs):
t1 = timer()
result = f(*args, **kwargs)
t2 = timer()
return result, t2 - t1
def sliding_window(iterable, size):
iterables = tee(iterable, size)
for i, iterator in enumerate(iterables):
for _ in range(i):
next(iterator)
return zip(*iterables)
def run(part_one, part_two, input_file):
print("PART 1")
result, time = timed(part_one, input_file)
print(f"Answer:\t{result}\nTime:\t{time*1000}ms")
print()
print(f"PART 2")
result, time = timed(part_two, input_file)
print(f"Answer:\t{result}\nTime:\t{time*1000}ms")
def run_test(part, test_input_file, expected, test_name: str = "", exit_on_fail: bool = True):
result, time = timed(part, test_input_file)
if result == expected:
print(f"Passed: {test_name} in {time*1000}ms")
else:
print(f"Failed: {test_name or test_input_file}\nExpected {expected}, got {result} in {time*1000}ms")
if exit_on_fail:
exit(1)