-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunner.py
94 lines (73 loc) · 2.31 KB
/
runner.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
"""Advent of code puzzle runner
Author: Scoder12"""
import time
import math
from typing import Any
def format_filename(day):
# You can customize this to your liking.
return str(day).zfill(2)
def format_runtime(ms):
# You can customize this to your liking
# Microseconds
if ms <= 1:
return f"{round(ms * 1000)}µs"
# Milliseconds
if ms < 1000:
whole_ms = math.floor(ms)
rem_ms = ms - whole_ms
return f"{whole_ms}ms " + format_runtime(rem_ms)
sec = ms / 1000
# Seconds
if sec < 60:
whole_sec = math.floor(sec)
rem_ms = ms - whole_sec * 1000
return f'{whole_sec}s ' + format_runtime(rem_ms)
# Minutes (hopefully it doesn't get to this point lol)
return f"{math.floor(sec / 60)}m " + format_runtime((sec % 60)* 1000)
def run_part(part: str, mod: Any, data: str):
funcname = f'part{part}'
f = getattr(mod, funcname, None)
if callable(f):
print(f"Running Part {part}")
start = time.perf_counter()
val = f(data)
end = time.perf_counter()
print(f"Output: {val}")
rtime = (end - start) * 1000 # sec -> ms
print(f"Took {format_runtime(rtime)}\n")
return rtime
else:
print(f"No {funcname} function")
return 0
return rtime
def get_data(day):
# Try to find the filename
fname = format_filename(day) + ".txt"
try:
with open(fname, "r") as f:
data = f.read()
except Exception as e:
raise ValueError(f"Unable to read file {fname}") from e
print(f"Loaded puzzle input from {fname}\n")
return data
def run(day, year=2020):
print(f"AOC {year} Day {day}")
mod = __import__(format_filename(day))
data = get_data(day)
part1Time = run_part(1, mod, data)
part2Time = run_part(2, mod, data)
if part1Time != 0 and part2Time != 0:
print(f"Total runtime: {format_runtime(part1Time + part2Time)}")
def get_day(max_day=None):
while True:
prompt = f"Enter day" + f" (max {max_day})" if max_day else ''
inp = input(prompt + ": ")
try:
day = int(inp)
except ValueError:
print("Invalid day")
continue
if max_day and day <= max_day:
return day
else:
print(f"Must be at most {max_day}")