AdventOfCode2023/Day 2/1.py

47 lines
1.7 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])
print(f'Found game: {game_id}')
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]] = (int)(part_split[0])
rounds.append(round_part)
game_list[game_id] = rounds
return game_list
def check_game_possibilities(max_blue: int, max_red: int, max_green: int, game_list: dict[int, list[dict[str, int]]]) -> int:
possible_sum = 0
for game_id in game_list:
is_possible = True
for round in game_list[game_id]:
if 'blue' in round and round['blue'] > max_blue:
is_possible = False
break
if 'red' in round and round['red'] > max_red:
is_possible = False
break
if 'green' in round and round['green'] > max_green:
is_possible = False
break
if is_possible:
possible_sum += game_id
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: {check_game_possibilities(14, 12, 13, game_list)}') # 8