AdventOfCode2023/Day 2/2.py

44 lines
1.5 KiB
Python

def parse_lists(lines: list[str]) -> dict[int, list[dict[str, int]]]:
game_list: dict[int, dict[str, int]] = dict()
for line in lines:
if not line == '':
game_id: int = (int)(line.split(':')[0].split(' ')[1])
dirty_rounds = line.split(': ')[1].split('; ')
rounds = list()
for dirty_round in dirty_rounds:
round_split = dirty_round.split(', ')
round_part: dict[str, int] = dict()
for split in round_split:
part_split = split.split(' ')
round_part[part_split[1].strip('\n')] = (int)(part_split[0])
rounds.append(round_part)
game_list[game_id] = rounds
return game_list
def get_game_possible_sum(game_list: dict[int, list[dict[str, int]]]) -> int:
possible_sum = 0
for game_id in game_list:
reds = list()
blues = list()
greens = list()
for round in game_list[game_id]:
if 'blue' in round:
blues.append(round['blue'])
if 'red' in round:
reds.append(round['red'])
if 'green' in round:
greens.append(round['green'])
possible_sum += max(reds) * max(blues) * max(greens)
return possible_sum
lines = open('./values', 'r').readlines()
game_list = parse_lists(lines)
print(f'Parsed {len(game_list)} games!')
print(f'Possible games sum: {get_game_possible_sum(game_list)}') # 2286