Example #1
0
def optimize(command, spell_list):
    moves = input_mapper.command_to_move(command)
    spell_moves = [filter(lambda move: move != input_mapper.IGNORED, input_mapper.command_to_move(spell)) for spell in spell_list]

    hashed_moves = rolling_hash(moves)
    hashed_spells = [rolling_hash(spell)[-1] for spell in spell_moves]
    spell_pow_memo = [pow(HASH_BASE, len(spell), HASH_MOD) for spell in spell_moves]

    optimized = StringIO()
    pos = 0
    while pos < len(moves):
        next_command = input_mapper.default_command(moves[pos])
        progress = 1
        for i, spell in enumerate(spell_moves):
            npos = pos + len(spell)
            if npos > len(moves): continue
            command_hash = (hashed_moves[npos] - hashed_moves[pos] * spell_pow_memo[i] % HASH_MOD + HASH_MOD) % HASH_MOD
            spell_hash = hashed_spells[i]
            if command_hash == spell_hash and moves[pos:npos] == spell:
                next_command = spell_list[i]
                progress = len(spell)
                break
        optimized.write(next_command)
        pos += progress
    return optimized.getvalue()
Example #2
0
def translate_single(config, seed, phrases=None, timelimit=3600):
    if phrases is None:
        phrases = []
    output = StringIO()
    print >> output, config["height"], config["width"]
    print >> output, len(config["units"])
    for unit in config["units"]:
        print >> output, unit["pivot"]["x"], unit["pivot"]["y"], len(unit["members"])
        for member in unit["members"]:
            print >> output, member["x"], member["y"]
    print >> output, len(config["filled"])
    for cell in config["filled"]:
        print >> output, cell["x"], cell["y"]
    print >> output, config["sourceLength"]
    rand = Rand(seed)
    for i in xrange(config["sourceLength"]):
        print >> output, rand.get() % len(config["units"])
    print >> output, len(phrases)
    for phrase in phrases:
        print >> output, len(phrase), "".join(map(str, input_mapper.command_to_move(phrase))).replace(
            str(input_mapper.IGNORED), ""
        )
    print >> output, timelimit
    return output.getvalue()