-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpresolve.py
72 lines (52 loc) · 1.6 KB
/
presolve.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
import os.path
import time
from datetime import datetime
import requests
from bs4 import BeautifulSoup
from dotenv import load_dotenv
load_dotenv()
YEAR = int(os.environ.get('YEAR', time.localtime().tm_year))
DAY = int(os.environ.get('DAY', time.localtime().tm_mday))
SESSION_COOKIE = str(os.environ.get('SESSION_COOKIE', ''))
cookies = {'session': SESSION_COOKIE}
while time.localtime().tm_hour != 0:
...
input_response = requests.get(
f'https://adventofcode.com/{YEAR}/day/{DAY}/input', cookies=cookies
)
if not os.path.exists(str(YEAR)):
os.mkdir(str(YEAR))
dirname = f'{YEAR}/day{DAY}'
if not os.path.exists(dirname):
os.mkdir(dirname)
response = requests.get(
f'https://adventofcode.com/{YEAR}/day/{DAY}', cookies=cookies
)
response.raise_for_status()
soup = BeautifulSoup(response.text, 'html.parser')
code_tags = soup.find_all('pre')
code_content_list = [code_tag.get_text() for code_tag in code_tags]
test_cases = [
"# data = '''" + '\n# '.join(code_content.split("\n")) + r"'''.split('\n')"
for code_content in code_content_list
]
main_content = r'''import pyperclip as pc
...
def solution(lines: list[str]):
ret = 0
for line in lines:
...
...
return ret
''' + '\n\n'.join(test_cases) + r'''
# data = open('input.txt', 'r').read().strip().split('\n')
s = solution(data)
print(s)
pc.copy(s)
'''
main_path = f'{dirname}/main{len(code_content_list)}.py'
if not os.path.exists(main_path):
with open(main_path, 'w+') as f:
f.write(main_content)
with open(f'{dirname}/input.txt', 'w+') as f:
f.write(input_response.content.decode().strip('\n'))