My Binary Golf Grand Prix 4 (BGGP4) Winning Entries

· bggp4, challenge, python, bash
  • The Binary Golf Grand Prix (BGGP) is a yearly code golf challenge
  • I won the python and shell categories of BGGP4 by cheekily interpreting the rules 👑

The Binary Golf Grand Prix (BGGP) is an annual competition that began in 2020, challenging participants to create the smallest possible binary files that perform specific tasks. Organized by @netspooky, the event builds on the concept of code golf, focusing on the creation of minimal executable binaries across various formats and architectures.

Participation in BGGP has grown steadily, with numerous entries each year showcasing innovative techniques and deep technical knowledge. The event has become a highlight in the niche community of binary golf enthusiasts, blending creativity with technical prowess. In 2023, BGGP4 opened up entries to non-binary formats which allowed many more people to participate.

The Binary Golf Grand Prix 4 (BGGP4) had specific rules that challenged participants to create the smallest executable files that could replicate themselves with an additional few constraints:

I decided to try my hand at the python and shell categories.

Python (42 bytes)

My python entry relied on a few common tricks such as loading all objects in the shutil module using the * symbol (fun fact: the space after import is optional). This gives us access to the copy() function, which only takes up 4 bytes. We then use the builtin __file__ variable as the first argument and the string '4' as the name of the destination file.

from shutil import*
copy(__file__,'4');4/0

Finally we get to the cheeky ace up my sleeve in order to get python to print 4 to stdout. The rules just stated 4 needed to be printed, it didn’t state that other output couldn’t accompany it. The shortest way I found of producing this sort of output is to try dividing 4 by 0, which will cause a ZeroDivisionError exception and terminate the script. I got confirmation from the organizers of the competition that this was within the spirit of the rules.

Bash (10 bytes)

Armed with a technical workaround for one of the rules, I tried the shell category. Most of the other competitors were using cp to move the script and echo to print out the number 4. I found that if you give cp the verbose flag, it will happily tell you the names of the files it is operating on - which prints out 4.

cp -v $0 4

Reviewing the other entries I noticed an efficiency which would have saved me a single byte. @caioluders submitted an 11 byte entry using * in place of $0 which selects all files in the current directory (see globbing). Had they also used the verbose flag, they would have won!

BGGP4 was a fun way to spend a few hours - big props to @netspooky and the rest of the team!