-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__main__.py
57 lines (42 loc) · 1.47 KB
/
__main__.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
import os
import requests
import pathlib
import json
import part1
import part2
def read_file(filename: str, split_lines: bool):
path = pathlib.Path(__file__).parent.resolve()
with open(path / filename, 'r') as f:
file = f.read()
if split_lines:
return file.splitlines()
return file
def save_file(filename: str, text: str) -> None:
with open(filename, 'w+') as file:
file.write(text)
def read_input(split_lines: bool):
day = pathlib.PurePath(__file__).parent.name.replace('day0', '').replace('day', '')
with open('../cookies.json', 'r') as cookies_file:
cookies = json.load(cookies_file)
input_url = 'https://adventofcode.com/2022/day/' + day + '/input'
response = requests.get(input_url, cookies=cookies, headers={'User-Agent': 'Mozilla/5.0'})
if response.ok:
text = response.text
save_file('input.txt', text)
if split_lines:
return text.splitlines()
return text
else:
return read_file('input.txt', split_lines)
def main() -> None:
split_lines = True
examples = [read_file(f, split_lines) for f in os.listdir('.') if os.path.isfile(f) and f.startswith("example")]
input = read_input(split_lines)
print("--- Part One ---")
part1.test(examples)
print("Result:", part1.result(input))
print("--- Part Two ---")
part2.test(examples)
print("Result:", part2.result(input))
if __name__ == "__main__":
main()