Skip to content

Instantly share code, notes, and snippets.

@avamsi
Last active February 15, 2016 17:51
Show Gist options
  • Select an option

  • Save avamsi/96e06b9cff0b7a6f0ece to your computer and use it in GitHub Desktop.

Select an option

Save avamsi/96e06b9cff0b7a6f0ece to your computer and use it in GitHub Desktop.
from itertools import chain, combinations, permutations, product
class CB(object):
def __init__(self):
self.all = list(chain(*[permutations(i) for i in combinations(xrange(10), 4)]))
self.seq = None
self.cab = None
self.cabs = [(i, j) for i, j in product(xrange(5), xrange(5)) if i + j <= 4]
self.cabs.remove((3, 1))
def _validate(self, seq):
r = 0
for s, s1 in zip(self.seq, seq):
if s == s1:
r += 1
return list(self.cab) == [r, len(set(self.seq) & set(seq)) - r]
def _result(self, seq, cab):
self.seq = seq
self.cab = cab
return filter(self._validate, self.all)
def update(self, seq, cab):
self.all = self._result(seq, cab)
self.analyze()
def analyze(self, show=False):
l = len(self.all)
if l > 24 and show:
if raw_input('-- are you sure? [y/n]: ') != 'y':
show = False
print '-'*12
if l < 9 or show:
print '\n'.join(map(str, self.all))
else:
print l
print '-'*7
out = ['_']*4
for j in xrange(4):
if all(i[j] == self.all[0][j] for i in self.all):
out[j] = str(self.all[0][j])
print ' '.join(out)
print '-'*12
def optimal(self, seq):
all_, cab = max(
((self._result(seq, cab), cab) for cab in self.cabs),
key=lambda x: len(x[0]))
self.all = all_
print '%sC %sB' % cab
def main():
while True:
cb1 = CB()
cb2 = CB()
while True:
r = raw_input()
if r == '0':
cb2.analyze()
break
if r == '-1':
return
if r == 'show':
cb1.analyze(show=True)
continue
l = [map(int, i) for i in r.split('-')]
if len(l) == 2:
cb1.update(*l)
else:
cb2.optimal(*l)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment