Browse Source

Uglify code in the name of performance

master
Chris Smith 5 years ago
parent
commit
b9fd54bc47
2 changed files with 41 additions and 41 deletions
  1. 40
    39
      2018.ipynb
  2. 1
    2
      docker/spliterator.py

+ 40
- 39
2018.ipynb View File

@@ -80,18 +80,16 @@
80 80
    },
81 81
    "outputs": [
82 82
     {
83
-     "data": {
84
-      "text/plain": [
85
-       "531"
86
-      ]
87
-     },
88
-     "execution_count": 2,
89
-     "metadata": {},
90
-     "output_type": "execute_result"
83
+     "name": "stdout",
84
+     "output_type": "stream",
85
+     "text": [
86
+      "531\n"
87
+     ]
91 88
     }
92 89
    ],
93 90
    "source": [
94
-    "sum(map(int, get_input(1)))"
91
+    "frequencies = list(map(int, get_input(1)))\n",
92
+    "print(sum(frequencies))"
95 93
    ]
96 94
   },
97 95
   {
@@ -130,14 +128,11 @@
130 128
    },
131 129
    "outputs": [
132 130
     {
133
-     "data": {
134
-      "text/plain": [
135
-       "76787"
136
-      ]
137
-     },
138
-     "execution_count": 3,
139
-     "metadata": {},
140
-     "output_type": "execute_result"
131
+     "name": "stdout",
132
+     "output_type": "stream",
133
+     "text": [
134
+      "76787\n"
135
+     ]
141 136
     }
142 137
    ],
143 138
    "source": [
@@ -150,7 +145,7 @@
150 145
     "    return None\n",
151 146
     "\n",
152 147
     "\n",
153
-    "first_duplicate(it.accumulate(it.cycle(map(int, get_input(1)))))"
148
+    "print(first_duplicate(it.accumulate(it.cycle(frequencies))))"
154 149
    ]
155 150
   },
156 151
   {
@@ -192,21 +187,20 @@
192 187
    },
193 188
    "outputs": [
194 189
     {
195
-     "data": {
196
-      "text/plain": [
197
-       "4920"
198
-      ]
199
-     },
200
-     "execution_count": 4,
201
-     "metadata": {},
202
-     "output_type": "execute_result"
190
+     "name": "stdout",
191
+     "output_type": "stream",
192
+     "text": [
193
+      "4920\n"
194
+     ]
203 195
     }
204 196
    ],
205 197
    "source": [
198
+    "boxes = list(get_input(2))\n",
199
+    "\n",
206 200
     "def count_dupes(str):\n",
207 201
     "    return [any(str.count(x) == i for x in str) for i in (2,3)]\n",
208 202
     "\n",
209
-    "op.mul(*map(sum, zip(*map(count_dupes, get_input(2)))))"
203
+    "print(op.mul(*map(sum, zip(*map(count_dupes, boxes)))))"
210 204
    ]
211 205
   },
212 206
   {
@@ -238,21 +232,28 @@
238 232
    },
239 233
    "outputs": [
240 234
     {
241
-     "data": {
242
-      "text/plain": [
243
-       "'fonbwmjquwtapeyzikghtvdxl'"
244
-      ]
245
-     },
246
-     "execution_count": 5,
247
-     "metadata": {},
248
-     "output_type": "execute_result"
235
+     "name": "stdout",
236
+     "output_type": "stream",
237
+     "text": [
238
+      "fonbwmjquwtapeyzikghtvdxl\n"
239
+     ]
249 240
     }
250 241
    ],
251 242
    "source": [
252
-    "def common_letters(str1, str2):\n",
253
-    "    return ''.join(i for i, j in zip(str1, str2) if i == j)\n",
254
-    "\n",
255
-    "max(it.starmap(common_letters, it.combinations(get_input(2), 2)), key=len)"
243
+    "for box1, box2 in it.combinations(boxes, 2):\n",
244
+    "    common = ''\n",
245
+    "    missed = 0\n",
246
+    "    for i, j in zip(box1, box2):\n",
247
+    "        if i == j:\n",
248
+    "            common += i\n",
249
+    "        else:\n",
250
+    "            missed += 1\n",
251
+    "            if missed > 1:\n",
252
+    "                break\n",
253
+    "    \n",
254
+    "    if missed == 1:\n",
255
+    "        print(common)\n",
256
+    "        break"
256 257
    ]
257 258
   }
258 259
  ],

+ 1
- 2
docker/spliterator.py View File

@@ -13,8 +13,7 @@ with open('2018.ipynb', 'r') as f:
13 13
             else:
14 14
                 if day not in days:
15 15
                     days[day] = common
16
-                days[day] += ''.join(cell['source'][:-1])
17
-                days[day] += f"print({cell['source'][-1]})\n\n"
16
+                days[day] += ''.join(cell['source']) + '\n\n'
18 17
 
19 18
     for day, code in days.items():
20 19
         with open(f"{day:02}.py", "w") as py: