-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
34 lines (27 loc) · 904 Bytes
/
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
from typing import List, Type, Generator
from timeit import default_timer as timer
def read_input(fname: str, types: List[Type] = None) -> Generator:
with open(fname) as f:
lines = (line.strip().split() for line in f.readlines())
if types is None:
yield from lines
else:
if len(types) > 1:
for line in lines:
yield tuple(typ(val) for typ, val in zip(types, line))
else:
for line in lines:
yield types[0](line[0])
def timed(f, *args, **kwargs):
t1 = timer()
result = f(*args, **kwargs)
t2 = timer()
return result, t2 - t1
def run(part_one, part_two):
print("PART 1")
result, time = timed(part_one)
print(f"Answer:\t{result}\nTime:\t{time*1000}ms")
print()
print(f"PART 2")
result, time = timed(part_two)
print(f"Answer:\t{result}\nTime:\t{time*1000}ms")