Example #1
0
def test(leading, trailing, possibles):
    if tuple([leading]+[trailing] + possibles) in checked:
        return 0
    checked.add(tuple([leading]+[trailing] + possibles))
    if ((leading + trailing + sum(possibles)) % 3) != 0:
        return 0
#    print [leading] + sorted(possibles) + [trailing]

    for perm in permutations(possibles):
        candidate = [1] + list(perm) + [trailing]
        c = numberize(candidate)
        base = sorted(candidate)
        found = 0
        for factor in [6,5,4,3,2]:
            z = c*factor
#            print c, z, base, sorted(digitize(z))
            if sorted(digitize(z)) != base:
                if found > 2:
                    print 'almost', c, factor
                break
            else:
                found += 1
        else:
            print '**!!!', c
            return c
    return 0
Example #2
0
 def app(ast):
     if isinstance(ast, astnode) and ast.fun == head:
         funargs = []
         for a in ast.args:
             if isinstance(a, astnode) or not utils.is_numberlike(a.val):
                 return
             else:
                 funargs.append(utils.numberize(a.val))
         return token(str(transform(*funargs)), *ast.metadata)
Example #3
0
 def app(ast):
     if isinstance(ast, astnode) and ast.fun == head:
         funargs = []
         for a in ast.args:
             if isinstance(a, astnode) or not utils.is_numberlike(a.val):
                 return
             else:
                 funargs.append(utils.numberize(a.val))
         return token(str(transform(*funargs)), *ast.metadata)
Example #4
0
def solution():
    results = defaultdict(list)
    for c in candidates():
        digits = repeated_digits(c)
        for digit in digits:
            for key in make_keys(c, digit):
                results[key].append(numberize(c))
                if len(results[key]) >= 7:
                    print len(results[key]), key, results[key]
                    if len(results[key]) > 7:
                        return sorted(results[key])[0]
    return 0            
Example #5
0
def compile_lll(ast):
    symb = mksymbol()
    # Literals
    if not isinstance(ast, utils.astnode):
        return [utils.numberize(ast)]


    subcodes = map(compile_lll, ast.args[1:])

    # Seq
    if ast.fun == 'seq':
        o = []
        for subcode in subcodes:
            o.extend(subcode)
        return o
    elif ast.fun == 'unless':
        assert len(ast.args) == 3
        return subcodes[0] + ['$endif'+symb, 'JUMPI'] + \
               subcodes[1] + ['~endif'+symb]
    elif ast.fun == 'if':
        assert len(ast.args) == 4
        return subcodes[0] + ['NOT', '$else'+symb, 'JUMPI'] + \
               subcodes[1] + ['$endif'+symb, 'JUMP', '~else'+symb] + \
               subcodes[2] + ['~endif'+symb]
    elif ast.fun == 'until':
        return ['~beg'+symb] + subcodes[0] + ['$end'+symb, 'JUMPI'] + \
               subcodes[1] + ['$beg'+symb, 'JUMP', '~end'+symb]
    elif ast.fun == 'lll':
        LEN = '$begincode'+symb+'.endcode'+symb
        STARTSYMB, STARTIND = '~begincode'+symb, '$begincode'+symb
        ENDSYMB, ENDIND = '~endcode'+symb, '$endcode'+symb
        return [LEN, 'DUP'] + subcodes[1] + [STARTIND, 'CODECOPY'] + \
               [ENDIND, 'JUMP', STARTSYMB, '#CODE_BEGIN'] + subcodes[0] + \
               ['#CODE_END', ENDSYMB]
    elif ast.fun == 'alloc':
        return subcodes[0] + ['MSIZE', 'SWAP', 'MSIZE'] + \
               ['ADD', 1, 'SWAP', 'SUB', 0, 'SWAP', 'MSTORE8']
    elif ast.fun == 'array_lit':
        x = ['MSIZE', 'DUP']
        for s in subcodes:
            x += s + ['SWAP', 'MSTORE', 'DUP', 32, 'ADD']
        return x[:-3] if len(subcodes) > 0 else ['MSIZE']
    else:
        o = []
        for subcode in subcodes[::-1]:
            o.extend(subcode)
        return o + [ast.fun]
Example #6
0
def dereference(c):
    c = map(utils.tokenify, c)
    label_length = utils.log256(len(c) * 4)
    iq = [x for x in c]
    mq = []
    pos = 0
    labelmap = {}
    beginning_stack = [0]
    while len(iq):
        front = iq.pop(0)
        if not utils.is_numeric(front.val) and front.val[0] == '~':
            labelmap[front.val[1:]] = pos - beginning_stack[-1]
        elif front.val == '#CODE_BEGIN':
            beginning_stack.append(pos)
        elif front.val == '#CODE_END':
            beginning_stack.pop()
        else:
            mq.append(front)
            if utils.is_numeric(front.val):
                pos += 1 + max(1, utils.log256(front.val))
            elif front.val[:1] == '$':
                pos += label_length + 1
            else:
                pos += 1
    oq = []
    for m in mq:
        oqplus = []
        if utils.is_numberlike(m.val):
            m.val = utils.numberize(m.val)
        if utils.is_numeric(m.val):
            L = max(1, utils.log256(m.val))
            oqplus.append('PUSH' + str(L))
            oqplus.extend(utils.tobytearr(m.val, L))
        elif m.val[:1] == '$':
            vals = m.val[1:].split('.')
            if len(vals) == 1:
                oqplus.append('PUSH' + str(label_length))
                oqplus.extend(utils.tobytearr(labelmap[vals[0]], label_length))
            else:
                oqplus.append('PUSH' + str(label_length))
                value = labelmap[vals[1]] - labelmap[vals[0]]
                oqplus.extend(utils.tobytearr(value, label_length))
        else:
            oqplus.append(m)
        oq.extend(map(lambda x: utils.tokenify(x, *m.metadata), oqplus))
    return oq
Example #7
0
def dereference(c):
    c = map(utils.tokenify, c)
    label_length = utils.log256(len(c)*4)
    iq = [x for x in c]
    mq = []
    pos = 0
    labelmap = {}
    beginning_stack = [0]
    while len(iq):
        front = iq.pop(0)
        if not utils.is_numeric(front.val) and front.val[0] == '~':
            labelmap[front.val[1:]] = pos - beginning_stack[-1]
        elif front.val == '#CODE_BEGIN':
            beginning_stack.append(pos)
        elif front.val == '#CODE_END':
            beginning_stack.pop()
        else:
            mq.append(front)
            if utils.is_numeric(front.val):
                pos += 1 + max(1, utils.log256(front.val))
            elif front.val[:1] == '$':
                pos += label_length + 1
            else:
                pos += 1
    oq = []
    for m in mq:
        oqplus = []
        if utils.is_numberlike(m.val):
            m.val = utils.numberize(m.val)
        if utils.is_numeric(m.val):
            L = max(1, utils.log256(m.val))
            oqplus.append('PUSH' + str(L))
            oqplus.extend(utils.tobytearr(m.val, L))
        elif m.val[:1] == '$':
            vals = m.val[1:].split('.')
            if len(vals) == 1:
                oqplus.append('PUSH'+str(label_length))
                oqplus.extend(utils.tobytearr(labelmap[vals[0]], label_length))
            else:
                oqplus.append('PUSH'+str(label_length))
                value = labelmap[vals[1]] - labelmap[vals[0]]
                oqplus.extend(utils.tobytearr(value, label_length))
        else:
            oqplus.append(m)
        oq.extend(map(lambda x: utils.tokenify(x, *m.metadata), oqplus))
    return oq
Example #8
0
def compile_lll(ast):
    symb = mksymbol()
    tokenify2 = lambda x: utils.tokenify(x, *ast.metadata)
    # Literals
    if not isinstance(ast, astnode):
        return [token(utils.numberize(ast.val), *ast.metadata)]
    subcodes = map(compile_lll, ast.args)
    # Seq
    if ast.fun == 'seq':
        o = []
        for subcode in subcodes:
            o.extend(subcode)
        return o
    elif ast.fun == 'unless' and len(ast.args) == 2:
        out = subcodes[0] + ['$endif'+symb, 'JUMPI'] + \
            subcodes[1] + ['~endif'+symb]
    elif ast.fun == 'if' and len(ast.args) == 3:
        out = subcodes[0] + ['NOT', '$else'+symb, 'JUMPI'] + \
            subcodes[1] + ['$endif'+symb, 'JUMP', '~else'+symb] + \
            subcodes[2] + ['~endif'+symb]
    elif ast.fun == 'until':
        out = ['~beg'+symb] + subcodes[0] + ['$end'+symb, 'JUMPI'] + \
            subcodes[1] + ['$beg'+symb, 'JUMP', '~end'+symb]
    elif ast.fun == 'lll':
        LEN = '$begincode' + symb + '.endcode' + symb
        STARTSYMB, STARTIND = '~begincode' + symb, '$begincode' + symb
        ENDSYMB, ENDIND = '~endcode' + symb, '$endcode' + symb
        out = [LEN, 'DUP'] + subcodes[1] + [STARTIND, 'CODECOPY'] + \
            [ENDIND, 'JUMP', STARTSYMB, '#CODE_BEGIN'] + subcodes[0] + \
            ['#CODE_END', ENDSYMB]
    elif ast.fun == 'alloc':
        out = subcodes[0] + ['MSIZE', 'SWAP', 'MSIZE'] + \
            ['ADD', 1, 'SWAP', 'SUB', 0, 'SWAP', 'MSTORE8']
    elif ast.fun == 'array_lit':
        x = ['MSIZE', 'DUP']
        for s in subcodes:
            x += s + ['SWAP', 'MSTORE', 'DUP', 32, 'ADD']
        out = x[:-3] if len(subcodes) > 0 else ['MSIZE']
    else:
        o = []
        for subcode in subcodes[::-1]:
            o.extend(subcode)
        out = o + [token(ast.fun, *ast.metadata)]
    return map(tokenify2, out)
Example #9
0
def compile_lll(ast):
    symb = mksymbol()
    tokenify2 = lambda x: utils.tokenify(x, *ast.metadata)
    # Literals
    if not isinstance(ast, astnode):
        return [token(utils.numberize(ast.val), *ast.metadata)]
    subcodes = map(compile_lll, ast.args)
    # Seq
    if ast.fun == 'seq':
        o = []
        for subcode in subcodes:
            o.extend(subcode)
        return o
    elif ast.fun == 'unless' and len(ast.args) == 2:
        out = subcodes[0] + ['$endif'+symb, 'JUMPI'] + \
            subcodes[1] + ['~endif'+symb]
    elif ast.fun == 'if' and len(ast.args) == 3:
        out = subcodes[0] + ['NOT', '$else'+symb, 'JUMPI'] + \
            subcodes[1] + ['$endif'+symb, 'JUMP', '~else'+symb] + \
            subcodes[2] + ['~endif'+symb]
    elif ast.fun == 'until':
        out = ['~beg'+symb] + subcodes[0] + ['$end'+symb, 'JUMPI'] + \
            subcodes[1] + ['$beg'+symb, 'JUMP', '~end'+symb]
    elif ast.fun == 'lll':
        LEN = '$begincode'+symb+'.endcode'+symb
        STARTSYMB, STARTIND = '~begincode'+symb, '$begincode'+symb
        ENDSYMB, ENDIND = '~endcode'+symb, '$endcode'+symb
        out = [LEN, 'DUP'] + subcodes[1] + [STARTIND, 'CODECOPY'] + \
            [ENDIND, 'JUMP', STARTSYMB, '#CODE_BEGIN'] + subcodes[0] + \
            ['#CODE_END', ENDSYMB]
    elif ast.fun == 'alloc':
        out = subcodes[0] + ['MSIZE', 'SWAP', 'MSIZE'] + \
            ['ADD', 1, 'SWAP', 'SUB', 0, 'SWAP', 'MSTORE8']
    elif ast.fun == 'array_lit':
        x = ['MSIZE', 'DUP']
        for s in subcodes:
            x += s + ['SWAP', 'MSTORE', 'DUP', 32, 'ADD']
        out = x[:-3] if len(subcodes) > 0 else ['MSIZE']
    else:
        o = []
        for subcode in subcodes[::-1]:
            o.extend(subcode)
        out = o + [token(ast.fun, *ast.metadata)]
    return map(tokenify2, out)
Example #10
0
def analyze_and_varify_ast(ast, data):
    if isinstance(ast, astnode):
        if ast.fun in ['alloc', 'array_lit']:
            data['alloc_used'] = True
        if ast.fun == 'lll':
            inner = {"varhash": {}, "inner": []}
            argz = [finalize(ast.args[0], inner),
                    analyze_and_varify_ast(ast.args[1], data)]
            data["inner"].append(inner)
        else:
            argz = map(lambda x: analyze_and_varify_ast(x, data), ast.args)
        return astnode(ast.fun, argz, *ast.metadata)
    elif utils.is_numberlike(ast.val):
        return token(str(utils.numberize(ast.val)), *ast.metadata)
    else:
        if ast.val not in data['varhash']:
            data['varhash'][ast.val] = str(len(data['varhash']) * 32)
        if ast.val == '__msg.data':
            data['msgdata_used'] = True
        return token(data['varhash'][ast.val], *ast.metadata)
Example #11
0
def analyze_and_varify_ast(ast, data):
    if isinstance(ast, astnode):
        if ast.fun in ['alloc', 'array_lit']:
            data['alloc_used'] = True
        if ast.fun == 'lll':
            inner = {"varhash": {}, "inner": []}
            argz = [
                finalize(ast.args[0], inner),
                analyze_and_varify_ast(ast.args[1], data)
            ]
            data["inner"].append(inner)
        else:
            argz = map(lambda x: analyze_and_varify_ast(x, data), ast.args)
        return astnode(ast.fun, argz, *ast.metadata)
    elif utils.is_numberlike(ast.val):
        return token(str(utils.numberize(ast.val)), *ast.metadata)
    else:
        if ast.val not in data['varhash']:
            data['varhash'][ast.val] = str(len(data['varhash']) * 32)
        if ast.val == '__msg.data':
            data['msgdata_used'] = True
        return token(data['varhash'][ast.val], *ast.metadata)
Example #12
0
def reverse(N):
    return numberize([x for x in reversed(digitize(N))])