Submission 4063ebe0...

ChallengeBrainFuck interpreter
Submitter0x53b21be84eece2e...
Submitted at2018-44-24
Gas used4565892
/**
 * This file is part of the 1st Solidity Gas Golfing Contest.
 *
 * This work is licensed under Creative Commons Attribution ShareAlike 3.0.
 * https://creativecommons.org/licenses/by-sa/3.0/
 */

pragma solidity 0.4.24;

contract BrainFuck {
    /**
     * @dev Executes a BrainFuck program, as described at https://en.wikipedia.org/wiki/Brainfuck.
     *
     * Memory cells, input, and output values are all expected to be 8 bit
     * integers. Incrementing past 255 should overflow to 0, and decrementing
     * below 0 should overflow to 255.
     *
     * Programs and input streams may be of any length. The memory tape starts
     * at cell 0, and will never be moved below 0 or above 1023. No program will
     * output more than 1024 values.
     *
     * @param program The BrainFuck program.
     * @param input The program's input stream.
     * @return The program's output stream. Should be exactly the length of the
     *          number of outputs produced by the program.
     */
    function execute(bytes program, bytes input) public pure returns(bytes) {

        // [62 60 43 45 46 44 91 93]
        //  >  <  +  -  .  ,  [  ]

        bytes memory tape = new bytes(1024);
        bytes memory output = new bytes(1024);
        uint pointer = 0;
        uint outpointer = 0;
        uint inpointer = 0;
        uint val;

        for (uint i = 0; i < program.length; i++) {
            uint8 char = uint8(program[i]);
            if (char == 62) { // >
                pointer++;
            }
            if (char == 60) { // <
                pointer--;
            }
            if (char == 43) { // +
                val = uint8(tape[pointer]);
                tape[pointer] = bytes1(val == 255 ? 0 : val + 1);
            }
            if (char == 45) { // -
                val = uint8(tape[pointer]);
                tape[pointer] = bytes1(val == 0 ? 255 : val - 1);
            }
            if (char == 46) { // .
                output[outpointer] = tape[pointer];
                outpointer++;
            }
            if (char == 44) { // ,
                tape[pointer] = input[inpointer];
                inpointer++;
            }
            if (char == 91) { // [
                // continue;
            }
            if (char == 93) { // ]
                if (tape[pointer] != 0) {
                    uint loop = 1;
                    while (loop > 0) {
                        i--;
                        uint8 current_char = uint8(program[i]);
                        if (current_char == 91) {
                            loop--;
                        } else if (current_char == 93) {
                            loop++;
                        }
                    }
                }
            }
        }

        bytes memory out2 = new bytes(outpointer);
        for (uint j = 0; j < outpointer; j++) {
            out2[j] = output[j];
        }
        return out2;


    }
}