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 utils and starter files

+49
+1
2019/starter.py
··· 8 8 from itertools import count, product, permutations, combinations, combinations_with_replacement # NOQA 9 9 10 10 from utils import parse_line, parse_nums, mul, all_unique, factors, memoize, primes # NOQA 11 + from utils import chunks, gcd, lcm, print_grid, min_max_xy # NOQA 11 12 from utils import new_table, transposed, rotated # NOQA 12 13 from utils import md5, sha256, knot_hash # NOQA 13 14 from utils import VOWELS, CONSONANTS # NOQA
+48
2019/utils.py
··· 64 64 for x in tup) 65 65 66 66 67 + def gcd(a,b): 68 + """Compute the greatest common divisor of a and b""" 69 + while b > 0: 70 + a, b = b, a % b 71 + return a 72 + 73 + 74 + def lcm(a, b): 75 + """Compute the lowest common multiple of a and b""" 76 + return a * b / gcd(a, b) 77 + 78 + 79 + def min_max_xy(points): 80 + if len(points) == 0: 81 + return None, None, None, None 82 + if type(points[0]) == tuple: 83 + min_x = min(p[0] for p in points) 84 + max_x = max(p[0] for p in points) 85 + min_y = min(p[1] for p in points) 86 + max_y = max(p[1] for p in points) 87 + else: 88 + min_x = min(p.x for p in points) 89 + max_x = max(p.x for p in points) 90 + min_y = min(p.y for p in points) 91 + max_y = max(p.y for p in points) 92 + 93 + return min_x, max_x, min_y, max_y 94 + 95 + 96 + def print_grid(grid, f=None): 97 + if f is None: 98 + f = lambda x: x # NOQA 99 + 100 + if type(grid) is dict: 101 + positions = grid.keys() 102 + min_x, max_x, min_y, max_y = min_max_xy(positions) 103 + if type(positions[0]) is tuple: 104 + for y in range(min_y, max_y + 1): 105 + print ''.join(f(grid.get((x, y), ' ')) for x in range(min_x, max_x + 1)) 106 + else: 107 + # (x, y) => point 108 + for y in range(min_y, max_y + 1): 109 + print ''.join(f(grid.get(Point(x, y), ' ')) for x in range(min_x, max_x + 1)) 110 + else: 111 + for y in range(len(grid)): 112 + print ''.join(f(grid[y][x]) for x in range(len(grid[0]))) 113 + 114 + 67 115 def memoize(f): 68 116 """Simple dictionary-based memoization decorator""" 69 117 cache = {}