tiny turing machine in C

I while ago I had a little contest with a friend to write a C/C++ Deterministic Turing Machine (DTM) in as little bytes of code as possible. We drew up a simple specification and set to work! Here’s mine (uncompressed):

#include <stdio.h>
#define w while
 
int i,j,k,h=1024;
char s='S',t[2049],d[]="S0a0>S1a0>a0a0>a1a0>a-H--";
 
int main(int c, char**v)
{
    w(i<h*2){t[i]='-';++i;}t[i]='\n';
    w(v[1][j]){t[j+h]=v[1][j];++j;};
    w(1){
        if(s=='H')break;i=0;
        w(d[i]){
            if (s==d[i]&&t[h]==d[i+1]){
                s=d[i+2];t[h]=d[i+3];
                h-=d[i+4]=='<';
                h+=d[i+4]=='>';
                break;
            }
            i+=5;
        }
    }
    printf("%c:",s);
    w(k<2049){if(t[k]!='-')putchar(t[k]);++k;};
    return 0;
}

The delta function is encoded in a char array. The DTM checks every fifth index i for a matching state and i+1 for a matching character on the tape. When it finds a match it changes to state i+2, writes i+3 and moves i+4. This way I can encode the delta function in as little space as possible. I used a fair amount of while loops and it worked out cheaper to add a #define. One neat trick I used was adding/subtracting the result of a boolean operation to change the h value.

h-=d[i+4]=='<';

This will subtract 1 if the head should move left, otherwise it will subtract 0 (do nothing). This removed the need for an if statement and got the code down by a couple of bytes.

Overall I am pretty happy. A limitation would be that the tape has a finite number of cells, but we agreed to this in our specification. I plan to write a Non-deterministic Turing Machine (NTM) next time I have a slow-day. This time I will use a linked list so the tape length is limited only by the size of the systems RAM.

Posted in Maths, Programming | Tagged , , , , | Leave a comment

a big list of tlds

I needed a big list of Top Level Domains (TLDs) for a small project I’ve been working on. It’s a pretty easy job with vim, some regex and a copy of this – but I’ve included it here for anyone that might be Googling for it.

You can get the full list here, but I’ve also included a handy selection of arrays.

The complete list was build with this vim command

:%s/^\.\([a-z]*\)\(.*\)$/.\1/

and one line of manual pruning.

Posted in Programming | Leave a comment

tmux with irssi – ncurses redraw bug

I recently noticed that in large terminals (over 40 rows) irssi won’t draw properly with tmux. Apparently this is a bug with tmux. It’s easy to fix, just add the following line to your ~/.tmux.conf and the problem should go away!

# set correct term
set -g default-terminal screen-256color
Posted in Linux | Tagged , , , | 2 Comments

Google Talk chatback badge – auto open chat

Just recently I needed to make a Google Talk chatback session start automatically, that a website could offer assistance after x amount of idletime and start the chat upon request. Far cleaner than asking them to click a link, I wanted the session to initiate once a user click “Yes, start live chat!” (or something to that effect).

The problem, was the Google Talk chatback badge uses an iFrame. I wanted to use jQuery to click the “open” link, but because of cross-domain scripting security within browsers this wasn’t possible.

My solution was quite simple. First of all I realised that I could easily grab the source to my chatback badge.

$ wget "http://www.google.com/talk/service/badge/Show?tk=...&amp;w=200&amp;h=60" -O talkBadge.html

Then I modified the talkBadge.html file to fix the broken resources. I did this with vim, since I already had it open, but you can do it with any tool using a simple search and replace.

:%s/"\/talk\//"http:\/\/www.google.com\/talk\//g

This replaces “/talk/ with “http://www.google.com/talk/. Finally I modified line 56 to give the a tag id=”talkLink”.

Now it was as easy as including the iFrame to my page as usual, but with the new talkBadge.html.

<iframe id="talkBadge" src="talkBadge.html" frameborder="0" allowtransparency="true" width="200" height="60"></iframe>

And then you can open the chatbox with the following line of Javascript (using jQuery).

$('#talkBox').contents().find('#talkLink').click();

I used the setTimeout function and a jQuery UI Dialog box to build a quick “Do you require assistance?” alert. Works a treat.

Posted in Programming | Tagged , , , , , | Leave a comment

perl password prompts

I spent a while trying to find a nice solution for a password prompt without writing to much code. This is the best solution I found, the IO::Prompt module! It’s awesome!

use IO::Prompt;
my $pswd = prompt("password for <your name here>: ", -e => '*');

Hopefully this can save someone some time.

Posted in Programming | Tagged , , , | Leave a comment
  • My name is Will. I'm a Mathematics student from the UK and I love to learn, build, and play.