Browse Source

Day 20

master
Chris Smith 7 years ago
parent
commit
39cd83bb12
2 changed files with 1110 additions and 0 deletions
  1. 35
    0
      20.py
  2. 1075
    0
      data/20.txt

+ 35
- 0
20.py View File

@@ -0,0 +1,35 @@
1
+#!/usr/bin/python3
2
+
3
+"""Solution for day 20 of Advent of Code 2016.
4
+
5
+This solution first compresses the blacklist by merging overlapping sections, using functools.reduce and the merge
6
+function below. For example, [(0, 100), (50, 150)] becomes (0, 150). The blacklist is then inverted to become a
7
+whitelist, which gives both answers pretty easily.
8
+"""
9
+
10
+import functools
11
+
12
+def merge(x, y):
13
+    if y[0] <= x[-1][1] + 1:
14
+        # This entry overlaps with the last one, combine them
15
+        x[-1] = (x[-1][0], max(y[1], x[-1][1]))
16
+    else:
17
+        # New, non-overlapping entry, just append it to our list
18
+        x.append(y)
19
+    return x
20
+
21
+with open('data/20.txt', 'r') as file:
22
+    ranges = sorted(list(map(lambda x: tuple(map(int, x.strip().split('-'))), file.readlines())))
23
+    blacklist = functools.reduce(merge, ranges[1:], [ranges[0]])
24
+
25
+    whitelist = []
26
+    last = 0
27
+    for pair in blacklist:
28
+        if pair[0] > last:
29
+            whitelist.append((last, pair[0] - 1))
30
+        last = pair[1] + 1
31
+    if last < 4294967295:
32
+        whitelist.append((last, 4294967295))
33
+
34
+    print("Part 1: %s" % whitelist[0][0])
35
+    print("Part 2: %s" % sum(map(lambda p: 1 + p[1] - p[0], whitelist)))

+ 1075
- 0
data/20.txt
File diff suppressed because it is too large
View File


Loading…
Cancel
Save