18 "def find_comment(text):\n",
19 " start = text.find('<')\n",
21 " end = text.find('>', start + 1)\n",
23 " return start, end\n",
33 "def decomment(text):\n",
34 " s, e = find_comment(text)\n",
35 " while s is not None:\n",
36 " text = text[:s] + text[e+1:]\n",
37 " s, e = find_comment(text)\n",
47 "def find_expansion_mark(text):\n",
48 " start = text.find(':')\n",
50 " mid = text.find(':', start + 1)\n",
52 " end = text.find(':', mid + 1)\n",
54 " length = int(text[start+1:mid])\n",
55 " times = int(text[mid+1:end])\n",
56 " return start, end, length, times\n",
57 " return None, None, None, None"
66 "def expand(text, expansion_limit=None):\n",
68 " s, e, l, n = find_expansion_mark(text)\n",
69 " while s is not None and (expansion_limit is None or (expansion_limit is not None and i <= expansion_limit)):\n",
71 " text = text[:s-l] + text[s-l:s] * n + text[e+1:]\n",
72 " s, e, l, n = find_expansion_mark(text)\n",
83 "data_p_t = [l.strip() for l in open('../../data/05-instructions.txt')]"
92 "data_p = [decomment(l) for l in data_p_t]\n",
93 "data = [expand(l) for l in data_p]"
107 "execution_count": 8,
109 "output_type": "execute_result"
113 "len([c for c in ''.join(data_p) if c not in string.whitespace])"
118 "execution_count": 9,
127 "execution_count": 9,
129 "output_type": "execute_result"
133 "len([c for c in ''.join(data) if c not in string.whitespace])"
138 "execution_count": 10,
142 "data_s_p_t = open('../../data/05-instructions.txt').read()\n",
143 "data_s_p = decomment(data_s_p_t)\n",
144 "data_s = expand(data_s_p)"
149 "execution_count": 11,
158 "execution_count": 11,
160 "output_type": "execute_result"
164 "sum(1 for c in data_s_p if c not in string.whitespace)"
169 "execution_count": 12,
178 "execution_count": 12,
180 "output_type": "execute_result"
184 "sum(1 for c in data_s if c not in string.whitespace)"
189 "execution_count": 13,
194 "output_type": "stream",
196 "22.7 ms ± 1.6 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)\n"
202 "data_p = [decomment(l) for l in data_p_t]\n",
203 "data = [expand(l) for l in data_p]"
208 "execution_count": 14,
213 "output_type": "stream",
215 "354 ms ± 7.34 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
221 "data_s_p = decomment(data_s_p_t)\n",
222 "data_s = expand(data_s_p)"
227 "execution_count": null,
235 "display_name": "Python 3",
236 "language": "python",
244 "file_extension": ".py",
245 "mimetype": "text/x-python",
247 "nbconvert_exporter": "python",
248 "pygments_lexer": "ipython3",