Part 2 was too hard for me, since I initially worked with range(s, e). It quickly became out of hand. A funnel method was instead used.
31 lines
598 B
Python
31 lines
598 B
Python
from output import ints
|
|
|
|
|
|
def solve(data):
|
|
p1 = None
|
|
p2 = 0
|
|
ranges, ids = data.split("\n\n")
|
|
ids = ints(ids)
|
|
R = [ints(line) for line in ranges.split()]
|
|
p1 = sum(any(s <= n <= e for s, e in R) for n in ids)
|
|
c = 0
|
|
for s, e in sorted(R):
|
|
s = max(c + 1, s)
|
|
if s <= e:
|
|
p2 += e - s + 1
|
|
c = max(e, c)
|
|
|
|
return p1, p2
|
|
|
|
|
|
if __name__ == "__main__":
|
|
with open("./input/05.txt", "r") as f:
|
|
inp = f.read().strip()
|
|
|
|
p1, p2 = solve(inp)
|
|
|
|
print(p1)
|
|
print(p2)
|
|
|
|
assert p1 == 509
|
|
assert p2 == 336790092076620
|