-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaoc.py
110 lines (88 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
import sys, os, shutil, time
import __main__
def get_input():
basename = __main__.__file__.replace(".py", ".txt")
input_fn = os.path.join(
os.path.dirname(__file__),
"input",
basename
)
if not os.path.exists(input_fn):
print(f"\n{basename} not found, have you downloaded it?")
exit(1)
return open(input_fn)
def header(subtitle : str):
day = __main__.__file__.replace("day", "").replace(".py", "")
title = f"Advent of Code 2019 – Day {int(day)}"
width = min(shutil.get_terminal_size().columns, 55)
print(f" ╭{'─' * (width - 3)}╮")
print(f" │ {{:^{width - 5}}} │".format(title))
print(f" │ {{:^{width - 5}}} │".format(subtitle))
print(f" ╰{'─' * (width - 3)}╯")
print()
def output(part : int, func, post=None, output=None, comment=None, args=[], kwargs={}):
print(f"⧖ Part {part}", end="", flush=True)
t0 = time.perf_counter()
result = func(*args, **kwargs)
t1 = time.perf_counter()
print(f"\r✓ Part {part}", flush=True)
print(f" Elapsed: {(t1-t0)*1000:>10.3f} ms")
if comment is not None: print(f" {comment}")
if output is None: output = lambda r: print(f" {r}")
if post is not None:
output(post(result))
else:
output(result)
print()
return result
def run_tests():
try:
t0 = time.perf_counter()
__main__.test()
t1 = time.perf_counter()
print("✓ All tests passed!")
print(f" Elapsed: {(t1-t0)*1000:>10.3f} ms")
except AssertionError as e:
print(f"✗ Tests failed!\n")
raise e
print()
def __find_next_day():
days = map(
lambda fn: int(os.path.basename(fn).replace("day","").replace(".py","")),
filter(
lambda fn: fn.startswith("day"),
os.listdir(os.path.dirname(__file__))
)
)
return max(days) + 1
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser()
subparser = parser.add_subparsers(dest="command")
parser_new = subparser.add_parser("new", help="Create a file for a new day")
parser_new.add_argument("day", type=int, nargs="?", default=__find_next_day(), help="Day to create (defaults to next day that doesn't exist)")
args = parser.parse_args()
if args.command == "new":
filename = os.path.join(os.path.dirname(__file__), f"day{args.day:02}.py")
if os.path.exists(filename):
print(f"day{args.day:02}.py exists, overwrite? (y/N)")
if input().lower() != "y":
exit()
with open(filename, 'w') as fd:
fd.write(
"""import aoc
def main():
aoc.header("Your title here")
aoc.run_tests()
# aoc.output(1, part1)
# aoc.output(2, part2)
def test():
pass
def part1():
pass
def part2():
pass
if __name__ == "__main__":
main()
""")
print(f"Created day{args.day:02}.py")