advent-of-code/2016-python2/output/day_20.py
Anders Englöf Ytterström 63da79b2a4 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.
2024-12-12 13:53:43 +01:00

31 lines
707 B
Python

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)