-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaoc23_day5_1.py
executable file
·57 lines (43 loc) · 1.07 KB
/
aoc23_day5_1.py
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
#!/usr/bin/env python
import bisect
def read_map():
res = []
# header
input()
while True:
try:
line = input()
if line.strip() == '':
break
a, b, c = [int(item) for item in line.split()]
res.append((b, a, c))
except EOFError:
break
res.sort()
return res
def convert(item, map_entry):
init_start, end_start, length = map_entry
offset = item - init_start
if offset >= 0 and offset < length:
return end_start + offset
else:
return item
def follow_seed(seed, maps):
item = seed
for mapx in maps:
idx = bisect.bisect_right(mapx, (item, 0, 0)) - 1
item = convert(item, mapx[idx])
return item
def main():
line = input()
seeds = [int(item) for item in line.split(':')[-1].split()]
# empty line
input()
maps = []
for _ in range(7):
mapx = read_map()
maps.append(mapx)
res = min((follow_seed(seed, maps) for seed in seeds))
print(res)
if __name__ == '__main__':
main()