From 63da79b2a490e0fc8c8d043954ea529657c2413c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anders=20Engl=C3=B6f=20Ytterstr=C3=B6m?= Date: Thu, 5 Dec 2024 23:17:58 +0100 Subject: [PATCH] Solve 2016:20 p1-2 "Firewall Rules" The code at one time used cached responses for in range bools, but it seems that does not improve performance. Some IP addresses are allowed multiple times, so min() and set() are used to find the distinct values. --- 2016-python2/output/day_20.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 2016-python2/output/day_20.py diff --git a/2016-python2/output/day_20.py b/2016-python2/output/day_20.py new file mode 100644 index 0000000..cb73b1e --- /dev/null +++ b/2016-python2/output/day_20.py @@ -0,0 +1,31 @@ +from output import ints + + +def solve(data): + lowest, highest = min(ints(data)), max(ints(data)) + data = [ints(line) for line in data.split()] + p1 = float("inf") + p2 = set() + for a, b in data: + X = a - 1 + Y = b + 1 + if X >= lowest: + if not any(x <= X <= y for x, y in data): + p1 = min(p1, X) + p2.add(X) + if Y <= highest: + if not any(x <= Y <= y for x, y in data): + p1 = min(p1, Y) + p2.add(Y) + p2 = len(p2) + return p1, p2 + + +if __name__ == "__main__": + with open("./input/20.txt", "r") as f: + inp = f.read().strip() + + p1, p2 = solve(inp) + + print(p1) + print(p2)