Created
December 4, 2017 07:27
-
-
Save saucecode/b7ccae2fa0e2e65e0822e250410ceeb8 to your computer and use it in GitHub Desktop.
my AoC day 4 part 2 prime-products solution
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import string | |
challenge = '''nyot babgr babgr kqtu kqtu kzshonp ylyk psqk | |
iix ewj rojvbkk phrij iix zuajnk tadv givslju ewj bda | |
-snip- | |
'''.split('\n') | |
primes = [int(x) for x in '2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101'.split(', ')] | |
letters = string.ascii_letters[:26] | |
lookup = dict(zip(letters, primes)) | |
def calculate_prime_hash(word): | |
i = 1 | |
for c in word: | |
i *= lookup[c] | |
return i | |
valids = 0 | |
for phrase in challenge: | |
words = phrase.split(' ') | |
pash = [calculate_prime_hash(word) for word in words] | |
valid = True | |
for product in pash: | |
if pash.count(product) > 1: | |
valid = False | |
break | |
if valid: | |
valids += 1 | |
print(valids) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment