# Advent of Code [With AI]: Day 2

Day 2 brings a new AoC challenge that I found quite a bit easier than day 1. The requirements are much more clear, so I suspect ChatGPT+ will be able to do both parts fairly easily. Let’s give it a go!

## Part 1

I decide to jump right into prompting with nothing but text taken directly from the page –

``````For example, the record of a few games might look like this:

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, three sets of cubes are revealed from the bag (and then put back again). The first set is 3 blue cubes and 4 red cubes; the second set is 1 red cube, 2 green cubes, and 6 blue cubes; the third set is only 2 green cubes.

The Elf would first like to know which games would have been possible if the bag contained only 12 red cubes, 13 green cubes, and 14 blue cubes?

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?
``````

After loading the text file of 100 inputs and working through a few different iterations on its own, it output the correct answer –

The analysis of the games from the file `day2.txt` indicates that the games with IDs 4, 5, 7, 8, 9, 10, 12, 14, 17, 19, 20, 21, 22, 23, 25, 28, 29, 30, 31, 32, 33, 34, 38, 40, 43, 46, 48, 49, 50, 54, 55, 56, 58, 61, 62, 63, 65, 67, 69, 71, 72, 74, 77, 78, 80, 83, 84, 87, 88, 89, 92, 93, 95, and 97 would have been possible if the bag had been loaded with only 12 red cubes, 13 green cubes, and 14 blue cubes. The sum of the IDs of these games is 2617.

and the code it used to get there –

``````# Correcting the parsing function to handle newline characters and re-running the analysis.

def parse_game_data(line):
"""Parse a line of game data to extract the game ID and the counts of each color in each set."""
# Removing any trailing newline characters
line = line.strip()

parts = line.split(': ')
game_id = int(parts[0].split(' ')[1])
sets = parts[1].split('; ')

cube_counts = []
for s in sets:
counts = s.split(', ')
cube_count = {'red': 0, 'green': 0, 'blue': 0}
for count in counts:
num, color = count.split(' ')
cube_count[color] = int(num)
cube_counts.append(cube_count)

return game_id, cube_counts

# Re-running the analysis
possible_games = []

with open(file_path, 'r') as file:
for line in file:
game_id, cube_counts = parse_game_data(line)
if is_game_possible(cube_counts, max_cubes):
possible_games.append(game_id)

# Calculate the sum of the IDs of the possible games
sum_possible_games = sum(possible_games)
sum_possible_games, possible_games
``````

I like the approach it took quite a bit and it managed to parse the strings just fine without any third-party libraries or regexs. More impressive was it took no real prompting to get the correct result.

## Part 2

Part 2 of this day’s challenge was much more clear than day 1. It was so clear I decided to just paste in the unaltered text once again and let ChatGPT have a pass at it –

``````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?``````

It responds –

The analysis of the games from the file `day2.txt` has provided the minimum number of cubes of each color that must have been in the bag for each game, along with the power of these sets. The sum of the powers of the minimum sets for all the games is 59,795.

And it’s right! Here’s the code it used –

``````# To find the minimum set of cubes that must have been present in each game and the sum of their powers,
# I'll analyze the data again. This time, I'll calculate the maximum number of each color of cubes shown
# in each game and then compute the power of these sets.

def find_minimum_cubes(file_path):
"""Find the minimum number of cubes of each color that must have been in the bag for each game."""
min_cubes_per_game = {}
powers_per_game = {}

with open(file_path, 'r') as file:
for line in file:
game_id, cube_counts = parse_game_data(line)

# Finding the maximum number of cubes shown for each color in the game
max_cubes_in_game = {'red': 0, 'green': 0, 'blue': 0}
for set_count in cube_counts:
for color, count in set_count.items():
max_cubes_in_game[color] = max(max_cubes_in_game[color], count)

min_cubes_per_game[game_id] = max_cubes_in_game
# Calculating the power of the set
power = max_cubes_in_game['red'] * max_cubes_in_game['green'] * max_cubes_in_game['blue']
powers_per_game[game_id] = power

return min_cubes_per_game, powers_per_game

# Calculating the minimum cubes and their powers for each game
min_cubes_per_game, powers_per_game = find_minimum_cubes(file_path)

# Sum of the power of the minimum sets
sum_power = sum(powers_per_game.values())
sum_power, min_cubes_per_game, powers_per_game
``````

There wasn’t too much to modify from the original function and no tricky edge cases.

## Final Thoughts

First day in the books of a flawless ChatGPT execution. Anyone with ChatGPT+ access and the ability to copy + paste could have gotten these answers no problem. 10/10 for both parts!