Compare commits
2 Commits
a3b11b628c
...
5bdba85192
| Author | SHA1 | Date | |
|---|---|---|---|
| 5bdba85192 | |||
| ff3a474c0b |
1
02-gift-shop/.gitignore
vendored
Normal file
1
02-gift-shop/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
*.in
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user