The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
Ruby Brainfuck golf

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Jan Lelis

Posts: 136
Nickname: rbjl
Registered: Aug, 2009

Jan Lelis is an IT student from Dresden/Germany
Ruby Brainfuck golf Posted: Sep 21, 2009 10:05 AM
Reply to this message Reply

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.
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Jan Lelis
Latest Posts From rbJ*_*L.net

Advertisement

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:

>+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[<++++>-]
<.>+++++++++++[<++++++++>-]<-.--------.+++.------.--------.[-]>++++++++[<++++>-
]<+.[-]++++++++++.
\nh3. Building the interpreter

At first, I built a basic brainfuck interpreter. It’s not that fast, but works fine ;).

f,e=$<.read.split'!'
e&&e=e.split('')
b=[a=i=n=0]*s=30000
while i<f.size
  case f[i]
  when ?>
    a+=1
  when ?<
    a-=1
  when ?+
    b[a]+=1
  when ?-
    b[a]-=1
  when ?.
    putc b[a]
  when ?,
    e==[]?i=s:b[a]=e.shift[0]
  when ?[
    n=d=1 if b[a]==0
  when ?]
  n,d=1,-1 if b[a]!=0
  end
  
  while n>0
    i+=d
    f[i]==?[&&n+=d
    f[i]==?]&&n-=d
  end
  i+=1
end
  • About 300 Byte without the whitespaces.
  • 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:

Ruby Brainfuck interpreter in 158 Bytes

gets'!'
%w(! [ ] + - > < . ,).zip(%w{9 ( )while(0!=b[a]) b[a]+=1 b[a]-=1 a+=1 a-=1 putc(b[a]) (b[a]=getc)<11&&1/0}).map{|a,b|gsub a,b+$/}
eval"b=[a=0]*$$
"+$_

It’s so crazy… I don’t know (yet) how it’s possible to shrink it another 51 Bytes °_°

CC-BY-SA (DE)

Read: Ruby Brainfuck golf

Topic: RPCFN: Shift Subtitle (#1) Previous Topic   Next Topic Topic: Learn ActionScript Object Orientation - new lesson

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use