Skip to content

Instantly share code, notes, and snippets.

@tslater2006
Last active December 2, 2023 22:44
Show Gist options
  • Select an option

  • Save tslater2006/37e0641eec57eba045e56d468d22f94b to your computer and use it in GitHub Desktop.

Select an option

Save tslater2006/37e0641eec57eba045e56d468d22f94b to your computer and use it in GitHub Desktop.
OpenAI generated solution to AoC 2023 Day 2. Only change by me was to wrap the answers in a print() and gave a dummy file path
# Function to parse the game data from the input text
def parse_game_data(game_data):
games = {}
for line in game_data.strip().split('\n'):
if not line.startswith('Game'):
continue
game_id_str, *game_reveals_list = line.replace(':', ';').split(';')
game_id = int(game_id_str.split()[1])
game_reveals = []
for reveal in game_reveals_list:
reveal_counts = {'red': 0, 'green': 0, 'blue': 0}
cubes = reveal.strip().split(',')
for cube in cubes:
count, color = cube.strip().split()
reveal_counts[color] += int(count)
game_reveals.append(reveal_counts)
games[game_id] = game_reveals
return games
# Function to check if all reveals are possible with the given constraints
def check_possible_games(games, red_max, green_max, blue_max):
possible_game_ids = []
for game_id, reveals in games.items():
if all(reveal['red'] <= red_max and reveal['green'] <= green_max and reveal['blue'] <= blue_max
for reveal in reveals):
possible_game_ids.append(game_id)
return possible_game_ids
# Read the content from the file path
file_path = '/path/to/day/input.txt'
with open(file_path, 'r') as file:
game_entries = file.read()
# Parse the game data
games = parse_game_data(game_entries)
# Define the maximum number of each colored cube allowed in the bag
red_max = 12
green_max = 13
blue_max = 14
# Determine possible games
possible_games = check_possible_games(games, red_max, green_max, blue_max)
# Calculate the sum of the IDs of the possible games
sum_possible_games = sum(possible_games)
print("Part 1: ",sum_possible_games)
# Function to calculate the minimum required cubes for each game
def calculate_minimum_cubes(games):
min_cubes_by_game = {}
for game_id, reveals in games.items():
# Initialize with zero cubes of each color for each game
min_cubes = {'red': 0, 'green': 0, 'blue': 0}
# Maximize the revealed cubes of each color to find the minimum needed cubes
for reveal in reveals:
min_cubes['red'] = max(min_cubes['red'], reveal['red'])
min_cubes['green'] = max(min_cubes['green'], reveal['green'])
min_cubes['blue'] = max(min_cubes['blue'], reveal['blue'])
min_cubes_by_game[game_id] = min_cubes
return min_cubes_by_game
# Function to calculate the power of a set of cubes (product of red, green, and blue cubes)
def calculate_power(cubes):
return cubes['red'] * cubes['green'] * cubes['blue']
# Using the previously parsed game data, calculate the minimum number of cubes needed for each game
min_cubes_by_game = calculate_minimum_cubes(games)
# Calculate the power for each game and sum them up
total_power_sum = sum(calculate_power(cubes) for cubes in min_cubes_by_game.values())
print("Part 2: ", total_power_sum)
Me: <Pasted the entire text of part 1>
Bot: To solve this problem, we need to analyze each game and check whether the subsets of cubes shown to us in each retrieval from the bag could have come from a bag containing only 12 red cubes, 13 green cubes, and 14 blue cubes.
A game is considered possible if at no point does the Elf show more cubes of a color than the maximum number specified (12 red, 13 green, 14 blue).
Let me write a Python function that processes this text data and checks each game description to see whether the conditions are met. Afterwards, I'll sum up the IDs of the games that are possible. Please provide the recorded information for all games that you played, so I can perform the analysis.
Me: <attaches input to chat and sends>
Bot: <code interpreter>
Bot: It seems there was an issue with parsing the provided game data. I'm going to correct this and process the data again accurately. Let's try that once more with the correct input.
Bot: <code interpreter>
Bot: The sum of the IDs of the games that would have been possible with a bag containing only 12 red cubes, 13 green cubes, and 14 blue cubes is 12.
However, please note that the analysis here is based on an incomplete set of games since we used a placeholder for illustration purposes due to the length of the content.
To provide you with the accurate result, I need to process the actual content of the file that was uploaded. Let's read the complete file and then I will perform the analysis on the full dataset.
Bot: <code interpreter>
Bot: After processing the complete dataset, the sum of the IDs of the games that would have been possible with a bag containing only 12 red cubes, 13 green cubes, and 14 blue cubes is <correct answer to part 1>.
Me: <Pastes in Part 2 starting with "as you continue your walk..."
Bot: <code_interpreter>
Bot: The sum of the power of the minimum sets of cubes that must have been present for each game is <correct part 2 answer>.
This value represents the total product of the minimum required red, green, and blue cubes across all games played.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment