-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday4_2.py
More file actions
52 lines (38 loc) · 1.47 KB
/
day4_2.py
File metadata and controls
52 lines (38 loc) · 1.47 KB
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
import re
def obtenerId(texto):
patron = r'Card\s*(\d+):'
match = re.search(patron, texto)
if match:
numero = match.group(1)
return int(numero)
else:
return None
def contarTotal(lineas):
num_dict = {}
for linea in lineas:
id = obtenerId(linea)
num_dict[id] = 1
for linea in lineas:
id = obtenerId(linea)
linea_div = linea.split(':')
lineas_numeros = linea_div[1].split('|')
lineas_numeros[0] = lineas_numeros[0].strip()
lineas_numeros[1] = lineas_numeros[1].strip()
numeros_ganadores = [int(numero) for numero in lineas_numeros[0].split() if numero]
numeros_actuales = [int(numero) for numero in lineas_numeros[1].split() if numero]
matching_numbers = sum(1 for num in numeros_actuales if num in numeros_ganadores)
if id is not None and id in num_dict:
for i in range(id + 1, id + 1 + matching_numbers):
if i in num_dict:
num_dict[i] += num_dict[id]
print(f'{num_dict[id]}')
total_scratchcards = sum(num_dict.values())
return total_scratchcards
def procesar_archivo(nombre_archivo):
try:
with open(nombre_archivo, 'r') as archivo:
lineas = archivo.readlines()
print(f'Total de lineas: {contarTotal(lineas)}')
except FileNotFoundError:
print(f'Error: El archivo {nombre_archivo} no fue encontrado.')
procesar_archivo('datosCartas.txt')