Compare commits

...

2 Commits

Author SHA1 Message Date
5bdba85192 Consolidate ranges before handling them separately. 2025-12-05 18:21:39 +01:00
ff3a474c0b Add gitignore. 2025-12-05 17:21:25 +01:00
2 changed files with 21 additions and 2 deletions

1
02-gift-shop/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
*.in

View File

@@ -1,7 +1,7 @@
#!/usr/bin/env python3
import sys
from typing import Optional
from typing import Optional, Iterator
def magnitude(x : int) -> int:
assert x >= 0
@@ -61,6 +61,24 @@ def sum_invalid_ids(lo : int, hi : int) -> int:
return total
def consolidate(rngs : list[tuple[int, int]]) -> Iterator[tuple[int, int]]:
rngs = sorted(rngs)
cur = None
for lo, hi in rngs:
match cur:
case None:
cur = lo, hi
case cur_lo, cur_hi:
if lo <= cur_hi:
cur = cur_lo, hi
else:
yield cur
cur = lo, hi
assert cur
yield cur
if __name__ == '__main__':
ranges = [
(int(lo_s), int(hi_s))
@@ -71,7 +89,7 @@ if __name__ == '__main__':
]
total = 0
for lo, hi in ranges:
for lo, hi in consolidate(ranges):
total += sum_invalid_ids(lo, hi)
print(total)