From c676a77fb503dba7cf44805ff5f883d3e2c88b8f Mon Sep 17 00:00:00 2001 From: Luke Hubmayer-Werner Date: Fri, 9 Dec 2022 18:42:16 +1030 Subject: [PATCH] Update helpers, add automatic input downloading --- 2022/helpers.py | 29 +++++++++++++++++++++------- 2022/today.ipynb | 50 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 7 deletions(-) create mode 100644 2022/today.ipynb diff --git a/2022/helpers.py b/2022/helpers.py index 4adec52..abadc09 100644 --- a/2022/helpers.py +++ b/2022/helpers.py @@ -1,27 +1,42 @@ -import numpy as np from numpy.typing import ArrayLike +import browser_cookie3 +import datetime +import numpy as np import re +import requests + +today = datetime.date.today() +day = today.day +year = today.year +def download_input(day: int = day): + filename = f'input/{day:02}' + r = requests.get(f"https://adventofcode.com/{year}/day/{day}/input", cookies=browser_cookie3.firefox()) + if r.status_code != 200: + print(r) + else: + with open(filename,'w') as f: + f.write(r.text) + numbers_pattern = re.compile(r'((?:(? list[int]: return [int(x) for x in numbers_pattern.findall(line)] -def lines_to_numbers(lines): +def lines_to_numbers(lines: list[str]) -> list[list[int]]: return [line_to_numbers(line) for line in lines] -def transpose_array_of_strings(aos, reverse_x = False, reverse_y = False, strip = ''): +def transpose_array_of_strings(aos: list[str], reverse_x = False, reverse_y = False, strip = '') -> list[str]: return [''.join(l[i] for l in aos[::-1 if reverse_y else 1]).strip(strip) for i in range(len(aos[0]))[::-1 if reverse_x else 1]] -def read_day(day): +def read_day(day: int) -> str: with open(f'input/{day:02}', 'r') as file: return file.read().strip() dtype = np.int32 - directions_array = np.array([[1,0], [-1,0], [0,1], [0,-1]], dtype=dtype) directions_dict = {'R': directions_array[0], 'L': directions_array[1], 'D': directions_array[2], 'U': directions_array[3]} # Positive down for visualization -def visualise_sparse_cells_set(cells: set, size=20, sym_hit='#', sym_miss='.'): +def visualise_sparse_cells_set(cells: set[tuple[int, int]], size=20, sym_hit='#', sym_miss='.'): vis_lol = [[sym_hit if (x,y) in cells else sym_miss for x in range(-size, size)] for y in range(-size, size)] vis_str = '\n'.join(''.join(line) for line in vis_lol) print(vis_str) diff --git a/2022/today.ipynb b/2022/today.ipynb new file mode 100644 index 0000000..450d0dd --- /dev/null +++ b/2022/today.ipynb @@ -0,0 +1,50 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from helpers import *\n", + "\n", + "print(f'Initializing day {day:02}')\n", + "download_input(day)\n", + "input_stripped = read_day(day)\n", + "lines = input_stripped.split('\\n')\n", + "# input_a, _, input_b = file.read().strip().partition('\\n\\n')\n", + "# transpose_array_of_strings(input_a)\n", + "# s = set(input)\n", + "# print(s, len(s))\n", + "# numbers = np.array([[int(x) for x in line] for line in lines])" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3.11.0 64-bit", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.11.0" + }, + "orig_nbformat": 4, + "vscode": { + "interpreter": { + "hash": "5c7b89af1651d0b8571dde13640ecdccf7d5a6204171d6ab33e7c296e100e08a" + } + } + }, + "nbformat": 4, + "nbformat_minor": 2 +}