-
Notifications
You must be signed in to change notification settings - Fork 0
/
make_day.py
51 lines (47 loc) · 2.19 KB
/
make_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
import sys
import os
from datetime import date
from urllib.request import Request, urlopen
# usage: python3 ./make_day.py 2022 3
def make_day(year, day):
days = {"1": "One", "2": "Two", "3": "Three", "4": "Four", "5": "Five",
"6": "Six", "7": "Seven", "8": "Eight", "9": "Nine", "10": "Ten",
"11": "Eleven", "12": "Twelve", "13": "Thirteen", "14": "Fourteen", "15": "Fifteen",
"16": "Sixteen", "17": "Seventeen", "18": "Eighteen", "19": "Nineteen", "20": "Twenty",
"21": "TwentyOne", "22": "TwentyTwo", "23": "TwentyThree", "24": "TwentyFour", "25": "TwentyFive"
}
day_string = days[day]
file_path = f"./src/main/scala/AOC_{year}/Day{day_string}.scala"
input_path = f"./src/test/resources/{year}/Day{day}Input.txt"
if (os.path.exists(file_path)):
print(f'File {file_path} already exists')
else:
with open(f"./utils/day_challenge_template.txt") as template:
lines = template.read()
to_write = lines.replace("[[YEAR]]", year).replace("[[DAY]]", day_string)
print(f"Created new file {file_path} with template")
with open(file_path, 'x') as file:
file.write(to_write)
if (os.path.exists(input_path)):
print(f"Input file {input_path} already exists")
else:
f = open(input_path, 'x')
f.close
print(f"Created new empty file {input_path} with template")
with open(input_path, "a+") as input_file:
requested_date = date(int(year), 12, int(day))
existing_content = input_file.read()
input_file.seek(0)
if input_file.read() == "" and date.today() >= requested_date:
print("10/10 would fetch")
req = Request(f"https://adventofcode.com/{year}/day/{day}/input")
with open("./session_cookie.txt", "r") as cookie_file:
cookie = cookie_file.read()
req.add_header('Cookie', f'session={cookie}')
content = urlopen(req).read().decode('utf-8')
print("got input data")
input_file.write(content)
else:
print(f"either input file {input_path} is already populated or date is in the future")
if __name__ == "__main__":
make_day(sys.argv[1], sys.argv[2])