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 day14.py

+16 -12
+16 -12
2016/day14.py
··· 8 8 9 9 @memoize 10 10 def salty_md5(salt, i): 11 - return md5(salt + str(i)).hexdigest() 11 + return md5('{}{}'.format(salt, i)).hexdigest() 12 12 13 13 14 14 @memoize ··· 27 27 return a 28 28 29 29 30 - def all_quintuplets(s): 30 + def all_quintuples(s): 31 31 for i in range(len(s) - 4): 32 32 a, b, c, d, e = s[i:i+5] 33 33 if a == b == c == d == e: 34 34 yield a 35 35 36 36 37 - def find_pad_key_64(salt, hash_fn): 37 + def find_64th_pad_key(salt, hash_fn): 38 38 valid_keys = 0 39 - quintuplets = defaultdict(set) 39 + triples = {} 40 + quintuples = defaultdict(set) 40 41 i = 0 41 42 42 43 while True: 43 44 digest = hash_fn(salt, i) 45 + triple = first_triple(digest) 44 46 45 - for quint in all_quintuplets(digest): 46 - quintuplets[i].add(quint) 47 + if triple is not None: 48 + triples[i] = triple 47 49 48 - if i >= 1000: 50 + for quint in all_quintuples(digest): 51 + quintuples[i].add(quint) 52 + 53 + if (i - 1000) in triples: 49 54 n = i - 1000 50 - triple = first_triple(hash_fn(salt, n)) 55 + triple = triples[n] 51 56 for j in range(n + 1, n + 1001): 52 - if triple in quintuplets[j]: 57 + if triple in quintuples[j]: 53 58 valid_keys += 1 54 59 sys.stdout.write('.') 55 60 sys.stdout.flush() ··· 64 69 65 70 if __name__ == "__main__": 66 71 SALT = fileinput.input()[0].strip() 67 - 68 - print "Index of 64th one-time pad key", find_pad_key_64(SALT, salty_md5) 69 - print "Index of key-stretched pad key", find_pad_key_64(SALT, stretched_md5) 72 + print "Index of 64th one-time pad key", find_64th_pad_key(SALT, salty_md5) 73 + print "Index of key-stretched pad key", find_64th_pad_key(SALT, stretched_md5)