The death of code golf with ChatGPT
Thanks to clcsimon for the image

The death of code golf with ChatGPT

Have you ever heard of code golf? It's a coder's game where the goal is to solve a problem with a minimum number of characters, using all the tricks available in your programming language. This is completely useless in a professional setting since the code becomes very hard to read, and many rules are violated. However, it demonstrates what your favorite language allows. There is a big community devoted to code golfing on Stack Overflow.

As a big fan of CodinGame, I have participated in numerous code golf challenges. It's one of the website's challenges, and I'm currently ranked in the top 100 out of thousands of players.

Most of my solutions are optimized to the point where I can't see or think of new ways to reduce the number of characters. However, with the rise of AI, I wondered if it was possible to get some help and improve my rank. To my surprise, AI such as ChatGPT has been a significant aid.

The original code

I am gonna focus on one of the code golf challenge named The Descent. The goal is to create an infinite loop where we have to choose the highest number between 8 numbers. Pretty simple, right? I came up with this code.

for(;;) {
    let result = 0
    for (let mountainHigher = parseInt(readline()), i = 1; i < 8; i++) {
        const mountainH = parseInt(readline());
        if (mountainH > mountainHigher) {
            result = i;
            mountainHigher = mountainH;
        }
    }
    console.log(result);
}        

The code is simple. The first line create the infinite loop and the nested block create the logic for searching the highest number represented by mountainHigher in my code. I keep the index and print it once I went through all the 8 numbers.

This solution is easy to understand but perform poorly on code golf since I am not really paying too much attention to the number of characters. Here, the solution has been made with 298 characters.

The ugly reduced solution

From the previous code, I started to work on it for reducing the number of characters while keeping the logic. I started to change the name of some variable, removed the space and the function ot use at my advantage the JS automatic type conversion. I also create a variable for readline for only using once this long name for reading the numbers. At this point, the code is still readable with a solution of 231 characters.

for(r=readline;;) {
    for(v=0,mountainHigher = +r(),i=1;i<8;i++){
        const mountainH = +r();
        if (mountainH > mountainHigher) {
            v = i;
            mountainHigher = mountainH;
        }
    }
    print(v);
}        

I went a bit further. I removed the space, changed even further the name of the variable, I moved the print inside the first for in order to remove the bracket. I also use the multiple assignment to remove one comma and I changed a bit the logic. This reduce the number of characters even further at 125 characters

for(;;print(v))
    for(b=i=0;i<8;i++){
        h=readline()
        if(h>b){
            v=i
            b=h
        }
    }        

Finally, I removed all the space to make it at short as possible making our final solution at 64 characters.

for(;;print(v))for(b=i=0;i<8;i++){h=readline()
if(h>b){v=i;b=h}}        

ChatGPT to the rescue

I was pretty satisfied with my solution. I did not see how to make it shorter. But I knew if I wanted to do so, I have to make a solution a single line. With the rise of ChatGPT, I was wondering if it was possible to ask him to make a solution shorter while keeping the logic. ChatGPT comes up with this answer.

Aucun texte alternatif pour cette image

It was not correct however it shows me an optimization, I did not thought about bringing our new solution to 62 characters:

for(;;print(v))for(b=i=0;i<8;i++){h=readline()
h>b&&(v=i,b=h)})        

By curiosity, I asked ChatGPT a second time.

Aucun texte alternatif pour cette image

It showed me that you can make it one line by adding a comma between two statements allowing us to remove the brackets. The final solution ended up being only 60 characters.

for(;;print(v))for(b=i=0;i<8;i++)h=readline(),h>b&&(v=i,b=h)        

Conclusion

Even without trying too hard, ChatGPT showed me solutions that I did not thought. If I had to find that by myself, I would have easily stayed in front of my code for multiple minutes or hours. ChatGPT managed to show me new way of reducing my code further and actually gain a big amount of time. However, it also scares me because code golfing is not the normal way of coding and it has been capable of doing so with some accuracy. In a very near future, this challenge will died as AI will be capable of finding the perfect solution instantly.

要查看或添加评论,请登录

Kevin Justal的更多文章

社区洞察

其他会员也浏览了