General updates
[honeycomb-puzzle.git] / honeycomb_puzzle_bits.ipynb
1 {
2 "cells": [
3 {
4 "cell_type": "markdown",
5 "id": "951180ec",
6 "metadata": {},
7 "source": [
8 "# Honeycomb letter puzzle\n",
9 "Solving the puzzle as posted on [FiveThirtyEight](https://fivethirtyeight.com/features/can-you-solve-the-vexing-vexillology/), also solved by [David Robinson](http://varianceexplained.org/r/honeycomb-puzzle/).\n"
10 ]
11 },
12 {
13 "cell_type": "code",
14 "execution_count": 1,
15 "id": "6f6fa3e7",
16 "metadata": {},
17 "outputs": [],
18 "source": [
19 "import string\n",
20 "import collections\n",
21 "from dataclasses import dataclass\n",
22 "import itertools\n",
23 "import ctypes\n",
24 "import cProfile"
25 ]
26 },
27 {
28 "cell_type": "code",
29 "execution_count": 5,
30 "id": "88f00e4c",
31 "metadata": {},
32 "outputs": [
33 {
34 "data": {
35 "text/plain": [
36 "172820"
37 ]
38 },
39 "execution_count": 5,
40 "metadata": {},
41 "output_type": "execute_result"
42 }
43 ],
44 "source": [
45 "with open('enable1.txt') as f:\n",
46 " words = [line.strip() for line in f]"
47 ]
48 },
49 {
50 "cell_type": "code",
51 "execution_count": 6,
52 "id": "1e75fba2",
53 "metadata": {},
54 "outputs": [],
55 "source": [
56 "words = set(w for w in words \n",
57 " if len(w) >= 4\n",
58 " if len(frozenset(w)) <= 7\n",
59 " if 's' not in w)"
60 ]
61 },
62 {
63 "cell_type": "code",
64 "execution_count": 8,
65 "id": "e1f9b35e",
66 "metadata": {},
67 "outputs": [
68 {
69 "data": {
70 "text/plain": [
71 "21661"
72 ]
73 },
74 "execution_count": 8,
75 "metadata": {},
76 "output_type": "execute_result"
77 }
78 ],
79 "source": [
80 "word_sets = collections.defaultdict(set)\n",
81 "for w in words:\n",
82 " letters = frozenset(w)\n",
83 " word_sets[letters].add(w)"
84 ]
85 },
86 {
87 "cell_type": "code",
88 "execution_count": 11,
89 "id": "95dc0eed-c759-481e-8598-9b861e3def8f",
90 "metadata": {},
91 "outputs": [],
92 "source": [
93 "def encode(letters):\n",
94 " encoded = 0\n",
95 " for l in string.ascii_lowercase:\n",
96 " encoded <<= 1\n",
97 " if l in letters:\n",
98 " encoded |= 1\n",
99 " return encoded"
100 ]
101 },
102 {
103 "cell_type": "code",
104 "execution_count": 12,
105 "id": "53d9ae67-a5b5-4d01-9e69-930acca023bc",
106 "metadata": {},
107 "outputs": [],
108 "source": [
109 "def decode(letter_bits):\n",
110 " letters = set()\n",
111 " for l in string.ascii_lowercase:\n",
112 " e = encode(l)\n",
113 " if (e & letter_bits):\n",
114 " letters.add(l)\n",
115 " return letters"
116 ]
117 },
118 {
119 "cell_type": "code",
120 "execution_count": 13,
121 "id": "ab295a0a-6cb0-4f24-b57b-cb9f99517067",
122 "metadata": {},
123 "outputs": [],
124 "source": [
125 "def subset(this, that):\n",
126 " return this & that == this"
127 ]
128 },
129 {
130 "cell_type": "code",
131 "execution_count": 19,
132 "id": "267130ba",
133 "metadata": {},
134 "outputs": [],
135 "source": [
136 "@dataclass(frozen=True, init=False)\n",
137 "class Honeycomb():\n",
138 " mandatory: int\n",
139 " letters: int\n",
140 " \n",
141 " def __init__(self, mandatory, letters):\n",
142 " object.__setattr__(self, 'mandatory', encode(mandatory))\n",
143 " object.__setattr__(self, 'letters', encode(letters))\n",
144 " \n",
145 " def __repr__(self):\n",
146 " return f'{\"\".join(decode(self.mandatory))}|{\"\".join(sorted(decode(self.letters) - set(decode(self.mandatory))))}'"
147 ]
148 },
149 {
150 "cell_type": "code",
151 "execution_count": 21,
152 "id": "bb848e88",
153 "metadata": {},
154 "outputs": [],
155 "source": [
156 "def present(target, honeycomb):\n",
157 " return (subset(honeycomb.mandatory, target)\n",
158 " and subset(target, honeycomb.letters))"
159 ]
160 },
161 {
162 "cell_type": "code",
163 "execution_count": 25,
164 "id": "3c6f212e",
165 "metadata": {},
166 "outputs": [],
167 "source": [
168 "def score(present_set):\n",
169 " score = 0\n",
170 " for w in word_sets[present_set]:\n",
171 " if len(w) == 4:\n",
172 " score += 1\n",
173 " else:\n",
174 " score += len(w)\n",
175 " if len(present_set) == 7:\n",
176 " score += len(word_sets[present_set]) * 7\n",
177 " return score"
178 ]
179 },
180 {
181 "cell_type": "code",
182 "execution_count": 28,
183 "id": "979d9ed5",
184 "metadata": {},
185 "outputs": [],
186 "source": [
187 "scored_sets = {s: score(s) for s in word_sets}\n",
188 "encoded_scored_sets = {encode(s): scored_sets[s] for s in scored_sets}"
189 ]
190 },
191 {
192 "cell_type": "code",
193 "execution_count": 30,
194 "id": "78d423a5",
195 "metadata": {},
196 "outputs": [],
197 "source": [
198 "def raw_score_honeycomb(honeycomb):\n",
199 " return sum(sc for letters, sc in scored_sets.items() \n",
200 " # if honeycomb.mandatory in letters\n",
201 " # if letters.issubset(honeycomb.letters)\n",
202 " if present(letters, honeycomb)\n",
203 " )"
204 ]
205 },
206 {
207 "cell_type": "code",
208 "execution_count": 31,
209 "id": "eb0d2af1-437b-4be5-8736-0a18022e9de2",
210 "metadata": {},
211 "outputs": [],
212 "source": [
213 "def score_honeycomb(honeycomb):\n",
214 " return sum(sc for letters, sc in encoded_scored_sets.items() \n",
215 " # if honeycomb.mandatory in letters\n",
216 " # if letters.issubset(honeycomb.letters)\n",
217 " if present(letters, honeycomb)\n",
218 " )"
219 ]
220 },
221 {
222 "cell_type": "code",
223 "execution_count": 40,
224 "id": "febee1c1",
225 "metadata": {},
226 "outputs": [
227 {
228 "name": "stdout",
229 "output_type": "stream",
230 "text": [
231 "pangram sets: 7986\n"
232 ]
233 }
234 ],
235 "source": [
236 "pangram_sets = [s for s in word_sets.keys() if len(s) == 7 if 's' not in s]"
237 ]
238 },
239 {
240 "cell_type": "code",
241 "execution_count": 41,
242 "id": "54a06bdf",
243 "metadata": {},
244 "outputs": [],
245 "source": [
246 "with open('pangaram_words.txt', 'w') as f:\n",
247 " for s in pangram_sets:\n",
248 " for w in word_sets[s]:\n",
249 " f.write(f'{w}\\n')"
250 ]
251 },
252 {
253 "cell_type": "code",
254 "execution_count": 42,
255 "id": "c0b0807c",
256 "metadata": {},
257 "outputs": [
258 {
259 "name": "stdout",
260 "output_type": "stream",
261 "text": [
262 "55902 honeycombs\n"
263 ]
264 }
265 ],
266 "source": [
267 "# ps_honeycombs = []\n",
268 "\n",
269 "# for ps in pangram_sets:\n",
270 "# for mand in ps:\n",
271 "# ps_honeycombs.append(Honeycomb(mandatory=mand, letters=ps))\n",
272 "\n",
273 "ps_honeycombs = [Honeycomb(mandatory=mand, letters=ps) \n",
274 " for ps in pangram_sets\n",
275 " for mand in ps]"
276 ]
277 },
278 {
279 "cell_type": "code",
280 "execution_count": 48,
281 "id": "914fece8",
282 "metadata": {},
283 "outputs": [
284 {
285 "data": {
286 "text/plain": [
287 "26"
288 ]
289 },
290 "execution_count": 48,
291 "metadata": {},
292 "output_type": "execute_result"
293 }
294 ],
295 "source": [
296 "partitioned_scored_sets = {encode(l): {s: encoded_scored_sets[s] \n",
297 " for s in encoded_scored_sets \n",
298 " if subset(encode(l), s)}\n",
299 " for l in string.ascii_lowercase}"
300 ]
301 },
302 {
303 "cell_type": "code",
304 "execution_count": 50,
305 "id": "f1454ccd",
306 "metadata": {},
307 "outputs": [],
308 "source": [
309 "def partitioned_score_honeycomb(honeycomb):\n",
310 " return sum(sc for letters, sc in partitioned_scored_sets[honeycomb.mandatory].items() \n",
311 " if subset(letters, honeycomb.letters)\n",
312 " )"
313 ]
314 },
315 {
316 "cell_type": "code",
317 "execution_count": 51,
318 "id": "7380dbd6",
319 "metadata": {},
320 "outputs": [
321 {
322 "name": "stdout",
323 "output_type": "stream",
324 "text": [
325 "r|aegint 3898\n",
326 "r|aegint 3898\n"
327 ]
328 },
329 {
330 "ename": "KeyboardInterrupt",
331 "evalue": "",
332 "output_type": "error",
333 "traceback": [
334 "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
335 "\u001b[0;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)",
336 "\u001b[0;32m/tmp/ipykernel_1590851/2427168464.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mget_ipython\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrun_cell_magic\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'timeit'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m''\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'best_honyecomb = max(ps_honeycombs, key=partitioned_score_honeycomb)\\nprint(best_honyecomb, partitioned_score_honeycomb(best_honyecomb))\\n'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
337 "\u001b[0;32m~/anaconda3/lib/python3.8/site-packages/IPython/core/interactiveshell.py\u001b[0m in \u001b[0;36mrun_cell_magic\u001b[0;34m(self, magic_name, line, cell)\u001b[0m\n\u001b[1;32m 2401\u001b[0m \u001b[0;32mwith\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mbuiltin_trap\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2402\u001b[0m \u001b[0margs\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mmagic_arg_s\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcell\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 2403\u001b[0;31m \u001b[0mresult\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mfn\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2404\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mresult\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2405\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
338 "\u001b[0;32m~/anaconda3/lib/python3.8/site-packages/decorator.py\u001b[0m in \u001b[0;36mfun\u001b[0;34m(*args, **kw)\u001b[0m\n\u001b[1;32m 230\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0mkwsyntax\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 231\u001b[0m \u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mkw\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mfix\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mkw\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0msig\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 232\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mcaller\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfunc\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m*\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mextras\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0margs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkw\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 233\u001b[0m \u001b[0mfun\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m__name__\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mfunc\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m__name__\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 234\u001b[0m \u001b[0mfun\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m__doc__\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mfunc\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m__doc__\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
339 "\u001b[0;32m~/anaconda3/lib/python3.8/site-packages/IPython/core/magic.py\u001b[0m in \u001b[0;36m<lambda>\u001b[0;34m(f, *a, **k)\u001b[0m\n\u001b[1;32m 185\u001b[0m \u001b[0;31m# but it's overkill for just that one bit of state.\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 186\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mmagic_deco\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0marg\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 187\u001b[0;31m \u001b[0mcall\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;32mlambda\u001b[0m \u001b[0mf\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m*\u001b[0m\u001b[0ma\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mk\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mf\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0ma\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mk\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 188\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 189\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mcallable\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0marg\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
340 "\u001b[0;32m~/anaconda3/lib/python3.8/site-packages/IPython/core/magics/execution.py\u001b[0m in \u001b[0;36mtimeit\u001b[0;34m(self, line, cell, local_ns)\u001b[0m\n\u001b[1;32m 1171\u001b[0m \u001b[0;32mbreak\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1172\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1173\u001b[0;31m \u001b[0mall_runs\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mtimer\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrepeat\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mrepeat\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mnumber\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 1174\u001b[0m \u001b[0mbest\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mmin\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mall_runs\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m/\u001b[0m \u001b[0mnumber\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1175\u001b[0m \u001b[0mworst\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mmax\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mall_runs\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m/\u001b[0m \u001b[0mnumber\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
341 "\u001b[0;32m~/anaconda3/lib/python3.8/timeit.py\u001b[0m in \u001b[0;36mrepeat\u001b[0;34m(self, repeat, number)\u001b[0m\n\u001b[1;32m 203\u001b[0m \u001b[0mr\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 204\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mrepeat\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 205\u001b[0;31m \u001b[0mt\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtimeit\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnumber\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 206\u001b[0m \u001b[0mr\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mt\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 207\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mr\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
342 "\u001b[0;32m~/anaconda3/lib/python3.8/site-packages/IPython/core/magics/execution.py\u001b[0m in \u001b[0;36mtimeit\u001b[0;34m(self, number)\u001b[0m\n\u001b[1;32m 167\u001b[0m \u001b[0mgc\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdisable\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 168\u001b[0m \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 169\u001b[0;31m \u001b[0mtiming\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0minner\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mit\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtimer\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 170\u001b[0m \u001b[0;32mfinally\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 171\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mgcold\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
343 "\u001b[0;32m<magic-timeit>\u001b[0m in \u001b[0;36minner\u001b[0;34m(_it, _timer)\u001b[0m\n",
344 "\u001b[0;32m/tmp/ipykernel_1590851/3736129931.py\u001b[0m in \u001b[0;36mpartitioned_score_honeycomb\u001b[0;34m(honeycomb)\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mpartitioned_score_honeycomb\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mhoneycomb\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m return sum(sc for letters, sc in partitioned_scored_sets[honeycomb.mandatory].items() \n\u001b[0m\u001b[1;32m 3\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0msubset\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mletters\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mhoneycomb\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mletters\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m )\n",
345 "\u001b[0;32m/tmp/ipykernel_1590851/3736129931.py\u001b[0m in \u001b[0;36m<genexpr>\u001b[0;34m(.0)\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mpartitioned_score_honeycomb\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mhoneycomb\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m return sum(sc for letters, sc in partitioned_scored_sets[honeycomb.mandatory].items() \n\u001b[0;32m----> 3\u001b[0;31m \u001b[0;32mif\u001b[0m \u001b[0msubset\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mletters\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mhoneycomb\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mletters\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 4\u001b[0m )\n",
346 "\u001b[0;32m/tmp/ipykernel_1590851/3073138769.py\u001b[0m in \u001b[0;36msubset\u001b[0;34m(this, that)\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0msubset\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mthis\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mthat\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mthis\u001b[0m \u001b[0;34m&\u001b[0m \u001b[0mthat\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0mthis\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
347 "\u001b[0;31mKeyboardInterrupt\u001b[0m: "
348 ]
349 }
350 ],
351 "source": [
352 "# %%timeit\n",
353 "# best_honyecomb = max(ps_honeycombs, key=partitioned_score_honeycomb)\n",
354 "# print(best_honyecomb, partitioned_score_honeycomb(best_honyecomb))"
355 ]
356 },
357 {
358 "cell_type": "code",
359 "execution_count": null,
360 "id": "0ecd0647-11df-4b5b-a487-cb30cfa080c0",
361 "metadata": {},
362 "outputs": [],
363 "source": [
364 "# %%timeit\n",
365 "best_honyecomb = max(ps_honeycombs, key=score_honeycomb)\n",
366 "print(best_honyecomb, score_honeycomb(best_honyecomb))"
367 ]
368 },
369 {
370 "cell_type": "code",
371 "execution_count": null,
372 "id": "7e4173b4-26a9-4198-b572-d57db321fe94",
373 "metadata": {
374 "lines_to_next_cell": 2
375 },
376 "outputs": [],
377 "source": [
378 "# cProfile.run('max(ps_honeycombs, key=score_honeycomb)')"
379 ]
380 },
381 {
382 "cell_type": "code",
383 "execution_count": null,
384 "id": "9a9070b7-a866-499e-a910-82337ecbd052",
385 "metadata": {},
386 "outputs": [],
387 "source": [
388 "# cProfile.run('max(ps_honeycombs, key=partitioned_score_honeycomb)')"
389 ]
390 },
391 {
392 "cell_type": "code",
393 "execution_count": null,
394 "id": "a77ce9b1-d5d8-4c8f-8666-5840af23c265",
395 "metadata": {},
396 "outputs": [],
397 "source": []
398 }
399 ],
400 "metadata": {
401 "jupytext": {
402 "formats": "ipynb,py:light"
403 },
404 "kernelspec": {
405 "display_name": "Python 3 (ipykernel)",
406 "language": "python",
407 "name": "python3"
408 },
409 "language_info": {
410 "codemirror_mode": {
411 "name": "ipython",
412 "version": 3
413 },
414 "file_extension": ".py",
415 "mimetype": "text/x-python",
416 "name": "python",
417 "nbconvert_exporter": "python",
418 "pygments_lexer": "ipython3",
419 "version": "3.8.8"
420 }
421 },
422 "nbformat": 4,
423 "nbformat_minor": 5
424 }