Skip to content

Commit 52aa72e

Browse files
committed
Added aoc new command to save on boilerplate
1 parent 3eaa76d commit 52aa72e

File tree

1 file changed

+56
-1
lines changed

1 file changed

+56
-1
lines changed

aoc.py

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,59 @@ def output(part : int, func, post=None, comment=None, args=[], kwargs={}):
3939
else:
4040
print(f" {result}")
4141

42-
print()
42+
print()
43+
44+
def __find_next_day():
45+
days = map(
46+
lambda fn: int(os.path.basename(fn).replace("day","").replace(".py","")),
47+
filter(
48+
lambda fn: fn.startswith("day"),
49+
os.listdir(os.path.dirname(__file__))
50+
)
51+
)
52+
return max(days) + 1
53+
54+
55+
if __name__ == "__main__":
56+
import argparse
57+
parser = argparse.ArgumentParser()
58+
subparser = parser.add_subparsers(dest="command")
59+
60+
parser_new = subparser.add_parser("new", help="Create a file for a new day")
61+
parser_new.add_argument("day", type=int, nargs="?", default=__find_next_day(), help="Day to create (defaults to next day that doesn't exist)")
62+
63+
args = parser.parse_args()
64+
if args.command == "new":
65+
filename = os.path.join(os.path.dirname(__file__), f"day{args.day:02}.py")
66+
if os.path.exists(filename):
67+
print(f"day{args.day:02}.py exists, overwrite? (y/N)")
68+
if input().lower() != "y":
69+
exit()
70+
with open(filename, 'w') as fd:
71+
fd.write(
72+
"""import aoc
73+
74+
def main():
75+
aoc.header("Your title here")
76+
test()
77+
78+
aoc.output(1, part1)
79+
aoc.output(2, part2)
80+
81+
def test():
82+
pass
83+
print("✓ All tests passed!")
84+
85+
def part1():
86+
pass
87+
88+
def part2():
89+
pass
90+
91+
if __name__ == "__main__":
92+
main()
93+
""")
94+
print(f"Created day{args.day:02}.py")
95+
96+
97+

0 commit comments

Comments
 (0)