-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaoc_init.py
89 lines (73 loc) · 2.67 KB
/
aoc_init.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
import sys
import os
import subprocess
try:
from aocd.models import Puzzle
except ImportError:
print("Please install aocd: pip install advent-of-code-data")
sys.exit(1)
def build_folder(day):
# create folder
folder = "day-{}".format(day)
print("Creating folder: {}".format(folder))
# create files
files = ["test.txt", "input.txt", "{}.py".format(day)]
try:
os.mkdir(folder)
except FileExistsError:
print("Folder {} already exists".format(folder))
for file in files:
try:
open(os.path.join(folder, file), "w")
except FileExistsError:
print("File {} already exists".format(file))
return folder
def download(day, year, path):
print("Downloading input and example data...")
# check if session key is set
if "AOC_SESSION" not in os.environ:
token = subprocess.check_output(["aocd-token".format(day, year)])
token = token.decode("utf-8").split(" ")[0].strip()
os.environ["AOC_SESSION"] = token
puzzle = Puzzle(year=year, day=day)
# download test
test_location = os.path.join(path, "test.txt")
with open(test_location, "w") as f:
f.write(puzzle.example_data)
# download input
input_location = os.path.join(path, "input.txt")
with open(input_location, "w") as f:
f.write(puzzle.input_data)
def write_default_code(day):
with open("day-{}/{}.py".format(day, day), "w") as f:
f.write("# Path: day-{}/{}.py\n".format(day, day))
f.write("# Solution for day {} of Advent of Code\n".format(day))
f.write("import os\n\nfrom aoc_tools import Test\n\n\n")
f.write("def read_data(file):\n pass\n\n\n")
f.write("def solve(data):\n pass\n\n\n")
f.write('if __name__ == "__main__":\n')
f.write(" os.chdir(os.path.dirname(os.path.abspath(__file__)))\n")
f.write(' test, input = read_data("test.txt"), read_data("input.txt")\n')
f.write(" # Test(test, solve, -1, -1)\n")
f.write(" solve(test)\n")
def main(args):
if len(args) != 3:
print("Usage: python setup.py <year> <day>")
sys.exit(1)
# setup a new day for aoc
year = int(args[1])
day = int(args[2])
# Check if folder exists
if os.path.exists("day-{}".format(day)):
print("Day {} already exists".format(day))
sys.exit(1)
print("Creating new day of AoC: {}-12-{}".format(year, day))
# Create folder
folder = build_folder(day)
# Download input and test data
download(day, year, folder)
# write default code for day
write_default_code(day)
print("Done! Happy coding!")
if __name__ == "__main__":
main(sys.argv)