My Advent of Code solutions in Python. kevinyap.ca/2019/12/going-fast-in-advent-of-code/
advent-of-code python
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Update starter and util files

+36 -14
+1 -1
2022/starter.py
··· 22 22 # combinations('ABCD', 2) AB AC AD BC BD CD 23 23 # combinations_with_replacement('ABCD', 2) AA AB AC AD BB BC BD CC CD DD 24 24 25 - # day .lines() .nlines() .paragraphs() .board() .pboard() .tboard() 25 + # day .lines .nlines .paragraphs .board .pboard .tboard 26 26 27 27 tot = 0 28 28 res = []
+35 -13
2022/utils.py
··· 25 25 26 26 27 27 def parse_nums(line, negatives=True): 28 - """Returns a list of numbers in `line`.""" 28 + """ 29 + Returns a list of numbers in `line`. 30 + 31 + Pass negatives=False to parse 1-2 as [1, 2]. 32 + """ 29 33 num_re = r'-?\d+' if negatives else r'\d+' 30 34 return [int(n) for n in re.findall(num_re, line)] 31 35 32 36 33 - def new_table(val, width, height): 37 + def new_table(width, height, val=None): 38 + """Returns a `width` by `height` table populated with `val`.""" 34 39 return [[val for _ in range(width)] for _ in range(height)] 35 40 36 41 ··· 64 69 65 70 66 71 def all_unique(lst): 72 + """Returns True if all items in `lst` are unique.""" 67 73 return len(lst) == len(set(lst)) 68 74 69 75 70 - def factors(n): 71 - """Returns the factors of n.""" 72 - return sorted( 73 - x for tup in ( 74 - [i, n // i] for i in range(1, int(n ** 0.5) + 1) 75 - if n % i == 0) 76 - for x in tup) 77 - 78 - 79 76 def gcd(a,b): 80 77 """Compute the greatest common divisor of a and b""" 81 78 while b > 0: ··· 104 101 raise ValueError("%d is not invertible mod %d" % (a, n)) 105 102 106 103 def crt(rems, mods): 107 - ''' Solve a system of modular equivalences via the Chinese Remainder Theorem. 108 - Does not require pairwise coprime moduli. ''' 104 + """ 105 + Solve a system of modular equivalences via the Chinese Remainder Theorem. 106 + Does not require pairwise coprime moduli. 107 + 108 + Returns (n, m), where n is the solution and m is the modulo. 109 + 110 + Arguments 111 + rems: the remainders of the problem 112 + mods: the modulos of the problem 113 + 114 + """ 109 115 110 116 # copy inputs 111 117 orems, omods = rems, mods ··· 153 159 154 160 155 161 def min_max_xy(points): 162 + """ 163 + For a list of points, returns min_x, max_x, min_y, max_y. 164 + This works on tuples (x, y) and Point(x, y). 165 + """ 156 166 if len(points) == 0: 157 167 return None, None, None, None 158 168 if type(points[0]) == tuple: ··· 280 290 return _mem_fn 281 291 282 292 293 + @memoize 283 294 def _eratosthenes(n): 284 295 """http://stackoverflow.com/a/3941967/239076""" 285 296 # Initialize list of primes ··· 297 308 _primes[j] = False 298 309 299 310 311 + @memoize 300 312 def primes(n): 301 313 """Return a list of primes from [2, n)""" 302 314 return list(_eratosthenes(n)) 315 + 316 + 317 + @memoize 318 + def factors(n): 319 + """Returns the factors of n.""" 320 + return sorted( 321 + x for tup in ( 322 + [i, n // i] for i in range(1, int(n ** 0.5) + 1) 323 + if n % i == 0) 324 + for x in tup) 303 325 304 326 305 327 def md5(msg):