As I wrote in my previous post, I would like to test ChatGPT for advent of code challenges this year and this post is about my initial thoughts.
Alright, let's dive straight in and tackle day 4 with it.
I started by calibrating ChatGPT to be aware of the goal of this conversation.
Me:
I need help to answer a advent of code question. I will provide you with the question and its up to you to give back javascript code that can be used to solve the question. Make sure that your answer matches with the example provided in the question. Are you ready?
Please note that I will divide the question and send it in chunks to you. I will tell you when the complete question has been transferred.
ChatGPT:
Yes, I'm ready to help you with your Advent of Code question. Please go ahead and provide the first part of the question. Let me know when you have transferred the complete question, and I'll do my best to provide you with the JavaScript code to solve it.
I gradually provide the entire question, piece by piece. As I was feeding segments of the question, ChatGPT was progressively constructing the script. Take for instance:
Me:
The first time a marker appears is after the seventh character arrives. Once it does, the last four characters received are jpqm, which are all different. In this case, your subroutine should report the value 7, because the first start-of-packet marker is complete after 7 characters have been processed.
ChatGPT:
Apologies for the oversight. I have revised the code to accurately detect the first occurrence of a complete start-of-packet marker after processing a given number of characters. Here's the updated code:
function findMarkerLength(datastream) {
const markerLength = 4; // Length of the start-of-packet marker
for (let i = markerLength - 1; i < datastream.length; i++) {
const marker = new Set(datastream.slice(i - markerLength + 1, i + 1));
if (marker.size === markerLength) {
return i - markerLength + 1; // Return the position of the first four-character marker
}
}
return -1; // Return -1 if the marker is not found
}
Now, let's use the updated code to find the position of the first start-of-packet marker in the given datastream buffer:
const datastream = "mjqjpqmgbljsphdztnvjfqwrcgsmlb";
const markerPosition = findMarkerLength(datastream);
if (markerPosition !== -1) {
const numCharacters = markerPosition + markerLength;
console.log("Number of characters to the end of the marker:", numCharacters);
} else {
console.log("Marker not found.");
}
When executing this revised code, it will output the following: Number of characters to the end of the marker: 7
In this case, the first complete start-of-packet marker "jpqm" appears after processing the seventh character. Therefore, the correct result is 7, representing the number of characters from the beginning of the buffer to the end of the marker.
I apologize for any confusion caused by the previous responses. Please let me know if you have any further questions or need additional assistance!
When the transfer of question was completed. I finally asked it for the complete script.
Me:
Please provide me with the complete code
ChatGPT:
function findMarkerLength(datastream) {
const markerLength = 4; // Length of the start-of-packet marker
for (let i = markerLength - 1; i < datastream.length; i++) {
const marker = new Set(datastream.slice(i - markerLength + 1, i + 1));
if (marker.size === markerLength) {
return i - markerLength + 1; // Return the position of the first four-character marker
}
}
return -1; // Return -1 if the marker is not found
}
const examples = [
"bvwbjplbgvbhsrlpgdmjqwftvncz",
"nppdvjthqldpwncqszvftbrmjlhg",
"nznrnfrfntjfmvfwmzdfjlvtqnbhcprsg",
"zcfzfwzzqfrljwzlrfnpqdbhtmscgvjw"
];
for (const example of examples) {
const markerPosition = findMarkerLength(example);
if (markerPosition !== -1) {
const numCharacters = markerPosition + 4; // markerLength = 4
console.log(`Number of characters for "${example}":`, numCharacters);
} else {
console.log(`Marker not found for "${example}".`);
}
}
And when I provided the complete puzzle input it worked. Magic!
Being upfront, this process doesn't seem work for most questions. The main problem seem to lies in the parsing of examples inputs or interpreting the intent. But with the right amount of human guidance, it can greatly speed things up things, particularly in the case of boilerplate's.
I'm gonna write another post about how I use ChatGPT in my every day life to crank up my efficiency. Stay tuned!
Happy Coding!