52 lines
921 B
Python
52 lines
921 B
Python
import os
|
|
|
|
if (os.path.isfile("./input")):
|
|
input = open("./input", "r").read()
|
|
else:
|
|
input = "3-5\n10-14\n16-20\n12-18\n\n"
|
|
|
|
#input = input[:-1]
|
|
|
|
fresh = []
|
|
c = 0
|
|
|
|
for line in input.split('\n'):
|
|
if (line == ""):
|
|
break
|
|
|
|
s = int(line.split('-')[0])
|
|
e = int(line.split('-')[1])
|
|
|
|
for f in fresh:
|
|
if (f[0] <= s <= f[1]):
|
|
s = f[1] + 1;
|
|
if (f[0] <= e <= f[1]):
|
|
e = f[0] - 1;
|
|
|
|
if (s > e):
|
|
continue
|
|
fresh.append([s, e])
|
|
fresh.sort()
|
|
c += (e - s) + 1
|
|
|
|
print(c)
|
|
|
|
ranges = []
|
|
for line in input.split("\n"):
|
|
if line == "":
|
|
break
|
|
ranges.append([int(line.split('-')[0]), int(line.split('-')[1])])
|
|
|
|
ranges.sort()
|
|
|
|
merged = []
|
|
for s, e in ranges:
|
|
if not merged or s > merged[-1][1] + 1:
|
|
merged.append([s, e])
|
|
else:
|
|
merged[-1][1] = max(merged[-1][1], e)
|
|
|
|
c = sum(e - s + 1 for s, e in merged)
|
|
|
|
print(c)
|