X-Git-Url: https://git.njae.me.uk/?a=blobdiff_plain;f=honeycomb_puzzle.py;fp=honeycomb_puzzle.py;h=5c0b6725f2c0ab879aa7748741f0c92549cee697;hb=978aa4af1b4a8753d0cd04f7fcceaf6d899bca79;hp=fcf190be840c1765eb4e5db1b0c8f27a38d6258e;hpb=68b4df6b83ce0f6bea2379d3f46d7296dd36a3c9;p=honeycomb-puzzle.git diff --git a/honeycomb_puzzle.py b/honeycomb_puzzle.py index fcf190b..5c0b672 100644 --- a/honeycomb_puzzle.py +++ b/honeycomb_puzzle.py @@ -21,37 +21,20 @@ import string import collections from dataclasses import dataclass import itertools - - -def only_letters(text): - return ''.join([c.lower() for c in text if c in string.ascii_letters]) - - -only_letters('Hell!o') - -with open('/usr/share/dict/british-english') as f: - words = [line.rstrip() for line in f] -len(words) +import cProfile with open('enable1.txt') as f: - words = [line.rstrip() for line in f] -len(words) + words = [line.strip() for line in f] -words = set(only_letters(w) - for w in words - if len(only_letters(w)) >= 4 - if len(frozenset(only_letters(w))) <= 7 +words = set(w for w in words + if len(w) >= 4 + if len(frozenset(w)) <= 7 if 's' not in w) -len(words) - word_sets = collections.defaultdict(set) for w in words: letters = frozenset(w) word_sets[letters].add(w) -len(word_sets) - -word_sets[frozenset('elephant')] @dataclass(frozen=True) @@ -63,21 +46,10 @@ class Honeycomb(): return f'{self.mandatory}|{"".join(sorted(self.letters - set(self.mandatory)))}' -honeycomb = Honeycomb(mandatory='g', letters=frozenset('apxmelg')) -honeycomb - - -def present(honeycomb, target): - return (honeycomb.mandatory in target) and target.issubset(honeycomb.letters) - - -present_sets = [s for s in word_sets if present(honeycomb, s)] -len(present_sets) - -present_sets - -for s in present_sets: - print(word_sets[s]) +def present(target, honeycomb): + return ( (honeycomb.mandatory in target) + and target.issubset(honeycomb.letters) + ) def score(present_set): @@ -92,102 +64,37 @@ def score(present_set): return score -sum(score(present_set) for present_set in present_sets) - -set('megaplex') in words - -scored_sets = {s: score(s) for s in word_sets} - -for s in present_sets: - print(s, word_sets[s], score(s), scored_sets[s]) - - def score_honeycomb(honeycomb): return sum(sc for letters, sc in scored_sets.items() - if honeycomb.mandatory in letters - if letters.issubset(honeycomb.letters) -# if present(honeycomb, letters) + # if honeycomb.mandatory in letters + # if letters.issubset(honeycomb.letters) + if present(letters, honeycomb) ) -score_honeycomb(honeycomb) - -# + -# hcs = [] - -# for mand in 'abcde': -# remaining = set('abcde') - set(mand) -# for others in itertools.combinations(remaining, r=3): -# hcs.append(Honeycomb(mandatory=mand, letters=frozenset(others) | frozenset(mand))) - -# print(len(hcs)) - -# + -# hcs - -# + -# honeycombs = [] - -# for mand in string.ascii_lowercase: -# remaining = set(string.ascii_lowercase) - set(mand) -# for others in itertools.combinations(remaining, r=6): -# honeycombs.append(Honeycomb(mandatory=mand, letters=frozenset(others) | frozenset(mand))) - -# print(len(honeycombs)) - -# + -# honeycombs = [] - -# candidate_letters = set(string.ascii_lowercase) -# candidate_letters.remove('s') -# candidate_letters = frozenset(candidate_letters) - -# for mand in candidate_letters: -# remaining = candidate_letters - set(mand) -# for others in itertools.combinations(remaining, r=6): -# honeycombs.append(Honeycomb(mandatory=mand, letters=frozenset(others) | frozenset(mand))) - -# print(len(honeycombs)) - -# + -# [(h, score_honeycomb(h)) for h in honeycombs[:5]] - -# + -# # %%timeit -# max(honeycombs, key=score_honeycomb) -# - - pangram_sets = [s for s in word_sets.keys() if len(s) == 7 if 's' not in s] -len(pangram_sets) +print("pangram sets:", len(pangram_sets)) -# + -ps_honeycombs = [] - -for ps in pangram_sets: - for mand in ps: - ps_honeycombs.append(Honeycomb(mandatory=mand, letters=ps)) - -print(len(ps_honeycombs)) +with open('pangaram_words.txt', 'w') as f: + for s in pangram_sets: + for w in word_sets[s]: + f.write(f'{w}\n') # + -# # %%timeit -# max(ps_honeycombs, key=score_honeycomb) - -# + -## 1 a,e,g,i,n,r,t r 3898 -## 2 a,e,g,i,n,r,t n 3782 -## 3 a,e,g,i,n,r,t e 3769 -# - - -score_honeycomb(Honeycomb('r', frozenset('aeginrt'))) +# ps_honeycombs = [] -score_honeycomb(Honeycomb('n', frozenset('aeginrtn'))) +# for ps in pangram_sets: +# for mand in ps: +# ps_honeycombs.append(Honeycomb(mandatory=mand, letters=ps)) -score_honeycomb(Honeycomb('e', frozenset('aeginrte'))) +ps_honeycombs = [Honeycomb(mandatory=mand, letters=ps) + for ps in pangram_sets + for mand in ps] +print(len(ps_honeycombs), "honeycombs") +# - partitioned_scored_sets = {l: {s: scored_sets[s] for s in scored_sets if l in s} for l in string.ascii_lowercase} -len(partitioned_scored_sets) def partitioned_score_honeycomb(honeycomb): @@ -196,17 +103,23 @@ def partitioned_score_honeycomb(honeycomb): ) +# + +# # # %%timeit +best_honyecomb = max(ps_honeycombs, key=partitioned_score_honeycomb) +print(best_honyecomb, partitioned_score_honeycomb(best_honyecomb)) +# - + # # %%timeit -print(max(ps_honeycombs, key=partitioned_score_honeycomb)) +# best_honyecomb = max(ps_honeycombs, key=score_honeycomb) +# print(best_honyecomb, partitioned_score_honeycomb(best_honyecomb)) # + -# partitioned_score_honeycomb(Honeycomb('a', 'abc')) +# cProfile.run('max(ps_honeycombs, key=score_honeycomb)') -# + -# max(len(partitioned_scored_sets[k]) for k in partitioned_scored_sets) # + -# len(scored_sets) +# cProfile.run('max(ps_honeycombs, key=partitioned_score_honeycomb)') # - +