-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday2_2.py
More file actions
75 lines (57 loc) · 2.01 KB
/
day2_2.py
File metadata and controls
75 lines (57 loc) · 2.01 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import re
MAX_RED = 12
MAX_GREEN = 13
MAX_BLUE = 14
def obtener_id(linea):
match = re.search(r'Game (\d+):', linea)
if match:
return int(match.group(1))
return '0'
def extraer_muestras(muestras):
return muestras.split(";")
def dividir_muestra(muestra):
return muestra.split(',')
def obtenerPotencia(linea):
partes_linea = linea.strip().split( ":")
muestras = extraer_muestras(partes_linea[1])
linea_invalida = False
max_red = 0
max_blue = 0
max_green = 0
for muestra in muestras:
pares_texto = muestra.split(',')
for par in pares_texto:
par_separado = par.strip().split(" ")
# print(f"{int(par_separado[0])}")
num = int(par_separado[0])
text = par_separado[1]
if text == 'red':
if num > max_red:
max_red = num
if num > MAX_RED:
linea_invalida = True
if text == 'blue':
if num > max_blue:
max_blue = num
if num > MAX_BLUE:
linea_invalida = True
if text == 'green':
if num > max_green:
max_green = num
if num > MAX_GREEN:
linea_invalida = True
return int(max_red*max_blue*max_green)
def procesar_archivo(nombre_archivo):
try:
with open(nombre_archivo, 'r') as archivo:
suma_total = 0
for linea in archivo:
id = obtener_id(linea)
potencia = obtenerPotencia(linea)
print(f'Game: {id}, Potencia: {potencia}')
suma_total += potencia
print(f'Suma total de calibraciones: {suma_total}')
except FileNotFoundError:
print(f'Error: El archivo {nombre_archivo} no fue encontrado.')
# Reemplaza 'nombre_del_archivo.txt' con el nombre de tu archivo
procesar_archivo('datos.txt')