Browse Source

Performance improvements

master
Chris Smith 5 years ago
parent
commit
634431a323
3 changed files with 20 additions and 11 deletions
  1. 18
    7
      03.py
  2. 1
    3
      docker/Dockerfile
  3. 1
    1
      run.sh

+ 18
- 7
03.py View File

@@ -2,18 +2,29 @@ import re
2 2
 from collections import defaultdict
3 3
 
4 4
 with open('data/03.txt', 'r') as file:
5
-    cells = defaultdict(list)
5
+    cells = defaultdict(lambda: 0)
6 6
     claims = map(lambda l: map(int, re.findall(r'\d+', l)), file.readlines())
7 7
     ids = set()
8
+    overlaps = 0
8 9
 
9 10
     for (cid, x, y, width, height) in claims:
10
-        ids.add(cid)
11
+        collision = False
11 12
         for i in range(width):
12 13
             for j in range(height):
13
-                cells[(x + i, y + j)].append(cid)
14
+                previous = cells[(x + i, y + j)]
15
+                if previous == 0:
16
+                    cells[(x + i, y + j)] = cid
17
+                elif previous == -1:
18
+                    collision = True
19
+                else:
20
+                    collision = True
21
+                    overlaps += 1
22
+                    if previous in ids:
23
+                        ids.remove(previous)
24
+                    cells[(x + i, y + j)] = -1
14 25
 
15
-    overlaps = [c for c in cells.values() if len(c) > 1]
16
-    print(len(overlaps))
26
+        if not collision:
27
+            ids.add(cid)
17 28
 
18
-    dupes = set(cid for sublist in overlaps for cid in sublist)
19
-    print(list(ids - dupes)[0])
29
+    print(overlaps)
30
+    print(list(ids)[0])

+ 1
- 3
docker/Dockerfile View File

@@ -1,6 +1,4 @@
1
-FROM pypy:3
2
-
3
-RUN pip3 install numpy
1
+FROM pypy:3-slim
4 2
 
5 3
 ADD entrypoint.sh /entrypoint.sh
6 4
 RUN chmod +x /entrypoint.sh

+ 1
- 1
run.sh View File

@@ -1,6 +1,6 @@
1 1
 #!/bin/bash
2 2
 
3
-IMAGE=csmith/aoc-2018-01
3
+IMAGE=csmith/aoc-2018-02
4 4
 
5 5
 docker image inspect $IMAGE >/dev/null 2>&1
6 6
 if [ $? -ne 0 ]