Skip to content

Instantly share code, notes, and snippets.

@cmastudios
Created December 7, 2017 19:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cmastudios/eeb9529a2bb90560dedfa73d4d3ab98a to your computer and use it in GitHub Desktop.
Save cmastudios/eeb9529a2bb90560dedfa73d4d3ab98a to your computer and use it in GitHub Desktop.
# coding: utf-8
import numpy as np
import random
code = (1, 2, 3, 4)
def play(code, guess):
red = 0
white = 0
g = list(guess)
scc = list(code)
for i in range(4):
if scc[i] == g[i]:
red += 1
scc[i] = 0
g[i] = 10
for i in range(4):
for j in range(4):
if scc[j] == g[i]:
white += 1
scc[j] = 0
g[i] = 10
return red, white
bin = [1, 2, 3, 4, 5, 6]
S = [ (x,y,z,a) for x in bin for y in bin for z in bin for a in bin ]
origS = list(S)
guess = (1, 1, 2, 2)
def score(possible):
global S, red, white
losses = 0
for s in S:
red2, white2 = play(possible, s)
if red2 != red or white2 != white:
losses += 1
return int(len(S) - losses)
def solves():
global S, code, origS, guess, red, white
moves = 0
while True:
moves += 1
print("trying " + str(guess))
red, white = play(code, guess)
if red == 4:
print("winner")
break
else:
print(len(S))
S.remove(guess)
for s in S:
red2, white2 = play(guess, s)
if red2 != red or white2 != white:
S.remove(s)
pass
scores = dict()
for possible in origS:
losses = 0
for s in S:
red2, white2 = play(possible, s)
if red2 != red or white2 != white:
losses += 1
scores[possible] = int(len(S) - losses)
optimal = sorted(scores.keys(), key=lambda k: -scores[k])
for possible in optimal:
if possible in S:
guess = possible
break
print(f"moves: {moves}")
# test random codes
while True:
S = list(origS)
code = random.choice(S)
guess = (1, 1, 2, 2)
solves()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment