Browse Source

Much nicer and faster day 12.

master
Chris Smith 6 years ago
parent
commit
62ca9f00fc
1 changed files with 11 additions and 20 deletions
  1. 11
    20
      12.py

+ 11
- 20
12.py View File

@@ -1,22 +1,13 @@
1
+import functools
2
+
1 3
 with open('data/12.txt', 'r') as file:
4
+    sets = []
2 5
     pipes = list(map(lambda l: l.strip().replace(' <-> ', ', ').split(', '), file.readlines()))
3
-    groups = [set('0')]
4
-    while len(pipes):
5
-        for gi, group in enumerate(groups):
6
-            last_size = 0
7
-            while len(group) != last_size:
8
-                last_size = len(group)
9
-                remove = []
10
-                for pipeset in pipes:
11
-                    if any(program in group for program in pipeset):
12
-                        group = group.union(pipeset)
13
-                        groups[gi] = group
14
-                        remove.append(pipeset)
15
-                for pipeset in remove:
16
-                    pipes.remove(pipeset)
17
-        if len(pipes):
18
-            pipeset = pipes[0]
19
-            groups.append(set(pipeset))
20
-            pipes.remove(pipeset)
21
-    print(f'Part one: {len(groups[0])}')
22
-    print(f'Part two: {len(groups)}')
6
+    for pipe in pipes:
7
+        overlapping = [s for s in sets if any(program in s for program in pipe)]
8
+        for overlap in overlapping:
9
+            sets.remove(overlap)
10
+        sets.append(functools.reduce(set.union, overlapping, set(pipe)))
11
+
12
+    print(f'Part one: {len(next(s for s in sets if "0" in s))}')
13
+    print(f'Part two: {len(sets)}')

Loading…
Cancel
Save