-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday5.py
78 lines (70 loc) · 3.11 KB
/
day5.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/env python3
import numpy as np
import re
f = open("ressources/day5.txt", "r") #Open File
lines = f.readlines() #Separate in lines
#Q1
seeds = lines[0].split()[1::]
seeds = list(map(int, seeds))
isMapped = [0]*len(seeds)
for line in lines[2::]:
words = line.split()
if len(words)==0:
isMapped = [0]*len(seeds)
if len(words)==3:
numbers = list(map(int, words))
for i, seed in enumerate(seeds):
if isMapped[i] == 0:
end = numbers[0]
start = numbers[1]
rangeOf = numbers[2]
if (seed >= start) and (seed < start + rangeOf):
seeds[i] = seed + end-start
isMapped[i] = 1
print("Minimum location is: {}".format(np.min(seeds)))
#Q2
seeds = lines[0].split()[1::]
seeds = list(map(int, seeds))
seedsStart = seeds[0::2]
seedsRange = seeds[1::2]
isMapped = [0]*len(seedsStart)
for line in lines[2::]:
words = line.split()
if len(words)==0:
isMapped = [0]*len(seedsStart)
if len(words)==3:
numbers = list(map(int, words))
for i, (seedStart, seedRange) in enumerate(zip(seedsStart, seedsRange)):
if isMapped[i] == 0:
end = numbers[0]
start = numbers[1]
rangeOf = numbers[2]
if (seedStart >= start) and (seedStart+seedRange < start + rangeOf): #Departure is fully included in arrival
seedsStart[i] = seedStart + end-start
seedsRange[i] = seedRange
isMapped[i] = 1
elif(seedStart < start) and (seedStart+seedRange > start+rangeOf): #Arrival is fully included in departure
seedsStart[i] = end
seedsRange[i] = rangeOf
isMapped[i] = 1
seedsStart.append(seedStart) #Append rest on the left
seedsRange.append(start-seedStart-1)
isMapped.append(0)
seedsStart.append(start+rangeOf) #Append rest on the right
seedsRange.append(seedStart+seedRange - (start+rangeOf)-1)
isMapped.append(0)
elif(seedStart >= start) and (seedStart < start + rangeOf): #Departure is partly included in arrival on the right
seedsStart[i] = seedStart + end-start
seedsRange[i] = start+rangeOf-seedStart
isMapped[i] = 1
seedsStart.append(start+rangeOf) #Append rest on the right
seedsRange.append(seedStart+seedRange - (start+rangeOf)-1)
isMapped.append(0)
elif(seedStart < start) and (seedStart+seedRange >= start): #Departure is partly included in arrival on the left
seedsStart[i] = end
seedsRange[i] = seedRange-(start-seedStart)
isMapped[i] = 1
seedsStart.append(seedStart) #Append rest on the left
seedsRange.append(start-seedStart-1)
isMapped.append(0)
print("Minimum range location is: {}".format(np.min(seedsStart)))