aoc 2023 02

This commit is contained in:
Julian Brammer 2023-12-02 18:20:59 +01:00
parent 64237b431a
commit 5183ae11b8

View File

@ -14,7 +14,7 @@ for line in range(len(file)):
blue_pos = set.find(" b")
positions = [red_pos, green_pos, blue_pos]
vals = [0, 0, 0]
for pos in range(len(positions)):
if positions[pos] != -1:
vals[pos] = int(set[positions[pos]-2:positions[pos]])
@ -24,4 +24,35 @@ for line in range(len(file)):
if possible:
sum += line+1
print(sum)
print("Part 1: " + str(sum))
#! Part 2
file = open("input.txt").readlines()
sum = 0
for line in range(len(file)):
game = file[line].split(";")
game_sum = 0
max_vals = [0, 0, 0]
for set in game:
red_pos = set.find(" r")
green_pos = set.find(" g")
blue_pos = set.find(" b")
positions = [red_pos, green_pos, blue_pos]
vals = [0, 0, 0]
for pos in range(len(positions)):
if positions[pos] != -1:
vals[pos] = int(set[positions[pos]-2:positions[pos]])
for i in range(len(max_vals)):
if vals[i] > max_vals[i]:
max_vals[i] = vals[i]
game_sum += max_vals[0] * max_vals[1] * max_vals[2]
sum += game_sum
print("Part 2: " + str(sum))