-
Notifications
You must be signed in to change notification settings - Fork 0
/
codeAdvent_13.py
178 lines (108 loc) · 3.57 KB
/
codeAdvent_13.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
'''
Created on 10 August 2021
@author: Antonio Martínez Fernández
'''
# PART 1
timestamp=0
number=0
smallest=999999999999
# PART 2
actual_time=0
actual_bus=0
start_time=0
x=["x"]
def busFinder():
global timestamp
global smallest
global number
archivo = open("input12.txt")
linea = archivo.readline()
timestamp=int(linea)
linea=archivo.readline()
linea=linea.split(",")
for bus in linea:
if bus!="x":
aux=timestamp // int(bus)
if(aux*int(bus)<timestamp):
mod=((aux+1)*int(bus))-timestamp
else:
mod=aux-timestamp
if mod<smallest:
number=int(bus)
smallest=mod
def mainFunction():
busFinder()
print("bus número ",number," , tiempo de espera ",smallest," min","\n")
def checkSequence_old(linea): # ! deprecated , demasiado lenta
global actual_bus
global actual_time
global start_time
i=0 # caracter actual que toca
while True:
#print("BUS ",actual_bus)
#print("TIME ",actual_time)
if(actual_bus not in x):
actual_bus=int(actual_bus)
if (actual_time % actual_bus == 0 and i+1==len(linea)):
print("timestamp where the sequence starts ",start_time)
break
elif(actual_time % actual_bus == 0 and i<len(linea)):
i+=1
actual_bus=linea[i]
if(start_time==0):
start_time=actual_time
else:
actual_bus=linea[0]
i=0
start_time=0
else:
i+=1
actual_bus=linea[i]
actual_time+=1
def mainFunction():
busFinder()
print("bus número ",number," , tiempo de espera ",smallest," min","\n")
print("answer = ",number*smallest)
def checkSequence(linea): # * objetivo , buscar a partir de los multiplos del primero
global actual_bus
global actual_time
global start_time
i=0 # caracter actual que toca
contador=1
actual_time=int(linea[0])*1 # en este caso empieza en el segundo 17
while True:
#print("BUS ",actual_bus)
#print("TIME ",actual_time)
if(actual_bus not in x):
actual_bus=int(actual_bus)
if (actual_time % actual_bus == 0 and i+1==len(linea)):
print("timestamp where the sequence starts ",start_time)
break
elif(actual_time % actual_bus == 0 and i<len(linea)):
i+=1
actual_bus=linea[i]
if(start_time==0):
start_time=actual_time
actual_time+=1
else:
actual_bus=linea[0]
i=0
start_time=0
contador+=1
actual_time=int(linea[0])*contador
else:
i+=1
actual_bus=linea[i]
actual_time+=1
def mainFunction_v2():
global actual_bus
archivo = open("input12.txt")
#archivo = open("prueba.txt")
archivo.readline()
linea=archivo.readline()
linea=linea.split(",")
actual_bus=linea[0]
checkSequence(linea)
if __name__ == '__main__':
mainFunction()
mainFunction_v2()