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.
This commit is contained in:
parent
39e09dd36e
commit
63da79b2a4
1 changed files with 31 additions and 0 deletions
31
2016-python2/output/day_20.py
Normal file
31
2016-python2/output/day_20.py
Normal file
|
|
@ -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)
|
||||
Loading…
Add table
Reference in a new issue