Skip to content

Commit

Permalink
Added aoc new command to save on boilerplate
Browse files Browse the repository at this point in the history
  • Loading branch information
LukeMoll committed Dec 5, 2019
1 parent 3eaa76d commit 52aa72e
Showing 1 changed file with 56 additions and 1 deletion.
57 changes: 56 additions & 1 deletion aoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,59 @@ def output(part : int, func, post=None, comment=None, args=[], kwargs={}):
else:
print(f" {result}")

print()
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")
test()
aoc.output(1, part1)
aoc.output(2, part2)
def test():
pass
print("✓ All tests passed!")
def part1():
pass
def part2():
pass
if __name__ == "__main__":
main()
""")
print(f"Created day{args.day:02}.py")



0 comments on commit 52aa72e

Please sign in to comment.