include .env | include .env | ||||
FILE = $(shell find . -path "./y????/p??.py" -type f | xargs ls -rt | tail -n 1) | |||||
DATA = $(shell echo $(FILE) | sed s/.py/.dat/) | |||||
PYTHONPATH = . | |||||
export | export | ||||
main: venv/ | |||||
@venv/bin/python -u toolkit.py $(FILE) | |||||
@cat $(DATA) | venv/bin/python -u $(FILE) | |||||
PYTHONPATH := . | |||||
FILE := $(shell find . -path "./y????/p??.py" -type f | xargs ls -rt | tail -n 1) | |||||
YEAR := $(shell echo ${FILE} | sed 's/[^0-9]/ /g' | cut -d' ' -f4) | |||||
DAY := $(shell echo ${FILE} | sed 's/[^0-9]/ /g' | cut -d' ' -f6 | bc) | |||||
URL := https://adventofcode.com/${YEAR}/day/${DAY}/input | |||||
DATA := $(shell echo ${FILE} | sed s/.py/.dat/) | |||||
flake: venv/ | |||||
venv/bin/flake8 --exclude=venv/ | |||||
venv/: requirements.txt | |||||
rm -rf venv/ | |||||
~/.pyenv/versions/3.8.0/bin/python -m venv venv | |||||
venv/bin/pip install -r requirements.txt | |||||
touch requirements.txt venv/ | |||||
# install flake8 git hook | |||||
# echo 'venv/bin/flake8 --exclude=venv/' > .git/hooks/pre-commit | |||||
# chmod +x .git/hooks/pre-commit | |||||
main: | |||||
@test -f ${DATA} || curl -s -b "session=${SESSION}" ${URL} > ${DATA} | |||||
@cat $(DATA) | python3 -u $(FILE) |
import sys | import sys | ||||
from pathlib import Path | from pathlib import Path | ||||
import requests | |||||
product = functools.partial(functools.reduce, operator.mul) | product = functools.partial(functools.reduce, operator.mul) | ||||
return path[::-1] | return path[::-1] | ||||
def ensure_data(path): | |||||
if not path.exists(): | |||||
year, qn = map(int, re.findall(r'\d+', sys.argv[1])) | |||||
url = f'https://adventofcode.com/{year}/day/{qn}/input' | |||||
cookies = {'session': os.environ['SESSION']} | |||||
response = requests.get(url, cookies=cookies) | |||||
response.raise_for_status() | |||||
path.write_bytes(response.content) | |||||
def batch(iterable, size): | def batch(iterable, size): | ||||
count = itertools.count() | count = itertools.count() | ||||
for _, sub in itertools.groupby(iterable, lambda _: next(count) // size): | for _, sub in itertools.groupby(iterable, lambda _: next(count) // size): | ||||
count += 1 | count += 1 | ||||
else: | else: | ||||
raise RuntimeError('Reached steady state') | raise RuntimeError('Reached steady state') | ||||
if __name__ == '__main__': | |||||
data_file = Path(sys.argv[1]).with_suffix('.dat') | |||||
ensure_data(data_file) |
ans1 = 0 | ans1 = 0 | ||||
ans2 = None | ans2 = None | ||||
for i, char in enumerate(data_file.read_text(), 1): | |||||
for i, char in enumerate(open(0).read(), 1): | |||||
ans1 += {'(': 1, ')': -1}[char] | ans1 += {'(': 1, ')': -1}[char] | ||||
if ans1 == -1 and ans2 is None: | if ans1 == -1 and ans2 is None: | ||||
ans2 = i | ans2 = i | ||||
print(ans1) | |||||
print(ans2) |