13 "[50, 44, 11, 49, 42, 46, 18, 32, 26, 40, 21, 7, 18, 43, 10, 47, 36, 24, 22, 40]"
18 "output_type": "execute_result"
22 "containers = [int(c.strip()) for c in open('advent17.txt').readlines()]\n",
39 "execution_count": 26,
50 "execution_count": 26,
52 "output_type": "execute_result"
57 "for c in containers:\n",
58 " new_partials = []\n",
59 " for p in partials:\n",
60 " if sum(p) + c <= target:\n",
61 " new_partials += [[c] + p]\n",
62 " new_partials += [p]\n",
64 " new_partials += [[c]]\n",
65 " partials = new_partials\n",
66 "solutions = list(filter(lambda p: sum(p) == target, partials))\n",
72 "execution_count": 28,
83 "execution_count": 28,
85 "output_type": "execute_result"
89 "shortest_solution = min(len(s) for s in solutions)\n",
95 "execution_count": 29,
104 "[[7, 49, 44, 50],\n",
105 " [10, 46, 44, 50],\n",
106 " [24, 32, 44, 50],\n",
107 " [40, 49, 11, 50],\n",
108 " [40, 49, 11, 50],\n",
109 " [47, 42, 11, 50],\n",
110 " [43, 46, 11, 50],\n",
111 " [40, 18, 42, 50],\n",
112 " [40, 18, 42, 50],\n",
113 " [26, 32, 42, 50],\n",
114 " [18, 40, 42, 50],\n",
115 " [40, 18, 42, 50],\n",
116 " [22, 36, 42, 50],\n",
117 " [36, 18, 46, 50],\n",
118 " [22, 32, 46, 50],\n",
119 " [47, 7, 46, 50],\n",
120 " [36, 18, 46, 50],\n",
121 " [47, 21, 32, 50],\n",
122 " [24, 36, 40, 50],\n",
123 " [36, 43, 21, 50],\n",
124 " [47, 10, 43, 50],\n",
125 " [40, 24, 36, 50],\n",
126 " [46, 49, 11, 44],\n",
127 " [36, 21, 49, 44],\n",
128 " [47, 10, 49, 44],\n",
129 " [18, 46, 42, 44],\n",
130 " [18, 46, 42, 44],\n",
131 " [24, 40, 42, 44],\n",
132 " [43, 21, 42, 44],\n",
133 " [40, 24, 42, 44],\n",
134 " [24, 36, 46, 44],\n",
135 " [40, 40, 26, 44],\n",
136 " [47, 43, 49, 11],\n",
137 " [43, 40, 18, 49],\n",
138 " [40, 43, 18, 49],\n",
139 " [36, 47, 18, 49],\n",
140 " [43, 26, 32, 49],\n",
141 " [22, 47, 32, 49],\n",
142 " [40, 21, 40, 49],\n",
143 " [43, 18, 40, 49],\n",
144 " [40, 43, 18, 49],\n",
145 " [36, 47, 18, 49],\n",
146 " [22, 36, 43, 49],\n",
147 " [36, 26, 46, 42],\n",
148 " [22, 40, 46, 42],\n",
149 " [40, 22, 46, 42],\n",
150 " [47, 43, 18, 42],\n",
151 " [36, 40, 32, 42],\n",
152 " [40, 36, 32, 42],\n",
153 " [47, 21, 40, 42],\n",
154 " [40, 47, 21, 42],\n",
155 " [47, 43, 18, 42],\n",
156 " [43, 21, 40, 46],\n",
157 " [40, 24, 40, 46],\n",
158 " [40, 43, 21, 46],\n",
159 " [36, 47, 21, 46],\n",
163 "execution_count": 29,
165 "output_type": "execute_result"
169 "[s for s in solutions if len(s) == shortest_solution]"
174 "execution_count": 31,
185 "execution_count": 31,
187 "output_type": "execute_result"
191 "len([s for s in solutions if len(s) == shortest_solution])"
196 "execution_count": 66,
207 "execution_count": 63,
213 "def int_to_bitstring(i, l):\n",
215 " for _ in range(l):\n",
217 " bitstring.append(True)\n",
219 " bitstring.append(False)\n",
221 " return reversed(bitstring)"
226 "execution_count": 64,
234 "[True, False, False, False, True, True, False, False]"
237 "execution_count": 64,
239 "output_type": "execute_result"
243 "list(int_to_bitstring(140, 8))"
248 "execution_count": 59,
259 "execution_count": 59,
261 "output_type": "execute_result"
265 "import itertools\n",
267 "c_small = containers[:5]\n",
268 "for i in range(2**len(c_small)):\n",
269 " mask = int_to_bitstring(i, len(c_small))\n",
270 " cs = list(itertools.compress(c_small, mask))\n",
271 " if sum(cs) == target:\n",
278 "execution_count": 65,
289 "execution_count": 65,
291 "output_type": "execute_result"
295 "import itertools\n",
297 "for i in range(2**len(containers)):\n",
298 " mask = int_to_bitstring(i, len(containers))\n",
299 " cs = list(itertools.compress(containers, mask))\n",
300 " if sum(cs) == target:\n",
307 "execution_count": null,
317 "display_name": "Python 3",
318 "language": "python",
326 "file_extension": ".py",
327 "mimetype": "text/x-python",
329 "nbconvert_exporter": "python",
330 "pygments_lexer": "ipython3",