# Hyperblender

This program has been disqualified.

 Author Sean Submission date 2016-08-22 09:56:50.022440 Rating 6906 Matches played 19 Win rate 78.95

## Source code:

``````if input == "":
import math
log = math.log
exp = math.exp
log_half = log(0.5)
third = 1.0 / 3.0
log_third = log(1.0/3.0)
log_two_thirds = log(2.0/3.0)

if y > x:
x, y = y, x
d = y - x
if d < -60:
return x
return x + log(1.0 + exp(d))

def log_sum(xs):
s = xs[0]
for i in xrange(len(xs) - 1):
s = log_add(s, xs[i + 1])
return s

def log_mean(x, y):

class ContextTree:
def __init__(self):
self.p_self = 0.0
self.p = 0.0
self.counts = [0, 0, 0]
self.children = [None, None, None]
def update(self, history, c, d, i=0):
t = sum(self.counts) + 3.0
self.p_self += log((self.counts[c] + 1.0) / t)
if i >= min(len(history) - 1, d):
self.p = self.p_self
return
x = history[i]
self.counts[c] += 1
if self.children[x] is None:
self.children[x] = ContextTree()
self.children[x].update(history, c, d, i + 1)
p_children = 0.0
for child in self.children:
if child is not None:
p_children += child.p
self.p = log_mean(self.p_self, p_children)
def predict(self, history, ps, d, i=0):
t = sum(self.counts) + 3.0
p_self = (self.p_self + log((self.counts[c] + 1.0) / t) for c in xrange(3))
if i >= min(len(history) - 1, d):
for i, p in enumerate(p_self):
ps[i] += p
return
x = history[i]
p_children = [0.0 for _ in self.children]
factor = 0.0
for y, child in enumerate(self.children):
if child is not None:
if y == x:
child.predict(history, p_children, d, i + 1)
else:
factor += child.p
elif y == x:
factor += log_third
for j, p in enumerate(p_children):
p_children[j] = p + factor
p_below = (log_mean(ps, pc) for ps, pc in zip(p_self, p_children))
for i, p in enumerate(p_below):
ps[i] += p

import collections
import random

R, P, S = range(3)
index = {"R": R, "P": P, "S": S}
name = ("R", "P", "S")
beat   = (P, S, R)
beaten = (S, R, P)
model = ContextTree()
our_model = ContextTree()
their_model = ContextTree()
history = collections.deque([])
our_history = collections.deque([])
their_history = collections.deque([])
output = random.choice(name)
p_totals = [0.0 for _ in xrange(4)]
pss = [[log_third for _ in xrange(3)] for _ in xrange(4)]
else:
i = index[input]
j = index[output]
for k in xrange(4):
p_totals[k] += pss[k][i]
model.update(history, i, 24)
our_model.update(our_history, i, 8)
their_model.update(their_history, i, 8)
history.appendleft(i)
history.appendleft(j)
our_history.appendleft(j)
their_history.appendleft(i)
ps = [0.0, 0.0, 0.0]
model.predict(history, ps, 24)
our_ps = [0.0, 0.0, 0.0]
model.predict(history, our_ps, 8)
their_ps = [0.0, 0.0, 0.0]
model.predict(history, their_ps, 8)
pss1 = [ps, our_ps, their_ps]
for k, psk in enumerate(pss1):
t = log_sum(psk)
for l, p in enumerate(psk):
psk[l] = p - t
pss[k] = psk
ps = [0.0, 0.0, 0.0]
for k in xrange(3):
ps[k] = log_sum([p_totals[l] + pss[l][k] for l in xrange(4)])
p0 = min(ps)
for i, p in enumerate(ps):
ps[i] = exp(p - p0)
scores = [0, 0, 0]
t = sum(ps)
for _ in xrange(3):
x = 0
r = random.uniform(0, t)
for k, p in enumerate(ps):
x += p
if x >= r:
break
scores[beat[k]]   += 1
scores[beaten[k]] -= 1
m = max(scores)
output = name[random.choice([k for k, x in enumerate(scores) if x == m])]``````