Solve day5
This commit is contained in:
22
day5/part2.py
Normal file
22
day5/part2.py
Normal file
@@ -0,0 +1,22 @@
|
||||
FILE = "input.txt"
|
||||
|
||||
def main():
|
||||
ranges = []
|
||||
for line in open(FILE):
|
||||
if line == "\n": break
|
||||
ranges.append(line.strip().split("-"))
|
||||
ranges = sorted([
|
||||
(int(start), int(stop)+1) for (start, stop) in ranges
|
||||
], key=lambda x: x[0])
|
||||
condensed = [ranges[0]]
|
||||
for r in ranges:
|
||||
end = condensed[-1][1]
|
||||
if r[0] <= end < r[1]:
|
||||
condensed[-1] = (condensed[-1][0], r[1])
|
||||
elif end < r[0]:
|
||||
condensed.append(r)
|
||||
return sum(len(range(r[0], r[1])) for r in condensed)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
print(main())
|
||||
Reference in New Issue
Block a user