···2222# combinations('ABCD', 2) AB AC AD BC BD CD
2323# combinations_with_replacement('ABCD', 2) AA AB AC AD BB BC BD CC CD DD
24242525-# day .lines() .nlines() .paragraphs() .board() .pboard() .tboard()
2525+# day .lines .nlines .paragraphs .board .pboard .tboard
26262727tot = 0
2828res = []
+35-13
2022/utils.py
···252526262727def parse_nums(line, negatives=True):
2828- """Returns a list of numbers in `line`."""
2828+ """
2929+ Returns a list of numbers in `line`.
3030+3131+ Pass negatives=False to parse 1-2 as [1, 2].
3232+ """
2933 num_re = r'-?\d+' if negatives else r'\d+'
3034 return [int(n) for n in re.findall(num_re, line)]
313532363333-def new_table(val, width, height):
3737+def new_table(width, height, val=None):
3838+ """Returns a `width` by `height` table populated with `val`."""
3439 return [[val for _ in range(width)] for _ in range(height)]
35403641···646965706671def all_unique(lst):
7272+ """Returns True if all items in `lst` are unique."""
6773 return len(lst) == len(set(lst))
687469757070-def factors(n):
7171- """Returns the factors of n."""
7272- return sorted(
7373- x for tup in (
7474- [i, n // i] for i in range(1, int(n ** 0.5) + 1)
7575- if n % i == 0)
7676- for x in tup)
7777-7878-7976def gcd(a,b):
8077 """Compute the greatest common divisor of a and b"""
8178 while b > 0:
···104101 raise ValueError("%d is not invertible mod %d" % (a, n))
105102106103def crt(rems, mods):
107107- ''' Solve a system of modular equivalences via the Chinese Remainder Theorem.
108108- Does not require pairwise coprime moduli. '''
104104+ """
105105+ Solve a system of modular equivalences via the Chinese Remainder Theorem.
106106+ Does not require pairwise coprime moduli.
107107+108108+ Returns (n, m), where n is the solution and m is the modulo.
109109+110110+ Arguments
111111+ rems: the remainders of the problem
112112+ mods: the modulos of the problem
113113+114114+ """
109115110116 # copy inputs
111117 orems, omods = rems, mods
···153159154160155161def min_max_xy(points):
162162+ """
163163+ For a list of points, returns min_x, max_x, min_y, max_y.
164164+ This works on tuples (x, y) and Point(x, y).
165165+ """
156166 if len(points) == 0:
157167 return None, None, None, None
158168 if type(points[0]) == tuple:
···280290 return _mem_fn
281291282292293293+@memoize
283294def _eratosthenes(n):
284295 """http://stackoverflow.com/a/3941967/239076"""
285296 # Initialize list of primes
···297308 _primes[j] = False
298309299310311311+@memoize
300312def primes(n):
301313 """Return a list of primes from [2, n)"""
302314 return list(_eratosthenes(n))
315315+316316+317317+@memoize
318318+def factors(n):
319319+ """Returns the factors of n."""
320320+ return sorted(
321321+ x for tup in (
322322+ [i, n // i] for i in range(1, int(n ** 0.5) + 1)
323323+ if n % i == 0)
324324+ for x in tup)
303325304326305327def md5(msg):