Last active
August 9, 2019 21:13
-
-
Save kkwteh/b81d8e599ec46a2d64b096f953a11e63 to your computer and use it in GitHub Desktop.
Script to beat https://www.expunctis.com/2019/03/07/Not-so-random.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
from collections import defaultdict, namedtuple | |
def penultimate_five_gram(hist): | |
return ''.join(hist[-6:-1]) | |
def last_five_gram(hist): | |
return ''.join(hist[-5:]) | |
def chunks(l, n): | |
"""Yield successive n-sized chunks from l.""" | |
for i in range(0, len(l), n): | |
yield l[i:i + n] | |
ctrs = defaultdict(lambda: [0,0]) | |
hist = [] | |
for i in range(100): | |
if len(hist) < 6 or ctrs[last_five_gram(hist)][0] == ctrs[last_five_gram(hist)][1]: | |
next_letter = random.choice(['L','R']) | |
else: | |
if ctrs[last_five_gram(hist)][0] < ctrs[last_five_gram(hist)][1]: | |
next_letter = 'L' | |
else: | |
next_letter = 'R' | |
hist.append(next_letter) | |
if len(hist) >= 6: | |
if hist[-1] == 'L': | |
ctrs[''.join(penultimate_five_gram(hist))][0] += 1 | |
else: | |
ctrs[''.join(penultimate_five_gram(hist))][1] += 1 | |
print(list(chunks(''.join(hist), 5))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment