Submission cfc44040...

ChallengeRemove duplicate elements
Submitterwicketh.eth
Submitted at2018-29-27
Gas used296840
/**
 * 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 Unique {
    
    function () external payable { assembly {
        
        // @author Remco Bloemen <[email protected]>
        mstore(0x40, 0)
        
        let i := calldataload(36)
        jumpi(main, i)
        
        // Empty list
        mstore(0, 32)
        mstore(32, 0)
        return(0, 64)
        
    main:
        // let i
        let htl
        let ptr
        let index2
        let iv
        let index1
        let scale
        let last2
        let last1
        let vhash
        
        // htl := and(mul(i, 64), 0xFFFFFFFFFFFFFE0)
        // scale := add(div(sub(0, htl), htl), 1)
        last1 := 0
        last2 := 0
        
        ptr := 0x2040
        i := 68
        
    oloop:
        // Read value
        vhash := not(calldataload(i))
        
        // Check recent values
        jumpi(seen, or(eq(vhash, last1), eq(vhash, last2)))
        last2 := last1
        last1 := vhash
        
        // Compute index 1
        index1 := and(mul(vhash, 32), 0x1fe0)
        
        // Read index 1
        iv := mload(index1)
        jumpi(unique1, iszero(iv))
        jumpi(seen, eq(iv, vhash))
        
        // Increment and test index 1
        index1 := and(add(index1, 32), 0x1fe0)
        iv := mload(index1)
        jumpi(unique1, iszero(iv))
        jumpi(seen, eq(iv, vhash))
        
        // Compute index 2
        index2 := and(div(mul(vhash,
0x1b6d296aa8b7284041b9f0e36895d18399d8026b57a51e5af0ed54c3e03bd3a1
        ), 0x1000000000000001), 0x3fe0)
        
        // Read index 2
        iv := mload(index2)
        jumpi(unique2, iszero(iv))
        jumpi(seen, eq(iv, vhash))
        
    iloop:
        // Increment and test index 1
        index1 := and(add(index1, 32), 0x1fe0)
        iv := mload(index1)
        jumpi(unique1, iszero(iv))
        jumpi(seen, eq(iv, vhash))
        
        // Incerment and test index 2
        index2 := and(add(index2, 32), 0x1fe0)
        iv := mload(index2)
        jumpi(unique2, iszero(iv))
        jumpi(seen, eq(iv, vhash))
        
        // Increment and test index 1
        index1 := and(add(index1, 32), 0x1fe0)
        iv := mload(index1)
        jumpi(unique1, iszero(iv))
        jumpi(seen, eq(iv, vhash))
        
        // Incerment and test index 2
        index2 := and(add(index2, 32), 0x1fe0)
        iv := mload(index2)
        jumpi(unique2, iszero(iv))
        jumpi(seen, eq(iv, vhash))
        
        // Loop
        jump(iloop)
        
    seen:
        // Resume loop
        i := add(i, 32)
        jumpi(oloop, lt(i, calldatasize))
        jump(oloop_end)
    
    unique2:
        // Add to the hash table
        mstore(index2, vhash)
        
        // Add to start of list
        mstore(ptr, not(vhash))
        ptr := add(ptr, 32)
        
        // Resume loop
        i := add(i, 32)
        jumpi(oloop, lt(i, calldatasize))
        jump(oloop_end)
        
    unique1:
        // Add to the hash table
        mstore(index1, vhash)
        
        // Add to start of list
        mstore(ptr, not(vhash))
        ptr := add(ptr, 32)
        
        // Resume loop
        i := add(i, 32)
        jumpi(oloop, lt(i, calldatasize))
    
    oloop_end:
        mstore(0x2000, 32)
        mstore(0x2020, div(sub(ptr, 0x2040), 32))
        return(0x2000, sub(ptr, 0x2000))
    }}
    
}