-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path5.py
More file actions
65 lines (47 loc) · 924 Bytes
/
Copy path5.py
File metadata and controls
65 lines (47 loc) · 924 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import math
import collections
from collections import Counter
import re
from tqdm import tqdm
TEST = """
3-5
10-14
16-20
12-18
1
5
8
11
17
32
"""
PROD = """wow
"""
x = TEST
x = PROD
x = x.strip()
# Data in differnt formats for quick access
text = x
# lines = x.split("\n")
# grid = [list(line) for line in lines]
result = 0
ranges, numbers = text.split("\n\n")
range_list = []
for line in ranges.split("\n"):
a, b = line.split("-")
range_list.append((int(a), int(b)))
range_list.sort()
merged = []
for a,b in range_list:
if not merged:
merged.append((a,b))
continue
if a > merged[-1][1]: # if the start of new is bigger than the end of the old
merged.append((a,b))
else: # extend the end to be larger of the two ends
old = merged[-1]
new = (old[0], max(old[1], b))
merged[-1]= new
for a,b in merged:
result += b - a + 1
print(result)