-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit_daily
executable file
·103 lines (85 loc) · 2.63 KB
/
init_daily
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
#!/usr/bin/env python3
import shutil
from datetime import datetime
import os
from pytz import timezone
import subprocess
from jinja2 import Template
import argparse
def main():
require_aoc()
os.makedirs(date_slug, exist_ok=True)
make_new_script()
download_readme()
download_input()
def require_aoc():
if not shutil.which('aoc'):
raise(ValueError('AOC commandline util not found. Get it from https://github.com/scarvalhojr/aoc-cli'))
def make_new_script():
if os.path.isfile(new_script_dest):
print(f'File {new_script_dest} already exists. Not overwriting.')
return
with open('./solution.py.j2') as t:
rendered = Template(t.read()).render(DD=args.day, YYYY=args.year)
with open(new_script_dest, "w") as fh:
fh.write(rendered)
os.chmod(new_script_dest, 0o755)
def download_readme():
if os.path.isfile(f'{date_slug}/puzzle.md'):
print(f'Already downloaded {date_slug}/puzzle.md')
return
subprocess.run(build_aoc_default_command(), cwd=date_slug)
def download_input():
if os.path.isfile(f'{date_slug}/input'):
print(f'Already downloaded {date_slug}/input')
return
subprocess.run(build_aoc_default_command() + ['d'], cwd=date_slug)
def build_aoc_default_command():
command = ['aoc']
if args.year:
command += ['-y', str(args.year)]
if args.day:
command += ['-d', str(args.day)]
return command
def valid_day(s):
try:
day = int(s)
if day < 1 or day > 25:
raise ValueError()
except ValueError:
raise argparse.ArgumentTypeError(f'{s} is not a valid day')
return str(day).rjust(2, '0')
def valid_year(s):
try:
year = int(s)
if year < 2015 or year > datetime.now(timezone('US/Eastern')).year:
raise ValueError()
except ValueError:
raise argparse.ArgumentTypeError(f'{s} is not a valid year')
return str(year)
if __name__ == "__main__":
eastern = timezone('US/Eastern')
now = datetime.now(eastern)
default_year = now.year
default_day = str(now.day).rjust(2, '0')
parser = argparse.ArgumentParser()
parser.add_argument(
"-d",
"--day",
dest="day",
default=default_day,
help="Puzzle day (defaults to today)",
type=valid_day,
)
parser.add_argument(
"-y",
"--year",
dest="year",
default=default_year,
help="Puzzle year (defaults to this year)",
type=valid_year
)
args = parser.parse_args()
date_slug = f'{args.year}/{args.day}'
new_script_dest = f'{date_slug}/solution.py'
main()