1
+ from collections import defaultdict , deque , Counter
2
+ # d = deque()
3
+ # d.append(5)
4
+ # x = d.popleft()
5
+ import re
6
+ # m = re.match(r"(\w+) (\w+)", "Isaac Newton, physicist")
7
+ # # or re.search
8
+ # >>> m.group(0) # The entire match
9
+ # 'Isaac Newton'
10
+ # >>> m.group(1) # The first parenthesized subgroup.
11
+ # 'Isaac'
12
+ # >>> m.group(2) # The second parenthesized subgroup.
13
+ # 'Newton'
14
+ # >>> m.group(1, 2) # Multiple arguments give us a tuple.
15
+ # ('Isaac', 'Newton')
16
+ from heapq import heappush , heappop
17
+ # >>> heap = []
18
+ # >>> data = [1, 3, 5, 7, 9, 2, 4, 6, 8, 0]
19
+ # >>> for item in data:
20
+ # ... heappush(heap, item)
21
+ # heap[0] is the smallest item
22
+ import string
23
+ # string.ascii_lowercase == 'abcde...'
24
+ # string.ascii_uppercase == 'ABCDE...'
25
+ from functools import lru_cache
26
+ # @lru_cache(maxsize=None)
27
+
28
+ import sys
29
+
30
+ sys .setrecursionlimit (100000 )
31
+
32
+ def get_ints (s ):
33
+ return list (map (int , re .findall (r"-?\d+" , s ))) # copied from mcpower from mserrano on betaveros' recommendation
34
+ dirs = [(0 ,1 ), (1 ,0 ), (0 ,- 1 ), (- 1 ,0 )]
35
+ directions = 'RDLU'
36
+ octs = [(0 ,1 ),(1 ,1 ),(1 ,0 ),(1 ,- 1 ),(0 ,- 1 ),(- 1 ,- 1 ),(- 1 ,0 ),(- 1 ,1 )]
37
+ def is_grid_valid (n ,m , r ,c ,):
38
+ return (0 <= r < n ) and (0 <= c < m )
39
+ def sign_of (x ):
40
+ if x == 0 :
41
+ return 0
42
+ return x / abs (x )
43
+
44
+ if False :
45
+ ans = 0
46
+ seeds = get_ints (input ())
47
+ next_seeds = seeds [:]
48
+ inps = []
49
+ maps = []
50
+
51
+ while True :
52
+ try :
53
+ i = input ()
54
+ if i .strip () == '' :
55
+ seeds = next_seeds
56
+ next_seeds = seeds [:]
57
+ continue
58
+
59
+ if i .split ()[- 1 ] == 'map:' :
60
+ continue
61
+ d , s , l = get_ints (i )
62
+ for j , seed in enumerate (seeds ):
63
+ if s <= seed < s + l :
64
+ next_seeds [j ] = d + seed - s
65
+ except EOFError :
66
+ break
67
+
68
+ print (min (seeds ))
69
+ else :
70
+ pass
71
+ ans = 0
72
+ seeds = get_ints (input ())
73
+ ranges = []
74
+ for i in range (0 , len (seeds ), 2 ):
75
+ ranges .append ((seeds [i ], seeds [i ] + seeds [i + 1 ]- 1 ))
76
+ next_ranges = []
77
+ while True :
78
+ try :
79
+ i = input ()
80
+ print (ranges , next_ranges )
81
+ print (i )
82
+ if i .strip () == '' :
83
+ ranges += next_ranges
84
+ next_ranges = []
85
+ continue
86
+ if i .split ()[- 1 ] == 'map:' :
87
+ continue
88
+ d , s , l = get_ints (i )
89
+ temp_ranges = []
90
+ for j , (left , right ) in enumerate (ranges ):
91
+ if right < s or left >= s + l :
92
+ temp_ranges .append ((left , right ))
93
+ continue
94
+ source_start = max (left , s )
95
+ source_end = min (right , s + l - 1 )
96
+ next_ranges .append ((d + source_start - s , d + source_end - s ))
97
+ if source_start > left :
98
+ temp_ranges .append ((left , source_start - 1 ))
99
+ if source_end < right :
100
+ temp_ranges .append ((source_end + 1 , right ))
101
+ ranges = temp_ranges
102
+ except EOFError :
103
+ break
104
+
105
+
106
+ print (min (ranges ))
0 commit comments