Done task 5
[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_no_re(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 " if length > start:\n",
57 " length = start\n",
58 " return start, end, length, times\n",
59 " return None, None, None, None"
60 ]
61 },
62 {
63 "cell_type": "code",
64 "execution_count": 5,
65 "metadata": {},
66 "outputs": [],
67 "source": [
68 "def expand_no_re(text, expansion_limit=None):\n",
69 " i = 1\n",
70 " s, e, l, n = find_expansion_mark(text)\n",
71 " while s and (expansion_limit is None or (expansion_limit is not None and i <= expansion_limit)):\n",
72 " text = text[:s-l] + text[s-l:s] * n + text[e+1:]\n",
73 " s, e, l, n = find_expansion_mark(text)\n",
74 " i += 1\n",
75 " return text"
76 ]
77 },
78 {
79 "cell_type": "code",
80 "execution_count": 6,
81 "metadata": {},
82 "outputs": [],
83 "source": [
84 "data_p_t = [l.strip() for l in open('../../data/05-instructions.txt')]"
85 ]
86 },
87 {
88 "cell_type": "code",
89 "execution_count": 7,
90 "metadata": {},
91 "outputs": [],
92 "source": [
93 "data_p = [decomment_no_re(l) for l in data_p_t]\n",
94 "data = [expand_no_re(l) for l in data_p]"
95 ]
96 },
97 {
98 "cell_type": "code",
99 "execution_count": 8,
100 "metadata": {},
101 "outputs": [
102 {
103 "data": {
104 "text/plain": [
105 "149043"
106 ]
107 },
108 "execution_count": 8,
109 "metadata": {},
110 "output_type": "execute_result"
111 }
112 ],
113 "source": [
114 "len([c for c in ''.join(data_p) if c not in string.whitespace])"
115 ]
116 },
117 {
118 "cell_type": "code",
119 "execution_count": 9,
120 "metadata": {},
121 "outputs": [
122 {
123 "data": {
124 "text/plain": [
125 "302266"
126 ]
127 },
128 "execution_count": 9,
129 "metadata": {},
130 "output_type": "execute_result"
131 }
132 ],
133 "source": [
134 "len([c for c in ''.join(data) if c not in string.whitespace])"
135 ]
136 },
137 {
138 "cell_type": "code",
139 "execution_count": 10,
140 "metadata": {},
141 "outputs": [],
142 "source": [
143 "data_s_p_t = open('../../data/05-instructions.txt').read()\n",
144 "data_s_p = decomment_no_re(data_s_p_t)\n",
145 "data_s = expand_no_re(data_s_p)"
146 ]
147 },
148 {
149 "cell_type": "code",
150 "execution_count": 11,
151 "metadata": {},
152 "outputs": [
153 {
154 "data": {
155 "text/plain": [
156 "149043"
157 ]
158 },
159 "execution_count": 11,
160 "metadata": {},
161 "output_type": "execute_result"
162 }
163 ],
164 "source": [
165 "sum(1 for c in data_s_p if c not in string.whitespace)"
166 ]
167 },
168 {
169 "cell_type": "code",
170 "execution_count": 12,
171 "metadata": {},
172 "outputs": [
173 {
174 "data": {
175 "text/plain": [
176 "302266"
177 ]
178 },
179 "execution_count": 12,
180 "metadata": {},
181 "output_type": "execute_result"
182 }
183 ],
184 "source": [
185 "sum(1 for c in data_s if c not in string.whitespace)"
186 ]
187 },
188 {
189 "cell_type": "code",
190 "execution_count": 13,
191 "metadata": {},
192 "outputs": [
193 {
194 "name": "stdout",
195 "output_type": "stream",
196 "text": [
197 "23.7 ms ± 1.79 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)\n"
198 ]
199 }
200 ],
201 "source": [
202 "%%timeit\n",
203 "data_p = [decomment_no_re(l) for l in data_p_t]\n",
204 "data = [expand_no_re(l) for l in data_p]"
205 ]
206 },
207 {
208 "cell_type": "code",
209 "execution_count": 14,
210 "metadata": {},
211 "outputs": [
212 {
213 "name": "stdout",
214 "output_type": "stream",
215 "text": [
216 "360 ms ± 6.67 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
217 ]
218 }
219 ],
220 "source": [
221 "%%timeit\n",
222 "data_s_p = decomment_no_re(data_s_p_t)\n",
223 "data_s = expand_no_re(data_s_p)"
224 ]
225 },
226 {
227 "cell_type": "code",
228 "execution_count": null,
229 "metadata": {},
230 "outputs": [],
231 "source": []
232 }
233 ],
234 "metadata": {
235 "kernelspec": {
236 "display_name": "Python 3",
237 "language": "python",
238 "name": "python3"
239 },
240 "language_info": {
241 "codemirror_mode": {
242 "name": "ipython",
243 "version": 3
244 },
245 "file_extension": ".py",
246 "mimetype": "text/x-python",
247 "name": "python",
248 "nbconvert_exporter": "python",
249 "pygments_lexer": "ipython3",
250 "version": "3.6.6"
251 }
252 },
253 "nbformat": 4,
254 "nbformat_minor": 2
255 }