-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_day.py
98 lines (70 loc) · 2.13 KB
/
generate_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
#!/usr/bin/env python3
from datetime import date
from enum import Enum
from pathlib import Path
from requests import get
class Language(str, Enum):
Python = "Python"
Haskell = "Haskell"
extension = {Language.Python: "py", Language.Haskell: "hs"}
cookie = open(".cookie", 'r').readline().strip()
cookies = {'session': cookie}
year = 2021
def python_stub(day):
return f"""from aoc import get_lines
from pathlib import Path
def parse_input(lines):
return lines
def part_1(lines):
pass
def part_2(lines):
pass
def main():
lines = get_lines("input_{day:02d}.txt")
lines = parse_input(lines)
print("Part 1:", part_1(lines))
print("Part 2:", part_2(lines))
if __name__ == '__main__':
main()
"""
def haskell_stub(day):
return f"""
main = do
contents <- readFile "../../inputs/input_{day:02d}.txt"
print . map readInt . words $ contents
readInt :: String -> Int
readInt = read
"""
def get_input(day: int):
input_file = Path("inputs") / f"input_{day:02d}.txt"
input_file.parent.mkdir(parents=True, exist_ok=True)
if input_file.exists():
return
print(f"Fetching day {day}")
raw_input = get(f"https://adventofcode.com/{year}/day/{day}/input", cookies=cookies)
with input_file.open('w') as f:
f.writelines(raw_input.content.decode('utf-8'))
def generate_stub(day: int, lang: Language):
input_file = Path("src") / lang.name / f"day_{day:02d}.{extension[lang]}"
input_file.parent.mkdir(parents=True, exist_ok=True)
if input_file.exists():
return
print(f"Creating stub day {day} for {lang.name}")
with input_file.open('w') as f:
if lang == Language.Python:
stub = python_stub(day)
else:
stub = haskell_stub(day)
f.write(stub)
def main():
today = date.today()
print("Today is:", today)
for day in range(1, 26):
if today.year == year and (today.day < day or today.month != 12):
break
get_input(day)
generate_stub(day, lang=Language.Python)
generate_stub(day, lang=Language.Haskell)
print("Done")
if __name__ == '__main__':
main()