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 more utility functions

+38
+38
2016/utils.py
··· 1 1 import math 2 + import operator 2 3 from functools import total_ordering 4 + 5 + 6 + LETTERS = [x for x in 'abcdefghijklmnopqrstuvwxyz'] 7 + VOWELS = {'a', 'e', 'i', 'o', 'u'} 8 + CONSONANTS = set(x for x in LETTERS if x not in VOWELS) 9 + 10 + 11 + def mul(lst): 12 + """Like sum(), but for multiplication.""" 13 + return reduce(operator.mul, lst, 1) 14 + 15 + 16 + def factors(n): 17 + """Returns the factors of n.""" 18 + return sorted( 19 + x for tup in ( 20 + [i, n//i] for i in range(1, int(n**0.5) + 1) 21 + if n % i == 0) 22 + for x in tup) 23 + 24 + 25 + def primes(n): 26 + """Returns the sorted list of primes in the range [2, n]""" 27 + limit = n + 1 28 + not_prime = set() 29 + primes = [] 30 + 31 + for i in range(2, limit): 32 + if i in not_prime: 33 + continue 34 + 35 + for f in range(i*2, limit, i): 36 + not_prime.add(f) 37 + 38 + primes.append(i) 39 + 40 + return primes 3 41 4 42 5 43 @total_ordering