2023-12-02 18:13:36 +00:00
|
|
|
def main(filename, bag, result = 0):
|
|
|
|
with open(filename, 'r') as f:
|
|
|
|
for line in f.readlines():
|
|
|
|
game = line.split(": ")
|
|
|
|
p = game[1].replace("\n","").split("; ")
|
|
|
|
i = int(game[0].split(" ")[1])
|
|
|
|
result += i
|
|
|
|
#print(p)
|
|
|
|
# par pack
|
|
|
|
for c in p:
|
|
|
|
nb = [0, 0, 0]
|
|
|
|
#print(c)
|
|
|
|
# par jeton
|
|
|
|
for j in c.split(", "):
|
|
|
|
k = j.split(" ")
|
|
|
|
#print(j)
|
|
|
|
if k[1] == 'red':
|
|
|
|
nb[0] += int(k[0])
|
|
|
|
elif k[1] == 'green':
|
|
|
|
nb[1] += int(k[0])
|
|
|
|
elif k[1] == 'blue':
|
|
|
|
nb[2] += int(k[0])
|
|
|
|
else:
|
|
|
|
print("error")
|
|
|
|
if nb[0] > bag[0] or nb[1] > bag[1] or nb[2] > bag[2]:
|
|
|
|
result -= i
|
|
|
|
break
|
2023-12-02 14:58:43 +00:00
|
|
|
|
2023-12-02 18:13:36 +00:00
|
|
|
return result
|
2023-12-02 14:58:43 +00:00
|
|
|
|
2023-12-02 18:13:36 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
#red / green / blue
|
|
|
|
print(main("input.txt", [12, 13, 14]))
|