advent2023/useful.py

51 lines
1.4 KiB
Python

#!/usr/bin/env python3
from functools import cache, reduce
from itertools import chain, combinations, count, cycle, dropwhile, groupby, pairwise, permutations, product, takewhile
from more_itertools import batched, collapse, sliding_window
from collections import defaultdict, deque
import re, sys
import math
import numpy as np
from icecream import ic
from copy import deepcopy
def commaline(line):
return [int(n) for n in line.strip().split(',')]
def headerfooter(f):
return f.read().split('\n\n', maxsplit=1)
def pgphs(text):
return text.strip().split('\n\n')
def hfl(f): # header footer lines
return [p.strip().split('\n') for p in headerfooter(f)]
def naturals(l): # usually what we want really
return [int(x) for x in re.findall(r'[0-9]+', l)]
def numbers(l): # usually what we want really
return [int(x) for x in re.findall(r'-?[0-9]+', l)]
def lines(f):
return [l.strip() for l in f.readlines()]
def dprint(*args):
sep = False
for arg in args:
if sep:
print('--------', file=sys.stderr)
sep = True
for a, b in arg.items():
print('%10s: %s' % (a, ','.join(str(bb) for bb in b)), file=sys.stderr)
print(file=sys.stderr)
def pp(twod):
for r in twod:
for c in r:
print('#' if c else '.', end='')
print()
def take(it, n):
return [x for (i, x) in enumerate(it) if i < n]