diff options
author | romangraef <romangraef@loves.dicksinhisan.us> | 2018-12-01 12:31:26 +0100 |
---|---|---|
committer | romangraef <romangraef@loves.dicksinhisan.us> | 2018-12-01 12:31:26 +0100 |
commit | 37a85f8df1fb3a0dac50152daabf243316f2d31d (patch) | |
tree | a27140252c0b62561f02ca328e2aa43a4f90dbea | |
download | aoc2018-37a85f8df1fb3a0dac50152daabf243316f2d31d.tar.gz aoc2018-37a85f8df1fb3a0dac50152daabf243316f2d31d.tar.bz2 aoc2018-37a85f8df1fb3a0dac50152daabf243316f2d31d.zip |
Initial commit
-rw-r--r-- | .gitignore | 3 | ||||
-rw-r--r-- | README.md | 4 | ||||
-rw-r--r-- | commons.py | 28 | ||||
-rw-r--r-- | day1/solve.py | 20 | ||||
-rw-r--r-- | requirements.txt | 1 |
5 files changed, 56 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6e4d8fc --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +venv/ +cache/ +session.txt diff --git a/README.md b/README.md new file mode 100644 index 0000000..fe4a1c7 --- /dev/null +++ b/README.md @@ -0,0 +1,4 @@ +## AoC 2018 + +Put your session token in `session.txt` and just run the files. + 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 + diff --git a/day1/solve.py b/day1/solve.py new file mode 100644 index 0000000..fda5b7d --- /dev/null +++ b/day1/solve.py @@ -0,0 +1,20 @@ +from itertools import cycle + +from commons import get_input, remove_empty + +seq = list(map(int, remove_empty(get_input(1).split('\n')))) + +found = set() + +freq = 0 +dup = 0 +for el in cycle(seq): + freq += el + if freq in found: + dup = freq + break + found.add(freq) + +if __name__ == '__main__': + print("first:", sum(seq)) + print("second:", dup) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..f229360 --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +requests |