diff options
Diffstat (limited to 'commons.py')
-rw-r--r-- | commons.py | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/commons.py b/commons.py new file mode 100644 index 0000000..2bc58c2 --- /dev/null +++ b/commons.py @@ -0,0 +1,28 @@ +from pathlib import Path + +import requests + +root: Path = Path(__file__).parent +cache: Path = root / 'cache' +cache.mkdir(parents=True, exist_ok=True) +session: str = (root / 'session.txt').read_text().strip() +cookies = dict(session=session) + + +def identity(x): + return x + + +def remove_empty(seq): + return filter(identity, seq) + + +def get_input(day: int) -> str: + cache_file = cache / str(day) + if cache_file.exists(): + return cache_file.read_text() + text = requests.get('http://adventofcode.com/2018/day/{}/input'.format(day), + cookies=cookies).text + cache_file.write_text(text) + return text + |