2022-12-09 17:00:35 +10:30
|
|
|
from numpy.typing import ArrayLike
|
2022-12-09 18:42:16 +10:30
|
|
|
import numpy as np
|
2022-12-13 16:13:12 +10:30
|
|
|
from functools import cmp_to_key
|
|
|
|
from math import prod
|
2022-12-13 16:50:30 +10:30
|
|
|
from time import perf_counter_ns
|
2022-12-05 20:42:34 +10:30
|
|
|
import re
|
2022-12-09 18:42:16 +10:30
|
|
|
import requests
|
2022-12-12 17:05:09 +10:30
|
|
|
import sys
|
|
|
|
sys.setrecursionlimit(100000)
|
2022-12-09 18:42:16 +10:30
|
|
|
|
2022-12-10 16:35:42 +10:30
|
|
|
try:
|
|
|
|
import datetime
|
|
|
|
today = datetime.date.today()
|
|
|
|
day = today.day
|
|
|
|
year = today.year
|
|
|
|
|
|
|
|
import browser_cookie3
|
|
|
|
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)
|
|
|
|
except ModuleNotFoundError as mod:
|
|
|
|
print(f'{mod.args[0]}, automatic input fetching will not work.')
|
2022-12-09 18:42:16 +10:30
|
|
|
|
2022-12-05 20:42:34 +10:30
|
|
|
numbers_pattern = re.compile(r'((?:(?<!\d)-)?\d+)')
|
|
|
|
|
2022-12-09 18:42:16 +10:30
|
|
|
def line_to_numbers(line: str) -> list[int]:
|
2022-12-09 17:00:35 +10:30
|
|
|
return [int(x) for x in numbers_pattern.findall(line)]
|
2022-12-05 20:42:34 +10:30
|
|
|
|
2022-12-09 18:42:16 +10:30
|
|
|
def lines_to_numbers(lines: list[str]) -> list[list[int]]:
|
2022-12-09 17:00:35 +10:30
|
|
|
return [line_to_numbers(line) for line in lines]
|
2022-12-05 20:42:34 +10:30
|
|
|
|
2022-12-09 18:42:16 +10:30
|
|
|
def transpose_array_of_strings(aos: list[str], reverse_x = False, reverse_y = False, strip = '') -> list[str]:
|
2022-12-09 17:00:35 +10:30
|
|
|
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]]
|
|
|
|
|
2022-12-09 18:42:16 +10:30
|
|
|
def read_day(day: int) -> str:
|
2022-12-09 17:00:35 +10:30
|
|
|
with open(f'input/{day:02}', 'r') as file:
|
|
|
|
return file.read().strip()
|
|
|
|
|
2022-12-13 16:50:30 +10:30
|
|
|
def print_time(name: str, ns: int):
|
|
|
|
print(f'{name} took {ns}ns = {ns/1000000:.2f}ms')
|
|
|
|
|
2022-12-09 17:00:35 +10:30
|
|
|
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
|
|
|
|
|
2022-12-09 18:42:16 +10:30
|
|
|
def visualise_sparse_cells_set(cells: set[tuple[int, int]], size=20, sym_hit='#', sym_miss='.'):
|
2022-12-09 17:00:35 +10:30
|
|
|
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)
|