This post originated from an RSS feed registered with Ruby Buzz
by Jan Lelis.
Original Post: Ruby Brainfuck golf
Feed Title: rbJ*_*L.net
Feed URL: http://feeds.feedburner.com/rbJL
Feed Description: Hi, I am a fan of Ruby and like to explore it and the world around ;).
So I started this blog, where I am publishing code snippets, tutorials for beginners as well as general thoughts about Ruby, the web or programming in general.
Some days ago, I discovered a website – which is the most addicting one I know :). It’s codegolf.com. The goal is, to solve programming problems with as short code as possible.
As I said, it is addicting. You do not write better ruby code by golfing. But you can really improve the knowledge of some parts of the programming language. And it is fun :)
Brainfuck
After doing some challenges I tried the brainfuck challenge. Brainfuck is a Turing-complete esoteric programming language with only 8 letters, operating on a 30000 cells-array. This is the hello world program:
Does not managae to calculate all square numbers till 1000 within 4 seconds..
There are some possibilites to shorten the code.. but not, to get down to 107 Bytes (that is the size of the brainfuck interpreter by the Ruby golfing god flagitious)! I had recognized, that I needed another approach.
Playing code golf
To understand the following solution better, here are the specifications about the brainfuck interpreter for the challenge:
The Ruby version is 1.8.5
The input for the brainfuck program is directly appended, split by an exclamation mark
There is no cell overflow (255 + 1 = 256, not 0)
The result of the tests (see the golfcode challenge page) has to be calculated within 4 seconds
You don’t need 30000 cells, just enough to pass the tests
The input is well formed brainfuck (…no comments are in it)
Exiting with an error is fine, as long the output is right :)
The ansatz is:
eval
Every letter of the brainfuck programm is translated into ruby and then executed via eval. Then, some golfing tricks are applied. For example the global variable
$$
holds a (big) process number, which are enough cells. There are some more nice golfing tips at the codegolf-board (Thanks).
After lots of hours and hours (days!) of thinking, trying, depression, but also with senses of achievement, this is my result: