A Long-ish Write Up on Some Very Short Code

June 21, 2017

I first learned about code golf  the way I assume a lot of other people learned about it: I saw an interesting question in the “Hot Network Questions” section of Stack Overflow. I clicked on the link and found the code golf page.  Code golf is the idea of writing code to solve a problem with the fewest amount of characters or bytes as possible. If you are going to work on a code golf problem with a normal programing language, this usually involves using as few white spaces as possible and finding some neat tricks with built in functions. There are also some languages that were built specifically for code golfing. These languages can sometimes solve problems with only a couple characters when it might take a normal language more than 50 to solve. With a lab day coming up I thought it would be fun to try out some the code golf challenges.

// https://codegolf.stackexchange.com/questions/119278/lengthen-letter-runs

//My first attempt at solving the problem
x="foobaaarr"
x2=x.split("")
let l2=x2[0]
let o = x2.reduce((a,c)=>{a+=l2==c?c:l2+c;l2=c;return a},"")+l2
console.log(o);

//My golfed (shortened) code
x=>{x=x.split("");let l=x[0];return x.reduce((a,c)=>{a+=l==c?c:l+c;l=c;return a},"")+l}


When I first started out, I tried to answer some of the challenges using Javascript. I found a question I liked, and I wrote my code like I would for a normal Javascript program. I thought this would let me get a better grasp on my program and see possible places to shorten it. Once the program was written, I began on making my code as small as possible. This was harder than I thought it would be. The first step was pretty obvious and helpful. I got all my code on one line and removed any spaces that weren’t necessary. I also shortened all variable names down to 1 letter. After that it got trickier. The best thing I found was to look at my input and see if there was some built in function that I could call. For example, an array has a function for filling in a range with an object that you give it, which would be shorter than looping through the array most of the time.

Once I thought I had my final answer, I would look to see if there was a Javascript answer already there. If there was, it was most likely shorter than mine because they had thought of some built in function that I had never heard of or never used. This was interesting to me because it really shows how small of fraction of javascript I really use. I stick to my for loops and if else statements for most of the time and there are so many other functions out there that I can use that I have never read about before. Writing this now I think it would be a helpful experience to read through some of the built in functions javascript has at its disposal.

I also wanted to try my hand at working with one of the languages that was designed to solve golfing problems. I browsed different questions looking at the answers and found some pretty cool languages. I found a link to a question about writing hello world in the smallest way possible and it had some really cool answers. There is a language that is based off of colors, a language based of off cab driving instructions (including having to refuel for gas), and there is even a language that reads like a Shakespeare play.

I ended up deciding on Befunge-93, a 2D grid based system. You start in the upper right and move the the right. Each character will tell you what to do. Some will interact with the stack, while others will tell you what direction to move in. Check out the wikipedia article for examples and to have them explained. Even though Befunge-93 wasn’t made for code golfing, I like it for its simplicity. For example, taking in an input and adding one is simply ‘&1+.@’. I also had a lot of fun getting the code as small as possible. I approached problems the same way as javascript, write them out normally then condense them. Befunge programs were fun to condense because it usually meant getting them on one line to remove whitespace. This was a fun challenge that involved jumping commands with the # character. The following is code that checks to see if a number follows the pattern of 123, 234, 345 etc.

&::57*2+3*%34*-!77*1+0p34*`!77*2+0p98*34**`77*3+0p111...@

Overall I think I very learned a lot this lab day. I got a fresh take on javascript code and I got to learn a really cool language with Befunge. These code golf challenges gave me some problems to solve that I don’t get to in a normal working environment. I would recommend code golf to anyone who is looking for a programming challenge.