finished part 2 for day 2

This commit is contained in:
Jake Pullen
2023-12-02 16:41:46 +00:00
parent c2d362c6b3
commit 6d6778ff51
3 changed files with 139 additions and 27 deletions
+28 -22
View File
@@ -1,5 +1,5 @@
# with open('advent_of_code_2023\day_2\input.txt', 'r') as file:
# input = file.read()
with open(r'advent_of_code\2023\day_2\input.txt', 'r') as file:
input = file.read()
# print(input)
test_input = '''Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green
@@ -19,29 +19,25 @@ for cube in amount_of_cubes:
amount_of_cubes_dict[cube[1]] = cube[0]
print(f'control: {amount_of_cubes_dict}')
def check_round(round_dict):
if 'red' in round_dict and not round_dict['red']:
# check blue and green
if round_dict['blue'] > amount_of_cubes_dict['blue'] or round_dict['green'] > amount_of_cubes_dict['green']:
print('round', round_counter, 'is invalid')
return False
if 'green' in round_dict and not round_dict['green']:
# check blue and red
if round_dict['blue'] > amount_of_cubes_dict['blue'] or round_dict['red'] > amount_of_cubes_dict['red']:
print('round', round_counter, 'is invalid')
return False
if 'blue' in round_dict and not round_dict['blue']:
# check red and green
if round_dict['red'] > amount_of_cubes_dict['red'] or round_dict['green'] > amount_of_cubes_dict['green']:
print('round', round_counter, 'is invalid')
return False
def check_round(round_dict):
# Check if the keys exist in the dictionary, if not assign 0
red = round_dict.get('red', 0)
green = round_dict.get('green', 0)
blue = round_dict.get('blue', 0)
# Now compare with the amount_of_cubes_dict
if red > amount_of_cubes_dict['red'] or green > amount_of_cubes_dict['green'] or blue > amount_of_cubes_dict['blue']:
print('round', round_counter, 'is invalid')
return False
else:
print('round', round_counter, 'is valid')
return True
# break out each game to check if any of the games are valid
games = test_input.split('\n')
#games = test_input.split('\n')
games = input.split('\n')
game_counter = 0
score = 0
for game in games:
game_counter += 1
game = game.split(': ')[1]
@@ -56,5 +52,15 @@ for game in games:
cube = [int(cube[:2]), cube[2:]]
cube[1] = cube[1].replace(' ', '')
round_dict[cube[1]] = cube[0]
print(round_dict)
check_round(round_dict)
#print(round_dict)
if check_round(round_dict) == False:
break
else:
continue
else:
score += game_counter
print('game', game_counter, 'is valid')
continue
else:
print('score:', score)