-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaoc.py
28 lines (22 loc) · 841 Bytes
/
aoc.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
from pathlib import Path
import requests
with open('sessioncookie.txt') as f:
SESSION_COOKIE = f.read().strip()
def get_input(day, as_list=True):
input_dir = Path('./input')
input_dir.mkdir(exist_ok=True)
filepath = input_dir / f'{day}.txt'
if not filepath.is_file():
print(f'Fetching input file for day {day} from AdventOfCode.com...')
response = requests.get(f'https://adventofcode.com/2024/day/{day}/input',
cookies={'session': SESSION_COOKIE})
if response.ok:
with open(filepath, 'w') as ipt_file:
ipt_file.write(response.text)
else:
raise ValueError(response.text)
with open(filepath) as f:
if as_list:
return f.read().splitlines()
else:
return f.read().strip()