Skip to content

Instantly share code, notes, and snippets.

@saucecode
Created December 4, 2017 07:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save saucecode/b7ccae2fa0e2e65e0822e250410ceeb8 to your computer and use it in GitHub Desktop.
Save saucecode/b7ccae2fa0e2e65e0822e250410ceeb8 to your computer and use it in GitHub Desktop.
my AoC day 4 part 2 prime-products solution
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