Submission c9cc8e24...

ChallengeHex decoder
Submitter0x53b21be84eece2e...
Submitted at2018-50-05
Gas used54107
/**
 * 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 HexDecoder {
    /**
     * @dev Decodes a hex-encoded input string, returning it in binary.
     *
     * Input strings may be of any length, but will always be a multiple of two
     * bytes long, and will not contain any non-hexadecimal characters.
     *
     * @param input The hex-encoded input.
     * @return The decoded output.
     */

// 384586
    function decode(string input) public pure returns(bytes output) {
        // output = bytes(input);
        // uint len = output.length;

        assembly {
        
        let len := mload(input)
        output := add(input, 0x20)
        for { let i := 0 } lt (i, len) { i := add(i, 0x20) } {
        // for (uint i = 0; i < len; i += 0x20) {
            // uint j = i;
                let d := mload(add(output, i))
                d := add(
                    and(0x0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f, d),
                    mul(9, and(0x0101010101010101010101010101010101010101010101010101010101010101,
                        div(d, 64)
                    ))
                )
                mstore(0x60, mul(0x100, add(
                        and(0x000f000f000f000f000f000f000f000f000f000f000f000f000f000f000f000f, d),
                    div(and(0x0f000f000f000f000f000f000f000f000f000f000f000f000f000f000f000f00, d), 0x10)
                )))
                mstore(0x61, mload(0x62))
                mstore(0x62, mload(0x63))
                mstore(0x63, mload(0x64))
                mstore(0x64, mload(0x65))
                mstore(0x65, mload(0x66))
                mstore(0x66, mload(0x67))
                mstore(0x67, mload(0x68))
                mstore(0x68, mload(0x69))
                mstore(0x69, mload(0x6a))
                mstore(0x6a, mload(0x6b))
                mstore(0x6b, mload(0x6c))
                mstore(0x6c, mload(0x6d))
                mstore(0x6d, mload(0x6e))
                mstore(0x6e, mload(0x6f))
                mstore(0x6f, mload(0x70))
                mstore(add(output, div(i, 2)), mload(0x60))
            }
            output := sub(output, 0x20)
            mstore(output, div(len, 2))
        }
    }
}