HashSkip2

AuthorSean
Submission date2018-08-31 05:12:35.903850
Rating6572
Matches played275
Win rate66.18

Use rpsrunner.py to play unranked matches on your computer.

Source code:

if input == "":
    
    import collections
    import random

    gamma = random.gammavariate
    hash_code = [random.randint(0, 0xFFFFFFFF) for _ in range(27)]
    table = collections.defaultdict(lambda: [1., 1., 1.])
    names = ("R", "P", "S")
    index = {"R": 0, "P": 1, "S": 2}
    history = collections.deque([])
    def toggle(h, i):
        return h ^ hash_code[3 * i + history[i]]
    best_move = random.randrange(3)
    output = random.choice(names)
    rot_table = [[1., 1., 1.] for _ in range(3)]
    rot = random.randrange(3)
    prev_rot = random.randrange(3)
    ctxts = []
else:
    inp = index[input]
    rot_table[prev_rot][(inp - best_move) % 3] += 1
    out = index[output]
    for ctxt in ctxts:
        ctxt[inp] += 1
    history.appendleft(inp)
    history.appendleft(out)
    stats = [0] * 3
    max_score = 0
    best_move = 0
    m = min(len(history), 7)
    ctxts = []
    def check(h):
        ctxt = table[h]
        ctxts.append(ctxt)
        global max_score, best_move
        counts = [gamma(n, 1) for n in ctxt]
        scores = [counts[(i + 2) % 3] - counts[(i + 1) % 3] for i in range(3)]
        best_score = max(scores) / sum(counts)
        if best_score >= max_score:
            max_score = best_score
            best_move = scores.index(max(scores))
    h = 0
    check(h)
    for i in range(m):
        h = toggle(h, i)
        check(h)
        for j in range(i):
            h1 = toggle(h, j)
            check(h1)
            for k in range(j):
                h2 = toggle(h1, k)
                check(h2)
                #for l in range(k):
                #    h3 = toggle(h2, l)
                #    check(h3)
            rot_context = rot_table[rot]
    rot_counts = [gamma(n, 1) for n in rot_context]
    rot_scores = [rot_counts[(i + 2) % 3] - rot_counts[(i + 1) % 3] for i in range(3)]
    prev_rot = rot
    rot = rot_scores.index(max(rot_scores))
    output = names[(best_move + rot) % 3]