~cdv/aoc-2019-rs

aoc-2019-rs/dl -rwxr-xr-x 1.1 KiB
c11aef2aChris Vittal Day 24: Use cycle finding for part 1 1 year, 9 months ago
                                                                                
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
#!/usr/bin/env python3
"""An aoc dowloader, for 2019"""
import argparse
import os
import sys
from datetime import datetime

import toml
import pytz
import requests

TZ = pytz.timezone('America/New_York')
TIME = datetime.now(tz=TZ)


def main():
    """Main"""
    parser = argparse.ArgumentParser()
    parser.add_argument('day', nargs='?')

    args = parser.parse_args()
    day = int(args.day) if args.day is not None else TIME.day
    if day > 25 or day < 1:
        raise ValueError(f'day = {day}, must be between 1 and 25')

    session = toml.load("credentials.toml")["session"]
    jar = requests.cookies.RequestsCookieJar()
    jar.set("session", session, domain=".adventofcode.com", path="/")
    resp = requests.get(f'https://adventofcode.com/2019/day/{day}/input',
                        cookies=jar)
    if not resp.ok:
        print(resp.text, file=sys.stderr, end='')
        resp.raise_for_status()

    os.makedirs(f'input', exist_ok=True)
    with open(f'input/day_{day:02d}.txt', 'w') as f:
        f.write(resp.text)
        f.flush()
    print(resp.text, end='')


if __name__ == "__main__":
    main()