I used ChatGPT instead of Stack Overflow today

/ Reading time: 9 minute/s

There's been no shortage of people harping about the amazing things you can do with OpenAI's ChatGPT. I admit to losing exorbitant amounts of sleep over the weekend because I was trying to "break" it --- I kept asking it practical but somewhat obscure technical questions to see what it can say.

Here's an example:

ChatGPT explaining how to run a function in response to a change in a GPIO pin state in a Raspberry Pi using Rust
ChatGPT seems to know how to use Rust to read state from a Raspberry Pi's GPIO pins

This is amazing. I personally don't know how to do that myself, but even if this response from ChatGPT has inaccuracies:

  • it's pointed me to the rppal crate which looks to be completely within context of what we're trying to do (crate documentation here)
  • it's specified that the example it's given is for version 0.12.0, so I can work towards grokking this in comparison to the latest version 0.14.1 at the time of this writing
  • it's shown how to get a handle on a specific pin, and even configured it to (seemingly) behave as an input GPIO pin, which is exactly what we want,
  • and shows I can poll a pin with a specified interval duration

This is more than enough information to get me going if I was really doing something like this now. I don't expect it to be 100% accurate and up-to-date, but it's more than enough to point me in the right direction of where to focus studying more about this topic.

Using it to work through today's Advent of Code problem

So I thought, why not try to use ChatGPT as a learning assistant? I could use it instead of something like Stack Overflow, and instead of sifting through pages and pages of search results of varying quality and mismatched contexts, ChatGPT should be able to give me responses that are more direct to the point, within context, and (hopefully) much more up-to-date.

I've been using Advent of Code this year to learn how to code in Ruby. While I do have the advantage of generally knowing what I have to do to solve problems, I currently don't know exactly how those are achieved in Ruby. So instead of asking ChatGPT right out to solve a problem for me, I'll instead ask it how something can be done in Ruby as I go along.

For reference, here is today's Advent of Code problem (2022 day 5).

Splitting arrays

The input data for today comes in two parts: (1) an initial crate configuration, and (2) a sequential list of instructions for moving crates across the system.

My idea was to read off the entire input file per line into an array, and split that into the two constituents on the empty line.

ChatGPT on how to split an array

That's implemented here.

Splitting a string in groups of N characters

Here is the sample crate arrangement input:

    [D]
[N] [C]
[Z] [M] [P]
 1   2   3

Because of the nature of the formatting, I can take each line of that input, collect the characters in groups of 4, and just drop everything but the 2nd character (which is the crate ID itself).

ChatGPT on how to collect characters into fixed size groups

That's implemented here.

Parsing instructions using regex

Here is the sample instructions input:

move 1 from 2 to 1
move 3 from 1 to 3
move 2 from 2 to 1
move 1 from 1 to 2

I wanted to use regular expressions to capture the three values in each instruction: (1) how many crates to move, (2) from which column, (3) to which column.

ChatGPT on using regex capture groups

Here I realized that I wanted to use named capture groups instead of just plain ones.

ChatGPT on using regex named capture groups

This is implemented here.

But then I wondered if it's possible to destructure the result of that into its constituent variables (instead of doing captures["count"], etc.).

ChatGPT on how to destructure a hash into specific variables

This is implemented here.

Concatenating arrays

I realized I didn't know how to actually append elements of one array into another array short of doing something like:

array_1 = (1..5).to_a
array_2 = (6..10).to_a

array_2.map { |item| array_1 << item }

So I thought to ask anyway:

ChatGPT on how to concatenate 2 arrays

And sure enough, there was an easier way. This is implemented here.

How did that feel?

Honestly, really well. Most of the time when I'd be searching for how something is done in a specific language / framework / platform, it's a huge bulk of sifting through various search results, evaluating blog posts, or adapting a working solution for a slightly different problem than I was going through. When I was using ChatGPT, it felt like I had the rough equivalent of a mentor / paired programmer that was working on the same thing beside me. The responses were on point most of the time, and I am very appreciative of the time that saves for me.

When I was still a Solutions Architect for AWS, one of the things we've really wanted to build was a theoretical system we called an "SA-in-a-box". It was going to be an interface where you could ask it some of the most common questions SAs get asked by customers all the time, and it would spit out valid answers, ideally within context.

I tried that with ChatGPT today too:

ChatGPT on how to optimize Amazon EKS cluster costs

I wouldn't just follow any of those suggestions blindly, but heck, I'd be damned if they weren't a good start to work off of.