Compare commits
No commits in common. "5b2c2df628c44361c5098925f6fdee65a5e7ca87" and "281efda496698c0c6de9eb39cb1ee1bbc07ca859" have entirely different histories.
5b2c2df628
...
281efda496
|
|
@ -1,9 +1,7 @@
|
||||||
from io import open
|
from io import open
|
||||||
import re
|
import re
|
||||||
import numpy as np
|
import numpy as np
|
||||||
from itertools import product
|
|
||||||
|
|
||||||
#Hay algo mal y no sé qué es
|
|
||||||
# We open the txt document containing the games
|
# We open the txt document containing the games
|
||||||
input=open('input.txt','r')
|
input=open('input.txt','r')
|
||||||
|
|
||||||
|
|
@ -18,7 +16,7 @@ symbols=[]
|
||||||
matches=[]
|
matches=[]
|
||||||
list_text=[]
|
list_text=[]
|
||||||
|
|
||||||
#Eliminamos los números y los puntos para obtener los símbolos que hay
|
#Eliminamos los números y los puntos para obtener los dígitos diferentes/símbolos que hay
|
||||||
numbers=[]
|
numbers=[]
|
||||||
for i in range(0,10):
|
for i in range(0,10):
|
||||||
numbers.append(str(i))
|
numbers.append(str(i))
|
||||||
|
|
@ -37,44 +35,8 @@ for position,line in enumerate(text):
|
||||||
del(list_text[position][-1])
|
del(list_text[position][-1])
|
||||||
for symbol in symbols:
|
for symbol in symbols:
|
||||||
try:
|
try:
|
||||||
#print(symbol,position,text[position].index(symbol))
|
#print(symbol,position,texto[position].index(symbol))
|
||||||
find=position,list_text[position].index(symbol)
|
find=position,list_text[position].index(symbol)
|
||||||
matches.append(find)
|
matches.append(find)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
#Identificamos las posiciones alrededor del símbolo que tiene números y los guardamos en una variable que se llama position_numbers=[]
|
|
||||||
position_numbers=[]
|
|
||||||
for symbol in matches:
|
|
||||||
row_option=symbol[0]
|
|
||||||
column_option=symbol[1]
|
|
||||||
extra_positions = len(str(symbol[0]))
|
|
||||||
#iteramos las posiciones que están alrededor del símbolo mediante iteración
|
|
||||||
posible_rows=list(range(max(0,row_option-1),min(row_option+2,len(text)))) #+2 porque el range no incluye el último número, len(text)=número filas máxima
|
|
||||||
posible_columns=list(range(max(0,column_option-1),min(column_option+extra_positions+1,len(text))))
|
|
||||||
#Guardamos las posiciones de los números que rodean a los simbolos
|
|
||||||
combinations_numbers=list(product(posible_rows,posible_columns))
|
|
||||||
for number in combinations_numbers:
|
|
||||||
value=text[number[0]][number[1]]
|
|
||||||
if value.isdigit():
|
|
||||||
position_numbers.append((number[0],number[1]))
|
|
||||||
|
|
||||||
#Una vez tenemos los dígitos que rodean a los simbolos hay que ver si tienen números adyacentes a estos para agrupar los números
|
|
||||||
visit_numbers=[]
|
|
||||||
final_numbers=[]
|
|
||||||
for row,column in position_numbers:
|
|
||||||
first_col=column
|
|
||||||
last_col=column
|
|
||||||
if (row,column) not in visit_numbers:
|
|
||||||
while text[row][first_col].isdigit() and first_col >= 0:
|
|
||||||
visit_numbers.append((row, first_col))
|
|
||||||
first_col = first_col - 1
|
|
||||||
try:
|
|
||||||
while text[row][last_col].isdigit() and last_col < len(text):
|
|
||||||
visit_numbers.append((row, last_col))
|
|
||||||
last_col = last_col + 1
|
|
||||||
except:
|
|
||||||
pass
|
|
||||||
final_numbers.append(int(text[row][first_col+1:last_col]))
|
|
||||||
|
|
||||||
print(sum(final_numbers))
|
|
||||||
|
|
@ -1,62 +0,0 @@
|
||||||
from io import open
|
|
||||||
import re
|
|
||||||
import numpy as np
|
|
||||||
from itertools import product
|
|
||||||
|
|
||||||
# We open the txt document containing the games
|
|
||||||
input=open('input.txt','r')
|
|
||||||
|
|
||||||
# We read the text inside distinguishing by lines
|
|
||||||
lines = input.readlines()
|
|
||||||
|
|
||||||
# Close the txt file to be able to work on the text without having the file open.
|
|
||||||
input.close()
|
|
||||||
del(input)
|
|
||||||
|
|
||||||
for i in range(len(lines)):
|
|
||||||
lines[i] = lines[i].replace("\n", "")
|
|
||||||
|
|
||||||
list_symbols = []
|
|
||||||
cont_row = 0
|
|
||||||
for line in lines:
|
|
||||||
cont_col = 0
|
|
||||||
for letter in line:
|
|
||||||
if letter != '.' and not letter.isdigit():
|
|
||||||
list_symbols.append((letter, cont_row, cont_col))
|
|
||||||
cont_col += 1
|
|
||||||
cont_row += 1
|
|
||||||
|
|
||||||
pos_numbers = []
|
|
||||||
for option in list_symbols:
|
|
||||||
row_option = option[1]
|
|
||||||
col_option = option[2]
|
|
||||||
extra_positions = len(str(option[0]))
|
|
||||||
possible_rows = list(range(max(0, row_option-1), min(row_option+2, len(lines))))
|
|
||||||
possible_columns = list(range(max(0, col_option-1), min(col_option+extra_positions+1, len(lines[0]))))
|
|
||||||
combinations = list(product(possible_rows, possible_columns))
|
|
||||||
checked = False
|
|
||||||
for combination in combinations:
|
|
||||||
value = lines[combination[0]][combination[1]]
|
|
||||||
if value.isdigit():
|
|
||||||
pos_numbers.append((combination[0], combination[1]))
|
|
||||||
checked = True
|
|
||||||
|
|
||||||
visited_numbers = []
|
|
||||||
final_numbers = []
|
|
||||||
for row, col in pos_numbers:
|
|
||||||
first_col = col
|
|
||||||
last_col = col
|
|
||||||
if (row, col) not in visited_numbers:
|
|
||||||
while lines[row][first_col].isdigit() and first_col >= 0:
|
|
||||||
visited_numbers.append((row, first_col))
|
|
||||||
first_col = first_col - 1
|
|
||||||
try:
|
|
||||||
while lines[row][last_col].isdigit() and last_col < 140:
|
|
||||||
visited_numbers.append((row, last_col))
|
|
||||||
last_col = last_col + 1
|
|
||||||
# print(last_col)
|
|
||||||
except:
|
|
||||||
pass
|
|
||||||
final_numbers.append(int(lines[row][first_col+1:last_col]))
|
|
||||||
|
|
||||||
print(sum(final_numbers))
|
|
||||||
Loading…
Reference in New Issue