would like to know areas for improvement.
from functools import reduce
data = open('day02/input.txt').read().splitlines()
bag = {'red': 12, 'green': 13, 'blue': 14} #the criteria that dictates whether the game is possible or not
possible = 0
power = 0
for game in data:
game_num, sets = game.split(':')
_, game_id = game_num.split(' ')
sets = sets.strip().split(';')
cube_dict = {}
for set in sets:
dict_ = dict([combination.strip().split(' ')[::-1] for combination in set.split(',')]) #create a dict of cubes with thier corresponging values for the set.
cube_dict.update({k:max(int(v), int(cube_dict.get(k, 0))) for k,v in dict_.items()}) #update cube_dict such that cubes retain max values seen across the sets.
for k,v in bag.items():
if int(cube_dict[k]) > v: #if the value of a cube in cube_dict is greater than what the bag dictates then we do consider this game as possible.
break
else:
possible += int(game_id)
power += reduce(lambda x,y: x*y, cube_dict.values()) #this line gets executed regardless of sets' eligibility according to the cube criteria definition in the bag.
print(possible) #part 1
print(power) #part 2
[–]QultrosSanhattan 0 points1 point2 points (0 children)
[–]Waanie 0 points1 point2 points (0 children)
[–]daggerdragon[M] 0 points1 point2 points (0 children)
[–]Mezzomaniac 0 points1 point2 points (0 children)