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)
+87 -3
View File
@@ -1,6 +1,90 @@
import os
from functools import reduce
import operator
with open('advent_of_code_2023\day_2\input.txt', 'r') as file:
with open(r'advent_of_code\2023\day_2\input.txt', 'r') as file:
input = file.read()
print(input)
# print(input)
test_input = '''Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green
Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue
Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red
Game 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red
Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green'''
amount_of_cubes = '12 red, 13 green, 14 blue'
amount_of_cubes = amount_of_cubes.split(', ')
# create a dictionary to store the amount of cubes
amount_of_cubes_dict = {}
for cube in amount_of_cubes:
# split the string into the amount and the colour
# first 2 characters are the amount
cube = [int(cube[:2]), cube[3:]]
amount_of_cubes_dict[cube[1]] = cube[0]
print(f'control: {amount_of_cubes_dict}')
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
def highest_count_per_colour(game):
colour_counts = {}
groups = game.split(';')
for group in groups:
pairs = group.split(',')
for pair in pairs:
count, color = pair.strip().split(' ')
count = int(count)
if color not in colour_counts or count > colour_counts[color]:
colour_counts[color] = count
print(f'colour counts: {colour_counts}')
product_of_counts = reduce(operator.mul, colour_counts.values(), 1)
print(f'product of colour counts: {product_of_counts}')
return product_of_counts
#return 0
# break out each game to check if any of the games are valid
#games = test_input.split('\n')
games = input.split('\n')
game_counter = 0
score = 0
sum_of_powers = 0
for game in games:
game_counter += 1
game = game.split(': ')[1]
print('game', game_counter)
print(game)
round_counter = 0
rounds = game.split('; ')
powers = highest_count_per_colour(game)
sum_of_powers += powers
for round in rounds:
round_counter += 1
round = round.split(', ')
round_dict = {}
for cube in round:
cube = [int(cube[:2]), cube[2:]]
cube[1] = cube[1].replace(' ', '')
round_dict[cube[1]] = cube[0]
#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)
print('sum of powers:', sum_of_powers)
+24 -2
View File
@@ -1,6 +1,6 @@
# Day 2 Puzzle Text.
--- Day 2: Cube Conundrum ---
--- Day 2: Cube Conundrum ---
You're launched high into the atmosphere! The apex of your trajectory just barely reaches the surface of a large island floating in the sky. You gently land in a fluffy pile of leaves. It's quite cold, but you don't see much snow. An Elf runs over to greet you.
The Elf explains that you've arrived at Snow Island and apologizes for the lack of snow. He'll be happy to explain the situation, but it's a bit of a walk, so you have some time. They don't get many visitors up here; would you like to play a game in the meantime?
@@ -25,4 +25,26 @@ The Elf would first like to know which games would have been possible if the bag
In the example above, games 1, 2, and 5 would have been possible if the bag had been loaded with that configuration. However, game 3 would have been impossible because at one point the Elf showed you 20 red cubes at once; similarly, game 4 would also have been impossible because the Elf showed you 15 blue cubes at once. If you add up the IDs of the games that would have been possible, you get 8.
Determine which games would have been possible if the bag had been loaded with only 12 red cubes, 13 green cubes, and 14 blue cubes. What is the sum of the IDs of those games?
Determine which games would have been possible if the bag had been loaded with only 12 red cubes, 13 green cubes, and 14 blue cubes. What is the sum of the IDs of those games?
--- Part Two ---
The Elf says they've stopped producing snow because they aren't getting any water! He isn't sure why the water stopped; however, he can show you how to get to the water source to check it out for yourself. It's just up ahead!
As you continue your walk, the Elf poses a second question: in each game you played, what is the fewest number of cubes of each color that could have been in the bag to make the game possible?
Again consider the example games from earlier:
```
Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green
Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue
Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red
Game 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red
Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green
```
In game 1, the game could have been played with as few as 4 red, 2 green, and 6 blue cubes. If any color had even one fewer cube, the game would have been impossible.
Game 2 could have been played with a minimum of 1 red, 3 green, and 4 blue cubes.
Game 3 must have been played with at least 20 red, 13 green, and 6 blue cubes.
Game 4 required at least 14 red, 3 green, and 15 blue cubes.
Game 5 needed no fewer than 6 red, 3 green, and 2 blue cubes in the bag.
The power of a set of cubes is equal to the numbers of red, green, and blue cubes multiplied together. The power of the minimum set of cubes in game 1 is 48. In games 2-5 it was 12, 1560, 630, and 36, respectively. Adding up these five powers produces the sum 2286.
For each game, find the minimum set of cubes that must have been present. What is the sum of the power of these sets?