Skip to content

Commit f126925

Browse files
committed
iterative solution
1 parent e1e9974 commit f126925

File tree

1 file changed

+132
-3
lines changed

1 file changed

+132
-3
lines changed

2018/Playground.ipynb

Lines changed: 132 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"cells": [
33
{
44
"cell_type": "code",
5-
"execution_count": 117,
5+
"execution_count": 45,
66
"metadata": {},
77
"outputs": [],
88
"source": [
@@ -15,11 +15,11 @@
1515
},
1616
{
1717
"cell_type": "code",
18-
"execution_count": 101,
18+
"execution_count": 20,
1919
"metadata": {},
2020
"outputs": [],
2121
"source": [
22-
"# data = \"2 3 0 3 10 11 12 1 1 0 1 99 2 1 1 2\""
22+
"data = \"2 3 0 3 10 11 12 1 1 0 1 99 2 1 1 2\""
2323
]
2424
},
2525
{
@@ -112,6 +112,135 @@
112112
"print(f\"Part 2:\", tree.part_2())"
113113
]
114114
},
115+
{
116+
"cell_type": "code",
117+
"execution_count": 21,
118+
"metadata": {},
119+
"outputs": [
120+
{
121+
"name": "stdout",
122+
"output_type": "stream",
123+
"text": [
124+
"Part 1: 138\n"
125+
]
126+
}
127+
],
128+
"source": [
129+
"def iter_process(N):\n",
130+
" stack = []\n",
131+
" meta = []\n",
132+
" stack.append(N[:2])\n",
133+
" N = N[2:]\n",
134+
"\n",
135+
" while len(stack):\n",
136+
" if stack[-1][0] == 0:\n",
137+
" _, num_meta = stack.pop()\n",
138+
" meta += N[:num_meta]\n",
139+
" N = N[num_meta:]\n",
140+
" else:\n",
141+
" stack[-1][0] -= 1\n",
142+
" stack.append(N[:2])\n",
143+
" N = N[2:]\n",
144+
" \n",
145+
" print('Part 1:', sum(meta))\n",
146+
" \n",
147+
"iter_process(list(map(int, data.split())))"
148+
]
149+
},
150+
{
151+
"cell_type": "code",
152+
"execution_count": 80,
153+
"metadata": {},
154+
"outputs": [
155+
{
156+
"name": "stdout",
157+
"output_type": "stream",
158+
"text": [
159+
"Part 1: 36566\n",
160+
"Part 2: 30548\n"
161+
]
162+
}
163+
],
164+
"source": [
165+
"def iter_process(N, part):\n",
166+
" num_nodes, num_meta, *N = N\n",
167+
" stack = [[num_nodes, num_meta, []]]\n",
168+
"\n",
169+
" while True:\n",
170+
" if stack[-1][0] == 0:\n",
171+
" _, num_meta, meta = stack.pop()\n",
172+
" \n",
173+
" if part == 1:\n",
174+
" tot = sum(meta) + sum(N[:num_meta])\n",
175+
" elif part == 2:\n",
176+
" tot = sum([meta[i-1] for i in N[:num_meta] if 0 < i <= len(meta)])\n",
177+
" \n",
178+
" if not len(stack):\n",
179+
" return tot\n",
180+
" \n",
181+
" stack[-1][2].append(tot)\n",
182+
" N = N[num_meta:]\n",
183+
" else:\n",
184+
" stack[-1][0] -= 1\n",
185+
" num_nodes, num_meta, *N = N\n",
186+
"\n",
187+
" if num_nodes == 0:\n",
188+
" stack[-1][2].append(sum(N[:num_meta]))\n",
189+
" N = N[num_meta:]\n",
190+
" else:\n",
191+
" stack.append([num_nodes, num_meta, []])\n",
192+
" \n",
193+
" \n",
194+
"print(\"Part 1:\", iter_process(map(int, data.split()), part=1))\n",
195+
"print(\"Part 2:\", iter_process(map(int, data.split()), part=2))"
196+
]
197+
},
198+
{
199+
"cell_type": "code",
200+
"execution_count": 106,
201+
"metadata": {},
202+
"outputs": [
203+
{
204+
"name": "stdout",
205+
"output_type": "stream",
206+
"text": [
207+
"Part 1: 36566\n",
208+
"Part 2: 30548\n"
209+
]
210+
}
211+
],
212+
"source": [
213+
"# Factored-to-death version:\n",
214+
"\n",
215+
"def iter_process(N, part):\n",
216+
" stack = [[1, 0, []]]\n",
217+
" \n",
218+
" while True:\n",
219+
" if stack[-1][0]:\n",
220+
" stack[-1][0] -= 1\n",
221+
" num_nodes, num_meta, *N = N\n",
222+
" if num_nodes == 0:\n",
223+
" tot = sum(N[:num_meta])\n",
224+
" else:\n",
225+
" num_nodes, num_meta, meta = stack.pop()\n",
226+
" if not len(stack):\n",
227+
" return meta[0] \n",
228+
" if part == 1:\n",
229+
" tot = sum(meta) + sum(N[:num_meta])\n",
230+
" elif part == 2:\n",
231+
" tot = sum(meta[i-1] for i in N[:num_meta] if 0 < i <= len(meta))\n",
232+
"\n",
233+
" if num_nodes == 0:\n",
234+
" stack[-1][2].append(tot)\n",
235+
" N = N[num_meta:]\n",
236+
" else:\n",
237+
" stack.append([num_nodes, num_meta, []])\n",
238+
" \n",
239+
" \n",
240+
"print(\"Part 1:\", iter_process(map(int, data.split()), part=1))\n",
241+
"print(\"Part 2:\", iter_process(map(int, data.split()), part=2))"
242+
]
243+
},
115244
{
116245
"cell_type": "code",
117246
"execution_count": null,

0 commit comments

Comments
 (0)