{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import string" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "def find_comment(text):\n", " start = text.find('<')\n", " if start > -1:\n", " end = text.find('>', start + 1)\n", " if end > -1:\n", " return start, end\n", " return None, None" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "def decomment_no_re(text):\n", " s, e = find_comment(text)\n", " while s is not None:\n", " text = text[:s] + text[e+1:]\n", " s, e = find_comment(text)\n", " return text" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "def find_expansion_mark(text):\n", " start = text.find(':')\n", " if start > -1:\n", " mid = text.find(':', start + 1)\n", " if mid > -1:\n", " end = text.find(':', mid + 1)\n", " if end > -1:\n", " length = int(text[start+1:mid])\n", " times = int(text[mid+1:end])\n", " if length > start:\n", " length = start\n", " return start, end, length, times\n", " return None, None, None, None" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "def expand_no_re(text, expansion_limit=None):\n", " i = 1\n", " s, e, l, n = find_expansion_mark(text)\n", " while s and (expansion_limit is None or (expansion_limit is not None and i <= expansion_limit)):\n", " text = text[:s-l] + text[s-l:s] * n + text[e+1:]\n", " s, e, l, n = find_expansion_mark(text)\n", " i += 1\n", " return text" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "data_p_t = [l.strip() for l in open('../../data/05-instructions.txt')]" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "data_p = [decomment_no_re(l) for l in data_p_t]\n", "data = [expand_no_re(l) for l in data_p]" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "149043" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len([c for c in ''.join(data_p) if c not in string.whitespace])" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "302266" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len([c for c in ''.join(data) if c not in string.whitespace])" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "data_s_p_t = open('../../data/05-instructions.txt').read()\n", "data_s_p = decomment_no_re(data_s_p_t)\n", "data_s = expand_no_re(data_s_p)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "149043" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sum(1 for c in data_s_p if c not in string.whitespace)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "302266" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sum(1 for c in data_s if c not in string.whitespace)" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "23.7 ms ± 1.79 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)\n" ] } ], "source": [ "%%timeit\n", "data_p = [decomment_no_re(l) for l in data_p_t]\n", "data = [expand_no_re(l) for l in data_p]" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "360 ms ± 6.67 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n" ] } ], "source": [ "%%timeit\n", "data_s_p = decomment_no_re(data_s_p_t)\n", "data_s = expand_no_re(data_s_p)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.6" } }, "nbformat": 4, "nbformat_minor": 2 }