12 "pi13 = [l.strip() for l in open('advent13.txt').read().splitlines()]"
26 "{'Alice': {'Bob': 54,\n",
32 " 'Mallory': -94},\n",
33 " 'Bob': {'Alice': 3,\n",
40 " 'Carol': {'Alice': -83,\n",
47 " 'David': {'Alice': 67,\n",
54 " 'Eric': {'Alice': -51,\n",
61 " 'Frank': {'Alice': 51,\n",
68 " 'George': {'Alice': -14,\n",
74 " 'Mallory': -17},\n",
75 " 'Mallory': {'Alice': -36,\n",
86 "output_type": "execute_result"
90 "# import collections\n",
92 "# Seating = collections.namedtuple('Seating', ['person', 'neighbour', 'gain'])\n",
95 "for line in pi13:\n",
96 " words = line.split()\n",
97 " gain = int(words[3])\n",
98 " if words[2] == 'lose':\n",
100 " if words[0] not in preferences:\n",
101 " preferences[words[0]] = {}\n",
102 " preferences[words[0]][words[-1][:-1]] = gain\n",
108 "execution_count": 7,
114 "def score(seating, prefs):\n",
116 " for i in range(len(seating)):\n",
117 " l = (i - 1) % len(seating)\n",
118 " r = (i + 1) % len(seating)\n",
119 " happiness += prefs[seating[i]][seating[l]]\n",
120 " happiness += prefs[seating[i]][seating[r]]\n",
121 " #print(i, l, r, ':', seating[i],'->', seating[l], preferences[seating[i]][seating[l]], \n",
122 " # ';', seating[i],'->', seating[r], preferences[seating[i]][seating[r]])\n",
128 "execution_count": 8,
139 "execution_count": 8,
141 "output_type": "execute_result"
145 "s = list(preferences.keys())\n",
146 "score(list(preferences.keys()), preferences)"
151 "execution_count": 9,
163 "execution_count": 9,
165 "output_type": "execute_result"
169 "import itertools\n",
171 "seatings = itertools.permutations(list(preferences.keys()))\n",
173 "max(score(s, preferences) for s in seatings)"
178 "execution_count": 10,
187 "{'Alice': {'Bob': 54,\n",
193 " 'Mallory': -94},\n",
194 " 'Bob': {'Alice': 3,\n",
200 " 'Mallory': 11},\n",
201 " 'Carol': {'Alice': -83,\n",
207 " 'Mallory': 29},\n",
208 " 'David': {'Alice': 67,\n",
215 " 'Eric': {'Alice': -51,\n",
221 " 'Mallory': 60},\n",
222 " 'Frank': {'Alice': 51,\n",
228 " 'Mallory': -3},\n",
229 " 'George': {'Alice': -14,\n",
235 " 'Mallory': -17},\n",
236 " 'Mallory': {'Alice': -36,\n",
245 "execution_count": 10,
247 "output_type": "execute_result"
251 "# import collections\n",
253 "# Seating = collections.namedtuple('Seating', ['person', 'neighbour', 'gain'])\n",
255 "preferences2 = {}\n",
256 "for line in pi13:\n",
257 " words = line.split()\n",
258 " gain = int(words[3])\n",
259 " if words[2] == 'lose':\n",
261 " if words[0] not in preferences2:\n",
262 " preferences2[words[0]] = {}\n",
263 " preferences2[words[0]][words[-1][:-1]] = gain\n",
269 "execution_count": 11,
278 "{'Alice': {'Bob': 54,\n",
284 " 'Mallory': -94,\n",
286 " 'Bob': {'Alice': 3,\n",
294 " 'Carol': {'Alice': -83,\n",
302 " 'David': {'Alice': 67,\n",
310 " 'Eric': {'Alice': -51,\n",
318 " 'Frank': {'Alice': 51,\n",
326 " 'George': {'Alice': -14,\n",
332 " 'Mallory': -17,\n",
334 " 'Mallory': {'Alice': -36,\n",
342 " 'me': {'Alice': 0,\n",
353 "execution_count": 11,
355 "output_type": "execute_result"
359 "preferences2['me'] = {other: 0 for other in preferences2}\n",
360 "for other in preferences2:\n",
361 " preferences2[other]['me'] = 0\n",
367 "execution_count": 12,
378 "execution_count": 12,
380 "output_type": "execute_result"
384 "seatings = itertools.permutations(list(preferences2.keys()))\n",
386 "max(score(s, preferences2) for s in seatings)"
391 "execution_count": null,
401 "display_name": "Python 3",
402 "language": "python",
410 "file_extension": ".py",
411 "mimetype": "text/x-python",
413 "nbconvert_exporter": "python",
414 "pygments_lexer": "ipython3",