-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcodeAdvent16.py
251 lines (142 loc) · 4.7 KB
/
codeAdvent16.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
'''
Created on 1 September 2021
@author: Antonio Martínez Fernández
'''
import re
lista_condiciones = []
my_ticket = []
lista_tickets = [] # all valid tickets
def procesLine(linea):
lista_aux = re.findall(r"\d+-\d+ or \d+-\d+", linea)
for aux in lista_aux:
lista_condiciones.append(aux)
def check_requirements(i):
for aux in lista_condiciones:
aux = aux.strip()
aux = aux.split("or")
aux1 = aux[0]
aux2 = aux[1]
aux1 = aux1.split("-")
a = int(aux1[0])
b = int(aux1[1])
aux2 = aux2.split("-")
c = int(aux2[0])
d = int(aux2[1])
if (i >= a and i <= b) or (i >= c and i <= d):
return True
return False
def check_possible(values_per_field):
global lista_condiciones
check = 0
actual_cond = 0 # numero de campo/condicion
ans = [] # options for each column
for aux in lista_condiciones: # para cada condicion - para cada columna
check = 0
aux = aux.strip()
aux = aux.split("or")
aux1 = aux[0]
aux2 = aux[1]
aux1 = aux1.split("-")
a = int(aux1[0])
b = int(aux1[1])
aux2 = aux2.split("-")
c = int(aux2[0])
d = int(aux2[1])
for i in values_per_field:
if not ((i >= a and i <= b) or (i >= c and i <= d)):
check = 1
if check == 0:
ans.append(actual_cond)
actual_cond += 1
return ans
def main_function():
ans = 0
archivo = open("input14.txt", "r")
linea = archivo.readline()
while not linea in '\n':
procesLine(linea)
linea = archivo.readline()
for i in range(4):
archivo.readline()
while True:
contador = 0
cont_aux = 0
linea = archivo.readline()
linea = linea.replace("\n", "")
if not linea:
break
aux = linea.split(",")
long = len(aux)
for i in aux:
if check_requirements(int(i)):
contador += 1
else:
cont_aux += int(i)
if long != contador:
ans += cont_aux
return ans
def main_function_v2():
archivo = open("input14.txt")
linea = archivo.readline()
while not linea in '\n':
procesLine(linea)
linea = archivo.readline()
archivo.readline() # linea your ticket
linea = archivo.readline()
linea = linea.split(",")
for i in linea:
my_ticket.append(i)
archivo.readline() # linea nearby tickets
archivo.readline()
while True:
aux = 0
linea = archivo.readline()
if linea:
linea = linea.replace("\n", "")
linea = linea.split(",")
if not linea:
break
# guardamos los tickets válidos
for i in linea:
if not check_requirements(int(i)):
aux = 1
if aux == 0:
lista_aux = []
for a in linea:
lista_aux.append(a)
lista_tickets.append(lista_aux)
value_and_pos = {} # diccionario de que campo es cada cual
for i in range(0, len(lista_tickets[0]), 1): # posicion actual del campo
values_per_field = []
for ticket in lista_tickets:
values_per_field.append(int(ticket[i]))
value_and_pos[i] = check_possible(values_per_field)
# value_and_pos[primera columa]=posicion de la condicion
while True:
parada = 0
for i in value_and_pos:
if len(value_and_pos.get(i)) > 1:
for j in value_and_pos:
if len(value_and_pos.get(j)) == 1 and value_and_pos.get(j)[0] in value_and_pos.get(i) and j != i:
value_and_pos[i].remove(value_and_pos.get(j)[0])
for i in value_and_pos:
if len(value_and_pos.get(i)) != 1:
parada = 1
if parada == 0:
break
answer = []
# posicion ticket - posicion campo
for i in value_and_pos:
if value_and_pos.get(i)[0] >= 0 and value_and_pos.get(i)[0] <= 5:
answer.append(i)
sol = 1
for i in range(0, len(my_ticket), 1):
if i in answer:
sol *= int(my_ticket[i])
return sol
if __name__ == '__main__':
print(">>> Parte 1 = ", main_function())
lista_condiciones = []
my_ticket = []
lista_tickets = []
print(">>> Parte 2 = ", main_function_v2())