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.

Add starter.py and utils.py for 2017

+181
+31
2017/starter.py
··· 1 + import os # NOQA 2 + import sys # NOQA 3 + import re # NOQA 4 + import math # NOQA 5 + import fileinput 6 + from collections import Counter, deque, namedtuple # NOQA 7 + from itertools import count, product, permutations, combinations, combinations_with_replacement # NOQA 8 + 9 + from utils import parse_line, mul, factors, memoize, primes, new_table, Point, DIRS, DIRS_4, DIRS_8 # NOQA 10 + 11 + # Itertools Functions: 12 + # product('ABCD', repeat=2) AA AB AC AD BA BB BC BD CA CB CC CD DA DB DC DD 13 + # permutations('ABCD', 2) AB AC AD BA BC BD CA CB CD DA DB DC 14 + # combinations('ABCD', 2) AB AC AD BC BD CD 15 + # combinations_with_replacement('ABCD', 2) AA AB AC AD BB BC BD CC CD DD 16 + 17 + total = 0 18 + result = [] 19 + table = new_table(None, width=2, height=4) 20 + 21 + for i, line in enumerate(fileinput.input()): 22 + line = line.strip() 23 + 24 + # data = [x for x in line.split(', ')] 25 + # data = [x for x in line] 26 + # data = [int(x) for x in line.split()] 27 + # data = re.findall(r'(\w+)', line) 28 + data = parse_line(r'', line) 29 + 30 + if i == 0: 31 + print(data)
+150
2017/utils.py
··· 1 + import re 2 + import math 3 + import operator 4 + from functools import total_ordering 5 + 6 + 7 + LETTERS = [x for x in 'abcdefghijklmnopqrstuvwxyz'] 8 + VOWELS = {'a', 'e', 'i', 'o', 'u'} 9 + CONSONANTS = set(x for x in LETTERS if x not in VOWELS) 10 + 11 + 12 + def parse_line(regex, line): 13 + ret = [] 14 + for match in re.match(regex, line).groups(): 15 + try: 16 + ret.append(int(match)) 17 + except ValueError: 18 + ret.append(match) 19 + 20 + return ret 21 + 22 + 23 + def new_table(val, width, height): 24 + return [[val for _ in range(width)] for _ in range(height)] 25 + 26 + 27 + def mul(lst): 28 + """Like sum(), but for multiplication.""" 29 + return reduce(operator.mul, lst, 1) # NOQA 30 + 31 + 32 + def chunks(l, n): 33 + """Yield successive n-sized chunks from l.""" 34 + for i in range(0, len(l), n): 35 + yield l[i:i + n] 36 + 37 + 38 + def factors(n): 39 + """Returns the factors of n.""" 40 + return sorted( 41 + x for tup in ( 42 + [i, n // i] for i in range(1, int(n ** 0.5) + 1) 43 + if n % i == 0) 44 + for x in tup) 45 + 46 + 47 + def memoize(f): 48 + """Simple dictionary-based memoization decorator""" 49 + cache = {} 50 + 51 + def _mem_fn(*args): 52 + if args not in cache: 53 + cache[args] = f(*args) 54 + return cache[args] 55 + 56 + _mem_fn.cache = cache 57 + return _mem_fn 58 + 59 + 60 + def _eratosthenes(n): 61 + """http://stackoverflow.com/a/3941967/239076""" 62 + # Initialize list of primes 63 + _primes = [True] * n 64 + 65 + # Set 0 and 1 to non-prime 66 + _primes[0] = _primes[1] = False 67 + 68 + for i, is_prime in enumerate(_primes): 69 + if is_prime: 70 + yield i 71 + 72 + # Mark factors as non-prime 73 + for n in xrange(i * i, n, i): # NOQA 74 + _primes[n] = False 75 + 76 + 77 + def primes(n): 78 + """Return a list of primes from [2, n)""" 79 + return list(_eratosthenes(n)) 80 + 81 + 82 + @total_ordering 83 + class Point: 84 + """Simple 2-dimensional point.""" 85 + def __init__(self, x, y): 86 + self.x = x 87 + self.y = y 88 + 89 + def __add__(self, other): 90 + return Point(self.x + other.x, self.y + other.y) 91 + 92 + def __sub__(self, other): 93 + return Point(self.x - other.x, self.y - other.y) 94 + 95 + def __mul__(self, n): 96 + return Point(self.x * n, self.y * n) 97 + 98 + def __div__(self, n): 99 + return Point(self.x / n, self.y / n) 100 + 101 + def __neg__(self): 102 + return Point(-self.x, -self.y) 103 + 104 + def __eq__(self, other): 105 + return self.x == other.x and self.y == other.y 106 + 107 + def __ne__(self, other): 108 + return not self == other 109 + 110 + def __lt__(self, other): 111 + return self.manhattan < other.manhattan 112 + 113 + def __str__(self): 114 + return "({}, {})".format(self.x, self.y) 115 + 116 + def __hash__(self): 117 + return hash(tuple((self.x, self.y))) 118 + 119 + @property 120 + def manhattan(self): 121 + return abs(self.x) + abs(self.y) 122 + 123 + @property 124 + def distance(self): 125 + return math.sqrt(self.x ** 2 + self.y ** 2) 126 + 127 + def neighbours_4(self): 128 + return [self + p for p in DIRS_4] 129 + 130 + def neighbours_8(self): 131 + return [self + p for p in DIRS_8] 132 + 133 + 134 + DIRS_4 = DIRS = [ 135 + Point(0, 1), # north 136 + Point(1, 0), # east 137 + Point(0, -1), # south 138 + Point(-1, 0), # west 139 + ] 140 + 141 + DIRS_8 = [ 142 + Point(0, 1), # N 143 + Point(1, 1), # NE 144 + Point(1, 0), # E 145 + Point(1, -1), # SE 146 + Point(0, -1), # S 147 + Point(-1, -1), # SW 148 + Point(-1, 0), # W 149 + Point(-1, 0), # NW 150 + ]