Tweaked syntax layout in successors
[summerofcode2018soln.git] / src / task5 / task5-nore.ipynb
1 {
2 "cells": [
3 {
4 "cell_type": "code",
5 "execution_count": 1,
6 "metadata": {},
7 "outputs": [],
8 "source": [
9 "import string"
10 ]
11 },
12 {
13 "cell_type": "code",
14 "execution_count": 2,
15 "metadata": {},
16 "outputs": [],
17 "source": [
18 "def find_comment(text):\n",
19 " start = text.find('<')\n",
20 " if start > -1:\n",
21 " end = text.find('>', start + 1)\n",
22 " if end > -1:\n",
23 " return start, end\n",
24 " return None, None"
25 ]
26 },
27 {
28 "cell_type": "code",
29 "execution_count": 3,
30 "metadata": {},
31 "outputs": [],
32 "source": [
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",
38 " return text"
39 ]
40 },
41 {
42 "cell_type": "code",
43 "execution_count": 4,
44 "metadata": {},
45 "outputs": [],
46 "source": [
47 "def find_expansion_mark(text):\n",
48 " start = text.find(':')\n",
49 " if start > -1:\n",
50 " mid = text.find(':', start + 1)\n",
51 " if mid > -1:\n",
52 " end = text.find(':', mid + 1)\n",
53 " if end > -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"
58 ]
59 },
60 {
61 "cell_type": "code",
62 "execution_count": 5,
63 "metadata": {},
64 "outputs": [],
65 "source": [
66 "def expand(text, expansion_limit=None):\n",
67 " i = 1\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",
70 " if l > s: l = s\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",
73 " i += 1\n",
74 " return text"
75 ]
76 },
77 {
78 "cell_type": "code",
79 "execution_count": 6,
80 "metadata": {},
81 "outputs": [],
82 "source": [
83 "data_p_t = [l.strip() for l in open('../../data/05-instructions.txt')]"
84 ]
85 },
86 {
87 "cell_type": "code",
88 "execution_count": 7,
89 "metadata": {},
90 "outputs": [],
91 "source": [
92 "data_p = [decomment(l) for l in data_p_t]\n",
93 "data = [expand(l) for l in data_p]"
94 ]
95 },
96 {
97 "cell_type": "code",
98 "execution_count": 8,
99 "metadata": {},
100 "outputs": [
101 {
102 "data": {
103 "text/plain": [
104 "149043"
105 ]
106 },
107 "execution_count": 8,
108 "metadata": {},
109 "output_type": "execute_result"
110 }
111 ],
112 "source": [
113 "len([c for c in ''.join(data_p) if c not in string.whitespace])"
114 ]
115 },
116 {
117 "cell_type": "code",
118 "execution_count": 9,
119 "metadata": {},
120 "outputs": [
121 {
122 "data": {
123 "text/plain": [
124 "302266"
125 ]
126 },
127 "execution_count": 9,
128 "metadata": {},
129 "output_type": "execute_result"
130 }
131 ],
132 "source": [
133 "len([c for c in ''.join(data) if c not in string.whitespace])"
134 ]
135 },
136 {
137 "cell_type": "code",
138 "execution_count": 10,
139 "metadata": {},
140 "outputs": [],
141 "source": [
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)"
145 ]
146 },
147 {
148 "cell_type": "code",
149 "execution_count": 11,
150 "metadata": {},
151 "outputs": [
152 {
153 "data": {
154 "text/plain": [
155 "149043"
156 ]
157 },
158 "execution_count": 11,
159 "metadata": {},
160 "output_type": "execute_result"
161 }
162 ],
163 "source": [
164 "sum(1 for c in data_s_p if c not in string.whitespace)"
165 ]
166 },
167 {
168 "cell_type": "code",
169 "execution_count": 12,
170 "metadata": {},
171 "outputs": [
172 {
173 "data": {
174 "text/plain": [
175 "302266"
176 ]
177 },
178 "execution_count": 12,
179 "metadata": {},
180 "output_type": "execute_result"
181 }
182 ],
183 "source": [
184 "sum(1 for c in data_s if c not in string.whitespace)"
185 ]
186 },
187 {
188 "cell_type": "code",
189 "execution_count": 13,
190 "metadata": {},
191 "outputs": [
192 {
193 "name": "stdout",
194 "output_type": "stream",
195 "text": [
196 "22.7 ms ± 1.6 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)\n"
197 ]
198 }
199 ],
200 "source": [
201 "%%timeit\n",
202 "data_p = [decomment(l) for l in data_p_t]\n",
203 "data = [expand(l) for l in data_p]"
204 ]
205 },
206 {
207 "cell_type": "code",
208 "execution_count": 14,
209 "metadata": {},
210 "outputs": [
211 {
212 "name": "stdout",
213 "output_type": "stream",
214 "text": [
215 "354 ms ± 7.34 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
216 ]
217 }
218 ],
219 "source": [
220 "%%timeit\n",
221 "data_s_p = decomment(data_s_p_t)\n",
222 "data_s = expand(data_s_p)"
223 ]
224 },
225 {
226 "cell_type": "code",
227 "execution_count": null,
228 "metadata": {},
229 "outputs": [],
230 "source": []
231 }
232 ],
233 "metadata": {
234 "kernelspec": {
235 "display_name": "Python 3",
236 "language": "python",
237 "name": "python3"
238 },
239 "language_info": {
240 "codemirror_mode": {
241 "name": "ipython",
242 "version": 3
243 },
244 "file_extension": ".py",
245 "mimetype": "text/x-python",
246 "name": "python",
247 "nbconvert_exporter": "python",
248 "pygments_lexer": "ipython3",
249 "version": "3.6.6"
250 }
251 },
252 "nbformat": 4,
253 "nbformat_minor": 2
254 }