Solve 2025 day 5 pt 1-2
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.
This commit is contained in:
parent
ac6b97590c
commit
2d3caced7f
1 changed files with 31 additions and 0 deletions
31
2025-python/output/day_05.py
Normal file
31
2025-python/output/day_05.py
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
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
|
||||
Loading…
Add table
Reference in a new issue