These past few days have been filled with news of a new kid on the block, namely ChatGPT. The results have been intriguing to say the least, and I was eventually convinced to give it a try myself. Surely it’s not all they make it out to be?

With that in mind, I set out to solve today’s Advent of Code puzzle (Day 4). I couldn’t wait to test it out, so I just pasted the instructions to ChatGPT1 as given to me. The results were very interesting:

import re

def fully_contains(range1, range2):
  a, b = range1
  c, d = range2
  return a <= c and d <= b

# Read the input from the file
with open('input.txt') as input_file:
  input_str = input_file.read()

# Split the input on the comma character
pairs_str = input_str.split(',')

# Use a regular expression to extract the two integers from each pair
pairs = []
for pair_str in pairs_str:
  match = re.match(r'(\d+)-(\d+)', pair_str)
  if match:
    pairs.append((int(match.group(1)), int(match.group(2))))

# Count the number of pairs where one range fully contains the other
num_fully_contained = 0
for i in range(len(pairs)):
  for j in range(i+1, len(pairs)):
    if fully_contains(pairs[i], pairs[j]):
      num_fully_contained += 1

# Print the result
print(num_fully_contained)

The code was not doing what we wanted, instead, it was checking each pair against every pair that appears after that in the input. Suffice it to say, this is not what we wanted. Still, very good for an AI.

I decided the prompt from Advent of Code was superfluous, so I had to make it brief and clear. Here is the new prompt I tried:

We are given a list of range pairs, each pair on a separate line, in a file called pairs.txt. For example, we have the following list of range pairs:

2-4,6-8
2-3,4-5
5-7,7-9
2-8,3-7
6-6,4-6
2-6,4-8

We need to write a program that for each pair of ranges, it determines whether one of the ranges contains the other. The final result we need is the number of pairs where one range contains the other.

So for the following pair of ranges:

2-3,1-5

The range 1-5 contains the range 2-3, because the range 1-5 consists of numbers 1, 2, 3, 4, 5, whereas the range 2-3 consists of numbers 2, 3.

I tried to be as clear as possible, without inundating the AI with tales of elves and whatnot. I hit the “send” button, waiting for ChatGPT to do its magic. I saw the “typing” indicator. After a few moments, it replied to me:

To solve this problem, we can iterate over the pairs of ranges in the input file, and for each pair, we can check whether one range contains the other.

Okay, makes sense so far.

First, we need to parse the input ranges, which are given as strings in the form “x-y”. To do this, we can use the split method to split the string at the “-” character, and then convert the resulting strings to integers using the int function. This will give us the starting and ending numbers of the range.

This shows that the AI has a clear understanding of what the input looks like, and also that it has a clear strategy of how to parse it.

Next, we need to check whether one range contains the other. To do this, we can compare the starting and ending numbers of the two ranges. If the starting number of the first range is less than or equal to the starting number of the second range, and the ending number of the first range is greater than or equal to the ending number of the second range, then the first range contains the second range.

Very well. We will look at the code, but at this point I was wondering whether it would only check if the first range contains the second one, or will it also check the other way around.

We can then keep track of the number of pairs of ranges where one range contains the other, and print this number as the final result.

So far so good. I was ecstatic at this description, but was the code any good?

Here is an implementation of this solution in Python:

(drum roll please…)

# Open the input file
with open("pairs.txt", "r") as f:
    # Initialize a counter for the number of pairs where one range contains the other
    count = 0

    # Iterate over the lines in the input file
    for line in f:
        # Split the line into the two ranges
        ranges = line.strip().split(",")

        # Parse the first range
        first_range = ranges[0].split("-")
        first_start = int(first_range[0])
        first_end = int(first_range[1])

        # Parse the second range
        second_range = ranges[1].split("-")
        second_start = int(second_range[0])
        second_end = int(second_range[1])

        # Check if the first range contains the second range
        if first_start <= second_start and first_end >= second_end:
            count += 1

        # Check if the second range contains the first range
        elif second_start <= first_start and second_end >= first_end:
            count += 1

# Print the final result
print(count)

Surely enough, as I expected, running this code with the given input yielded the desired result. Though not all is sunshine and rainbows. Right after the code snippet, which was correct, there was this wrong statement about the sample input:

The final result is the number of pairs of ranges where one range contains the other. For the input given in the problem statement, this number is 4.

Regardless, I got one star! But will it get me the second star?

At this point I had no doubt, as the change required was minimal. The great thing about ChatGPT is that it literally acts as a chat, so it has state and you can basically repeatedly mold its output until you get the desired one. So I got prompting, and replied with the following:

Great. Slight change of requirements from the manager. We now want the number of pairs of ranges, where the ranges overlap with each other. For example, for the following pair of ranges:

2-3,4-5

There is no overlap. However, for this pair of ranges:

14-16,16-18

There is an overlap. Update the code to match the new requirement.

After a few seconds but what felt like hours due to the anticipation, ChatGPT started replying. Its response was almost identical to the above, with some exceptions.

To solve this problem, we can iterate over the pairs of ranges in the input file, and for each pair, we can check whether the ranges overlap.

The first paragraph it outputted showed that it understood what the “manager” now wants of us :). Then it spurted out the parsing paragraph which was identical to the previous one.

Next, we need to check whether the ranges overlap. To do this, we can compare the starting and ending numbers of the two ranges. If the starting number of the first range is less than or equal to the ending number of the second range, and the ending number of the first range is greater than or equal to the starting number of the second range, then the ranges overlap.

Notice how it now describes how to check whether the ranges overlap, which seems to be accurate.

We can then keep track of the number of pairs of ranges where the ranges overlap, and print this number as the final result.

Here is an implementation of this solution in Python:

Would the code be a working solution? All signs pointed to “yes” by now.

# Open the input file
with open("pairs.txt", "r") as f:
    # Initialize a counter for the number of pairs where the ranges overlap
    count = 0

    # Iterate over the lines in the input file
    for line in f:
        # Split the line into the two ranges
        ranges = line.strip().split(",")

        # Parse the first range
        first_range = ranges[0].split("-")
        first_start = int(first_range[0])
        first_end = int(first_range[1])

        # Parse the second range
        second_range = ranges[1].split("-")
        second_start = int(second_range[0])
        second_end = int(second_range[1])

        # Check if the ranges overlap
        if first_start <= second_end and first_end >= second_start:
            count += 1

# Print the final result
print(count)

Unsurprisingly now, I got the second star. Of course, there was that attempt to give an answer for the sample input, which was, again, wrong:

The final result is the number of pairs of ranges where the ranges overlap. For the input given in the problem statement, this number is 3.

That doesn’t worry me though, as I wouldn’t expect a language model to be able to execute code. Still, it gets points for trying.

I’m not sure what I think about this. I think the biggest achievement is creating an AI that has such an advanced understanding of natural language. Eventually it would become good at writing code, too. At the moment, giving appropriate prompts requires having some understanding of the problem, so the AI is not truly independent.

That said, I can foresee a time when the AI becomes so good that it will be able to replace many professions, though I imagine some oversight will always be required.


  1. Funny thing about ChatGPT, I keep writing it as ChatGPL by mistake, which is kind of ironic given the drama about GitHub’s CoPilot and its (assumed?) use of GPL licensed code. ↩︎