-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_day.py
234 lines (216 loc) · 7.06 KB
/
create_day.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
"""
Scaffolds an Advent of Code day.
"""
import argparse
from datetime import date
import os
import shutil
import requests
def create_sublime_project(basedir, daystr, write_python, write_ruby, sample_count):
"""Creates a skeleton sublime project."""
with open(os.path.join(basedir, daystr + '.sublime-project'), 'w') as file:
file.write("""{
"folders":
[
{
"path": "src",
"name": "Source"
},
{
"path": "input",
"name": "Input files"
}
],
"build_systems":
[""")
if write_python:
file.write("""
{
"name": "Run Python Part 1",
"cmd": ["py", "-3", "$file", "-f", "../input/input.txt", "-p", "1"]
},
{
"name": "Run Python Part 2",
"cmd": ["py", "-3", "$file", "-f", "../input/input.txt", "-p", "2"]
},""")
if sample_count == 1:
file.write("""
{
"name": "Run Python Sample Part 1",
"cmd": ["py", "-3", "$file", "-f", "../input/sample.txt", "-p", "1"]
},
{
"name": "Run Python Sample Part 2",
"cmd": ["py", "-3", "$file", "-f", "../input/sample.txt", "-p", "2"]
},""")
else:
for i in range(sample_count):
message = (
"\n"
" {\n"
f" \"name\": \"Run Python Sample {i + 1} Part 1\",\n"
f" \"cmd\": [\"py\", \"-3\", \"$file\", \"-f\", \"../input/sample{i + 1}.txt\", \"-p\", \"1\"]\n"
" },\n"
" {\n"
f" \"name\": \"Run Python Sample {i + 1} Part 2\",\n"
f" \"cmd\": [\"py\", \"-3\", \"$file\", \"-f\", \"../input/sample{i + 1}.txt\", \"-p\", \"2\"]\n"
" },"
)
file.write(message)
if write_ruby:
file.write("""
{
"name": "Run Ruby Part 1",
"cmd": ["ruby", "$file", "-f", "../input/input.txt", "-p", "1"]
},
{
"name": "Run Ruby Part 2",
"cmd": ["ruby", "$file", "-f", "../input/input.txt", "-p", "2"]
},""")
if sample_count == 1:
file.write("""
{
"name": "Run Ruby Sample Part 1",
"cmd": ["ruby", "$file", "-f", "../input/sample.txt", "-p", "1"]
},
{
"name": "Run Ruby Sample Part 2",
"cmd": ["ruby", "$file", "-f", "../input/sample.txt", "-p", "2"]
},""")
else:
for i in range(sample_count):
message = (
"\n"
" {\n"
f" \"name\": \"Run Ruby Sample {i + 1} Part 1\",\n"
f" \"cmd\": [\"ruby\", \"$file\", \"-f\", \"../input/sample{i + 1}.txt\", \"-p\", \"1\"]\n"
" },\n"
" {\n"
f" \"name\": \"Run Ruby Sample {i + 1} Part 2\",\n"
f" \"cmd\": [\"ruby\", \"$file\", \"-f\", \"../input/sample{i + 1}.txt\", \"-p\", \"2\"]\n"
" },"
)
file.write(message)
file.write("""
]
}
""")
def create_src_dir(basedir, daystr, write_python, write_ruby, sample_count):
"""Creates a source directory with ruby or python skeleton code."""
os.mkdir(os.path.join(basedir, 'src'))
if write_python:
create_python_src_file(basedir, daystr, sample_count)
if write_ruby:
create_ruby_src_file(basedir, daystr, sample_count)
def create_python_src_file(basedir, daystr, sample_count):
"""Creates a skeleton python file."""
filename = ''
if sample_count == 1:
filename = '../input/sample.txt'
else:
filename = '../input/sample1.txt'
with open(os.path.join(basedir, 'src', daystr + '.py'), 'w') as file:
message = (
"import argparse\n"
"\n"
"def main():\n"
" parser = argparse.ArgumentParser()\n"
f" parser.add_argument('-f', '--filename', default='{filename}')\n"
" parser.add_argument('-p', '--part', choices=[1, 2], default=1, type=int)\n"
" args = parser.parse_args()\n"
"\n"
"if __name__ == \"__main__\":\n"
" main()\n"
)
file.write(message)
def create_ruby_src_file(basedir, daystr, sample_count):
"""Creates a skeleton python file."""
filename = ''
if sample_count == 1:
filename = '../input/sample.txt'
else:
filename = '../input/sample1.txt'
with open(os.path.join(basedir, 'src', daystr + '.rb'), 'w') as file:
message = (
"#!/usr/bin/ruby\n"
"\n"
"require 'optparse'\n"
"\n"
"options = {\n"
" :part => 1,\n"
f" :filename => \"{filename}\"\n"
"}\n"
"\n"
"OptionParser.new do |opts|\n"
f" opts.banner = \"Usage: {daystr}.rb [options]\"\n"
" opts.on(\"-p PART\", \"--part=PART\", Integer, \"Part 1 or 2\")\n"
" opts.on(\"-f FILENAME\", \"--filename=FILENAME\", String, \"Which filename to use?\")\n"
"end.parse!(into: options)\n"
"\n"
"if options[:part] == 1\n"
" # do part 1\n"
"elsif options[:part] == 2\n"
" # do part 2\n"
"end\n"
)
file.write(message)
def create_input_file(basedir, day, year):
"""Fetches my input from Advent of Code."""
os.mkdir(os.path.join(basedir, 'input'))
url = 'https://adventofcode.com/' + str(year) + '/day/' + str(day) + '/input'
cookies = dict()
with open('session.txt', 'r') as file:
cookies['session'] = file.read().strip()
with open(os.path.join(basedir, 'input', 'input.txt'), 'w') as file:
input_request = requests.get(url=url, cookies=cookies)
file.write(input_request.text)
def create_sample_files(basedir, count):
"""Creates a number of sample files."""
if count == 1:
with open(os.path.join(basedir, 'input', 'sample.txt'), 'w') as file:
file.write('Fill in sample here!')
else:
for i in range(count):
with open(os.path.join(basedir, 'input', f'sample{i + 1}.txt'), 'w') as file:
file.write('Fill in sample here!')
def get_day(day):
"""Gets today's date, or the date passed in."""
if day is not None:
return day
return date.today().day
def get_year(year):
"""Gets today's date, or the date passed in."""
if year is not None:
return year
return date.today().year
def main():
"""Scaffolds an AoC day."""
parser = argparse.ArgumentParser()
parser.add_argument('-d', '--day', default=None, type=int)
parser.add_argument('-y', '--year', default=None, type=int)
parser.add_argument('-e', '--delete', action='store_true')
parser.add_argument('-r', '--ruby', action='store_true')
parser.add_argument('-p', '--python', action='store_true')
parser.add_argument('-s', '--samples', default=1, type=int)
args = parser.parse_args()
day = get_day(args.day)
year = get_year(args.year)
yearstr = str(year)
daystr = 'Day' + str(day)
basedir = os.path.join(yearstr, daystr)
if args.delete:
if not os.path.exists(basedir):
print(f'{basedir} does not exist.')
else:
shutil.rmtree(basedir)
else:
if os.path.exists(basedir):
print(f'{basedir} already exists.')
else:
os.mkdir(basedir)
create_sublime_project(basedir, daystr, args.python, args.ruby, args.samples)
create_src_dir(basedir, daystr, args.python, args.ruby, args.samples)
create_input_file(basedir, day, year)
create_sample_files(basedir, args.samples)
if __name__ == "__main__":
main()