Created
January 3, 2020 19:31
-
-
Save jowens/80289d982ac261274618e934b701df42 to your computer and use it in GitHub Desktop.
Code to solve Riddler Classic 3 Jan 2020
This file contains 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
#!/usr/bin/env python3 | |
from itertools import chain, combinations | |
def powerset(iterable): | |
"powerset([1,2,3]) --> () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)" | |
s = list(iterable) | |
return chain.from_iterable(combinations(s, r) for r in range(len(s)+1)) | |
fp = open('enable1.txt', 'r') | |
scorers = {} # key: grid (string), value: set(words) | |
points = {} # key: grid (string), value: points for that grid (int) | |
def validGrid(wordSet): | |
out = [] | |
for letter in sorted(wordSet): | |
l = sorted(wordSet) | |
l.insert(0, l.pop(l.index(letter))) | |
out.append(''.join(l)) | |
return out | |
for word in fp.readlines(): | |
word = word.rstrip() | |
if 's' in word: # s can't be in the grid | |
continue | |
if len(word) < 4: # too short | |
continue | |
wordSet = set(word) | |
if len(wordSet) > 7: # too many letters | |
continue | |
for t in validGrid(wordSet): | |
if t not in scorers: | |
scorers[t] = set() | |
scorers[t].add(word) # this is NOT within the if clause | |
def computeScore(word): | |
# Note that letters can be repeated. For example, the words GAME | |
# and AMALGAM are both acceptable words. Four-letter words are | |
# worth 1 point each, while five-letter words are worth 5 points, | |
# six-letter words are worth 6 points, seven-letter words are | |
# worth 7 points, etc. Words that use all of the seven letters in | |
# the honeycomb are known as “pangrams” and earn 7 bonus points | |
# (in addition to the points for the length of the word). So in | |
# the above example, MEGAPLEX is worth 15 points. | |
s = len(word) | |
if len(set(word)) == 7: # pangram! | |
s += 7 | |
return s | |
# now, go grab all substrings and add them to "scorers" | |
# assumes only keys in scorers are possible winners, and only those with pangrams | |
# probably good assumptions, but not an exhaustive search | |
for grid in scorers: | |
if len(grid) == 7: # only ones with pangrams | |
for s in powerset(grid): # all possible subgrids | |
if len(s) > 0 and len(s) < 7: # only subgrids size [1--6] | |
for key in validGrid(s): # all possible keys for this subgrid | |
if key in scorers: # did we find words for this key? | |
for word in scorers[key]: # if so, add 'em all ... | |
if grid[0] in word: # ... only if they use the middle letter | |
# print(grid, key, word, s) | |
scorers[grid].add(word) | |
for grid in scorers: | |
points[grid] = sum(map(computeScore, scorers[grid])) | |
# grids = sorted(scorers, key=lambda k: len(scorers[k]), reverse=True) | |
grids = sorted(scorers, key=lambda grid: points[grid], reverse=True) | |
for g in grids: | |
print(g, scorers[g], points[g]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment