www.sandromaffiodo.com
/homepage /list +nerd

Contact: yt tw fb in mail git


Tags: [all] obfuscated rubik arduino vgax terminal ebook epub esp8266 espvgax game javascript php scifi ioccc piano music book youtube curl

IOCC 2015 not winning entries

Sometimes you win sometimes you NOT. One year after my 2014 Winning entries i have participated another times to the IOCCC contest without winning nothing. But here they are ALL of the programs that i have created for the nerdy contest!

SOUNDOFPI

pi

OVERVIEW

This is a song generator. The generated song is based on an obscure sequence of numbers. Who can understand what is this sequence?

BUILD

$(CC) prog.c -o prog

RUN

./prog 1000 > song.wav

FEATURES

This program generate a 44100Hz 16bit mono audio file. The length and duration of the file depends on the argument given to the program. You can try with these values:

./prog 200
./prog 1000
./prog 6000

The more the number is great, the more the length of the file will be big.

WAV EXAMPLE

DOCS
SOURCECODE

SIERPINSKI

sierpinski

OVERVIEW

This is a fractals generator. It generates a fractal like the Sierpinski Carpet but little different.

BUILD

$(CC) prog.c -o prog

RUN

echo 27 | ./prog > image.ppm

To view the generate image you can use GIMP or ImageMagick to convert the image

convert image.ppm image.jpg

FEATURES

This program reads a number N from stdin and generates a PPM image with resolution NxN

SIERPINSKI CARPET in 149 bytes

This is the standard Sierpinski Carpet fractal, if you don't want my fractal:

a,b,c,x,y;main(){scanf("%d",&a);printf("P5 %d %d 1 ",a,a);for(;b!=a*a;b++){x=b%a;y=b/a;c=1;while(c&&x||y){c&=!(x%3&1&&y%3&1);x/=3;y/=3;}putchar(c);}}

DOCS
SOURCECODE

PACMAN

pacman pacman-screenshot

OVERVIEW

This is not a pizza. Obviously this is a tribute to the famous PAC MAN game.

BUILD

$(CC) prog.c -lSDL -o prog

RUN

cat level-ioccc | ./prog && echo YOU WIN!

Or

cat level-classic | ./prog && echo YOU WIN!

The program return zero if you win, not-zero if you are a loser.

FEATURES

This simple program is a partial implementation of the original PAC MAN . The gameplay is a bit ' different : Ghosts , once eaten , never become two eyes and never return ghosts . This difference made ​​the game easier.

The program does not count the lives or points . Just wait until the player dies or wins ( by collecting all points / coins ) .

The program does not implement a traditional pathfinding algorithm , but use one that I wrote specifically for the event ( see chapter Pathfinding ) .

The description of the level is read from stdin so you can customize what you want . You can add ghosts , walls and dots .

The game sprites are contained in a single bitmap that you can change, if you like.

LEVEL DESCRIPTION

Each line must be 21 characters long.

These are the supported characters:

PATHFINDING

Instead of implementing one of the standard pathfinding algorithms i've decided to write a cooperative pathfinding algorithm that use a single weighted map for all ghosts. The code is small and seems to do the job.

The algorithm follow these three rules:

  1. every time the player moves, the global weighted table is computed using this formula:

    cell.weight = manhattan_distance(cell.pos, pacman.pos)

    If the cell is a wall its weight is infinitely great.

  2. each ghost moves to one of its nearest cells (up, down, left, right), choosing the one with the lowest weight
  3. when a ghost leaves a cell, the cell weight is multiplied by two

NOTES

The program will not terminate until the player win or dies. Press ESCAPE to quit.

DOCS
SOURCECODE

Two examples of levels build for this awesome and clearly clean program:

CHIP8

chip8 chip8_A chip8_B chip8_C chip8_D

OVERVIEW

As you can see , it is an emulator CHIP8. CHIP8 is an old virtual machine used in the 70s and 80s. It was used inside the VIP COSMAC , TELMAC 1800 and in some other microcomputer.

CHIP8 virtual machine has this features:

BUILD

$(CC) prog.c -lSDL -o prog

RUN

cat BRIX | ./prog

You can try one of these games (downloaded from here):

(DE)OBFUSCATION

The main obfuscation tricks is to encode the instruction set inside the V string:

*V="`__m__`__mm________`_____a_____b___`_c___`ad___a_e___`_f___"
    "`ag___aag__`aag__aaag__baag__caag__daag__eaag__faag__maah_"
    "__a_i_____j_____k___`_l___bbm_hmabm_i`abn__fabn__iabn_`dab"
    "n_`gabn_`mabn_ahabn_bbabn_ddabn_eda"

This string is a simple sequence encoded in base16. If you decodes it correctly you obtain this:

*V="100E00100EE000000001000002000003000104000125000206000107000"
    "12800022800122800222800322800422800522800622800722800E2290"
    "0020A00000B00000C00010D00033E09E23E0A123F00723F00A23F01523"
    "F01823F01E23F02923F03323F05523F0652"

Each subsequence of 6 characters contain these informations:

1 00E0 0
| |__| |
|    | +-- instruction parameters format (0=_NNN, 
|    |                                    1=_XNN, 
|    |                                    2=_XY_, 
|    |                                    3=_XYN)
|    |
|    +---- instruction opcode
|
+--------- instruction mask (0=1111000000000000, 
                             1=1111111111111111, 
                             2=1111000000001111, 
                             3=1111000011111111)

With these informations is pretty simple to recognize each one of the 35 instructions of the CHIP8, by searching a match inside the V string. This solution is obviously really slow but the resulting code is pretty fun. This is the main switch used to decode one instruction, rewritten using the _ macro:

#define _ ;}else if(((o=0),(c=*n++-95),\
            k(12),k(8),k(4),k(0),\
            (f=*n++-95),1)&&(q&r[c])==o){\
                s[f]();

void instruction_set_switch() {
    if (0) {
    _ _ _ _ _ 
    _ _ _ _ _ 
    _ _ _ _ _ 
    _ _ _ _ _ 
    _ _ _ _ _ 
    _ _ _ _ _ 
    }
}

From here you can de obfuscate the rest of the program. It's pretty simple to do... don't you think? :D

NOTES

You can slow down the emulator by increasing the parameter of

SDL_Delay(3);

You can change the zoom factor of the screen by editing this variable

I=4

DOCS
SOURCECODE

SUBLEQ

subleq-ob

Some INFO extracted from the official DOCS


OVERVIEW

This is a simple SUBLEQ emulator. It also embeds a SUBLEQ interpreter writen in SUBLEQ.

SUBLEQ is a simple OISC machine: A one instruction set computer (OISC), sometimes called an ultimate reduced instruction set computer (URISC), is an abstract machine that uses only one instruction [...] an OISC is capable of being a universal computer in the same manner as traditional computers that have multiple instructions.

SUBLEQ instruction is defined like this:

The subleq instruction ("SUbtract and Branch if Less than or EQual to zero") subtracts the contents at address A from the contents at address B, stores the result at address B, and then, if the result is not positive, transfers control to address C (if the result is positive, execution proceeds to the next instruction in sequence)

A SUBLEQ instruciton looks like this:

A B C 

This emulator execute its input until the program jump to the address -1 (C==-1).

This emulator can also read values from stdin, write to stdout and allocate some memory. To do that i've overloaded the original meaning of the SUBLEQ instruction by adding two special memory addresses: -1 and -2. These addresses must be used like that:

To terminate the sourcecode of your program you must use the magic number:

-65535

Your programs cannot be greater than 64 kilobytes of RAM.


SOURCECODE

IOCC 2014 not winning entries

In the year of the Lord (of the Ring) 2014 i won two times the IOCCC contest for my [Super Mario Bros Engine]() (aka MARIO) and [ASCII ART Generator]() (aka ART) programs.

These are others three programs that i have created and send to the 2014 contest but, unfortunately, was not winning entries.

THIRD

third third_A third_B third_C third_D

Some info extracted from the official DOCS:


OVERVIEW

This is not another Ray Tracer.

This is a classical 3D Software Rasterizer that supports:

The program reads an infinite list of frame descriptors from stdin and generate an infinite sequence of rendered frames to stdout.

HOW IT WORK

The program starts reading the output resolution from the command line arguments; then reads a raw BGRA texture image from stdin.

Texture format

The texture format is very simple:

Frame Descriptor

After that, the program continue to reading an infinite list of frame descriptor from stdin. Each frame descriptor is encoded in ASCII and contain a list of float values, comma separated:

The list of float values must terminate with the "E" character. For each 3 vertices the renderer will generate a single triangle.

Example

This is an example of a descriptor for one coloured triangle:

-1.81066, 0, 0, 0,
0, -2.414213, 0, 0,
0, 0, -1.083333, -1,
0, 0, -8.333333, 0,

1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, -10, 1,

10, 10, 1, 1, 

0, 0, 1, 1,     1, 0, 0,    0, 0,   -1, -1, 1, 1,
0, 0, 1, 1,     1, 0, 1,    1, 0,   1, -1,  1, 1,
0, 0, 1, 1,     1, 1, 1,    1, 1,   1,  1,  1, 1,

E

This command will generate the same triangle in the file frame.bgra, using a white 2x2 texture:

$ printf "\001\077\077\077\077\077\077\077\077\077\077\077\077\077\077\077\077 -1.81066, 0, 0, 0, 0, -2.414213, 0, 0, 0, 0, -1.083333, -1,0, 0, -8.333333, 0,1, 0, 0, 0,0, 1, 0, 0,0, 0, 1, 0,0, 0, -10, 1, 10, 10, 1, 1, 0, 0, 1, 1,1, 0, 0, 0, 0, -1, -1, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0, 1, -1,  1, 1, 0, 0, 1, 1, 1, 1, 1,1, 1, 1,  1,  1, 1, E" | ./prog 320 200 > frame.bgra

To convert frame.bgra to JPG you can use this:

convert -size 320x200 -depth 8 frame.bgra frame.jpg

SOURCECODE

Some shell scripts used to generate the required input data:

Sourcecode (obfuscated, obviously) of the additional tools used to test and prepare the required input data:

ALIEN

alien alien-terminal

INFO extracted from the official DOCS


OVERVIEW

Obviously this is a SPACE INVADER game. It use NCURSES to get the terminal screen size and handle inputs/outputs.

The program return 0 if the user win or not-zero if the user is A LOSER.

RUN

To run the program type

./prog

You can move the ship to the LEFT using key A and to the RIGHT using any other key. Use SPACE to shoot a rocket.

The program return 0 if the user win so you can test it and make something usefull (or not), like that:

./prog && echo "you win"

Try the following alias! Every time you type ls you need to beat the aliens to list your files! :D

alias ls="'`pwd`/prog' && ls"

This is The mad SOURCECODE.

KUBRIK

kubrik

INFO extracted from the official DOCS


OVERVIEW

How small can be a Rubik's cube solver?

This is a Rubik's cube solver.

The program solves a 3x3 Rubik's cube using a modified version of the method "layer by layer" (explained here: http://solvethecube.com/).

This is not a bruteforce solver. One bruteforce solver takes too time to find a solution... This program will find its in the blink of an eye.

The program reads a scramble sequence from stdin and writes the solution to stdout. The program will not solve the cube by reversing the scramble sequence (this would be too simple!!). It uses the method "layer by layer", and is capable of solving any permutation of the cube (one of 43252003274489856000 permutations).

This program works with a cube with a standard color schema.

RUN

The program starts with a solved cube. You need to type a sequence of moves to scramble the cube.

You can get a scramble sequence from http://www.jaapsch.net/scramble_cube.htm or use one of these scramble:

L U B2 F' L' R B2 L' R2 F' R F2 D U' B D U' B U F'

B2 F' L U' F D' B D2 U2 R' U2 F' U F2 D' U2 B D2 U R'

L' R2 D2 U F2 D2 U2 L2 R2 B2 F D B2 U' L2 R2 D' B F' D

R U B2 L2 U2 F R'

The starts position of your cube MUST BE with the GREEN FACE on front and the WHITE FACE on top. The scramble sequence MUST NOT include the rotation moves: x, y, z.

You can type the scramble to the program using EOF (CTRL+D) to terminate the sequence, or use a pipe:

$ echo "R U B2 L2 U2 F R'" | ./prog

SOURCECODE

OI Ultimate reducted RISC machine

This is an old project that i've made in the past.. when a spider has bitten me and gave me the powers of the DORK NERD.

What is OI?

oi

OI is a small interpreter of an OISC machine.

Wikipedia define an OISC like this:

"A one instruction set computer (OISC), sometimes called an ultimate reduced instruction set computer (URISC), is an abstract machine that uses only one instruction [...] an OISC is capable of being a universal computer in the same manner as traditional computers that have multiple instructions."

OI is a SUBLEQ interpreter. SUBLEQ is an instruction defined like this:

"The subleq instruction ("SUbtract and Branch if Less than or EQual to zero") subtracts the contents at address A from the contents at address B, stores the result at address B, and then, if the result is not positive, transfers control to address C (if the result is positive, execution proceeds to the next instruction in sequence)"

The primarly nerdy part of this project WAS NOT to implement an OISC machine, but was to write an OISC interpreter using only the single instruction SUBLEQ. Really absurd huh? Yeeeah, like things that nerdy do.

Check out this simple and clean SUBLEQ interpreter writen in SUBLEQ:

-2 341 3 334 334 6 341 334 9 334 39 12 334 40 15 334 43 18 -1 330 21 330 -1 24 335 335 27 330 335 30 336 336 33 347 336 36 336 335 57 0 0 42 330 0 45 337 43 48 337 39 51 337 40 54 334 334 18 346 -2 60 346 -2 63 346 -2 66 334 334 69 341 334 72 334 102 75 334 105 78 334 108 81 337 105 84 337 108 87 337 108 90 343 343 93 331 331 96 332 332 99 333 333 102 0 331 105 0 332 108 0 333 111 334 334 114 338 334 117 331 334 159 334 334 123 337 334 126 331 334 159 334 334 132 335 335 135 341 334 138 334 335 141 331 335 144 237 237 147 334 334 150 335 334 153 334 237 156 334 334 168 340 343 162 237 237 165 331 237 168 334 334 171 338 334 174 332 334 222 334 334 180 337 334 183 332 334 222 334 334 189 335 335 192 341 334 195 334 335 198 332 335 201 238 238 204 246 246 207 334 334 210 335 334 213 334 238 216 334 246 219 334 334 237 238 238 225 246 246 228 332 238 231 332 246 234 340 343 237 0 0 240 337 343 267 334 334 246 0 334 249 335 335 252 334 335 267 339 102 258 339 105 261 339 108 264 334 334 90 334 334 270 333 334 327 334 334 276 335 335 279 341 334 282 334 335 285 333 335 288 342 342 291 334 334 294 335 342 297 102 102 300 105 105 303 108 108 306 342 102 309 342 105 312 342 108 315 337 105 318 337 108 321 337 108 324 334 334 90 334 334 -1 0 0 0 0 0 0 0 -1 -2 -3 1 0 0 0 33 35 10 65535 -65535

Here you can look at the assembler sourcecode that i've used to write the SUBLEQ interpreter in SUBLEQ. The assembler language used was created by me to simplify the writing process. Here you can see the assembler compiler i've writen for that purpose. OMG, what a nerdy thing!!

Read more on OI homepage.

This SUBLEQ interpreter was submitted on the IOCCC 2015 competion but unfortunately didn't win any awards. Check out this post about my SUBLEQ obfuscated IOCCC 2015 entry.

Have fun!

IOCCC 2014 Winning entries

Long time ago (2014), in a galaxy far far away, i have won two nominations at the Internatonal Obfuscated Code Contest. I remember when I discovered the IOCCC, back in 2003, thanks to EFFEOTTO ... I would have never thought to participate and win something .. but in a single year i have won two times! Incredible :-)

Homage to a classic game

This is the source code of the first winning entry:

obfuscated-program

The program is not only a Super Mario Bros partial implementation but also a generic Engine for Platform Games. It can be used to create games like Super Mario Bros. The contest entry was released with two example games level: one level of Super Mario Bros and one level of Giana the Sister (an SMB-like game for Amiga).

These are two screenshots of the two playable levels:

super-mario-bros

giana-the-sister

The Engine implements all the needed stuff:

These are the IOCCC Selected Judges Remarks about this program:

"A classic for a particular generation. Like all good programs, being data driven means you can do fun things in small spaces."

Here you can download the obfuscated sourcecode. If you open the file with and see some ugly alignment, set the TAB WIDTH of your text editor equal to 4. :-)

Here you can download the not obfuscated sourcecode. The code is already unreadable due to the shrink in sizes and due to the multiple rewrite steps (needed to simplify all the internal logic of the Engine).

Here you can read the official IOCCC winning entry description.

Most tweetable entry

The second winning entry is very little. This is the program full sourcecode:

d=80,e,j;g(){j+=getchar();}main(a,b)char**b;{for(;;){j=0;g();if(j<0)break;g(g());putchar(b[1][j/3*strlen(b[1])>>8]);if(!(++e%d))puts("");}}

There is also a shortest version of the same program, with a simplified logic:

d=80,e,j;g(){j+=getchar();}main(){for(;;){j=0;g();if(j<0)break;g(g());putchar(" .:#@"[j/3*5>>8]);if(!(++e%d))puts("");}}

What the hell do the program do?

monna-lisa

The program convert the image given in input to an ASCII ART!

These are the IOCCC Selected Judges Remarks about this program:

"On the face of it :-) given what this program one might wonder what makes this winner special. But when you realize the source is small enough to tweet on twitter you understand.

Who will be the first to tweet this source? How many re-tweets will such tweet get? And how many people will really understand the tweet?"

Here you can read the official IOCCC winning entry description.

...

Now... Really... How much is insanely NERD all of this?