19 "comment_mark = re.compile('<[^>]*>') # A < followed by some things that aren't > followed by >\n",
20 "expand_mark = re.compile(':(\\d+):(\\d+):') # A : followed by some digits, followed by another :, then some more digits, then a final :"
29 "def decomment(text):\n",
30 " m = comment_mark.search(text)\n",
34 " text = text[:s] + text[e:]\n",
35 " m = comment_mark.search(text)\n",
45 "def expand(text, expansion_limit=None):\n",
47 " m = expand_mark.search(text)\n",
48 " while m and (expansion_limit is None or (expansion_limit is not None and i <= expansion_limit)):\n",
49 " l = int(m.group(1))\n",
50 " n = int(m.group(2))\n",
55 " text = text[:s-l] + text[s-l:s] * n + text[e:]\n",
56 " m = expand_mark.search(text)\n",
67 "data_p_t = [l.strip() for l in open('../../data/05-instructions.txt')]"
76 "data_p = [decomment(l) for l in data_p_t]\n",
77 "data = [expand(l) for l in data_p]"
93 "output_type": "execute_result"
97 "len([c for c in ''.join(data_p) if c not in string.whitespace])"
102 "execution_count": 8,
111 "execution_count": 8,
113 "output_type": "execute_result"
117 "len([c for c in ''.join(data) if c not in string.whitespace])"
122 "execution_count": 9,
126 "data_s_p_t = open('../../data/05-instructions.txt').read()\n",
127 "data_s_p = decomment(data_s_p_t)\n",
128 "data_s = expand(data_s_p)"
133 "execution_count": 10,
142 "execution_count": 10,
144 "output_type": "execute_result"
148 "sum(1 for c in data_s_p if c not in string.whitespace)"
153 "execution_count": 11,
162 "execution_count": 11,
164 "output_type": "execute_result"
168 "sum(1 for c in data_s if c not in string.whitespace)"
173 "execution_count": 12,
178 "output_type": "stream",
180 "27.7 ms ± 908 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)\n"
186 "data_p = [decomment(l) for l in data_p_t]\n",
187 "data = [expand(l) for l in data_p]"
192 "execution_count": 13,
197 "output_type": "stream",
199 "861 ms ± 7.25 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
205 "data_s_p = decomment(data_s_p_t)\n",
206 "data_s = expand(data_s_p)"
211 "execution_count": null,
219 "display_name": "Python 3",
220 "language": "python",
228 "file_extension": ".py",
229 "mimetype": "text/x-python",
231 "nbconvert_exporter": "python",
232 "pygments_lexer": "ipython3",