Friday, October 23, 2015

PuzzlOR: Matchmaker

Matchmaker
In [1]:
import itertools
from time import process_time
In [2]:
F_data=[
    ('Name','Eyes','Hair'),
    ('1','Brown','Yellow'),
    ('2','Green','Red'),
    ('3','Blue','Black'),
    ('4','Brown','Black'),
    ('5','Brown','Red'),
    ('6','Brown','Black')]
M_data=[
    ('Name','Eyes','Hair'),
    ('A','Green','Red'),
    ('B','Blue','Yellow'),
    ('C','Brown','Yellow'),
    ('D','Green','Red'),
    ('E','Green','Black'),
    ('F','Blue','Yellow')]
In [3]:
t=process_time()

GoodMatches=[]
# Consider every possible permutation
for arrangement in itertools.permutations([1,2,3,4,5,6]):
    M_i=1
    matchQuality=[]
    # print("Candidate Match:")
    for F_i in arrangement:
        m=((F_data[F_i][1]==M_data[M_i][1]) or (F_data[F_i][2]==M_data[M_i][2]))
        matchQuality.append(m)
        # print(F_data[F_i][0],M_data[M_i][0],m)
        M_i+=1
        
    if sum(matchQuality)==6:
        GoodMatches.append(arrangement)
      
print("Valid Matches for A, B, C, D, E, F")
for arrangement in GoodMatches:
    print(arrangement)
    
elapsed_time=process_time()-t        
print("\nElapsed Time: ", elapsed_time)
Valid Matches for A, B, C, D, E, F
(2, 1, 4, 5, 6, 3)
(2, 1, 6, 5, 4, 3)
(2, 3, 4, 5, 6, 1)
(2, 3, 6, 5, 4, 1)
(5, 1, 4, 2, 6, 3)
(5, 1, 6, 2, 4, 3)
(5, 3, 4, 2, 6, 1)
(5, 3, 6, 2, 4, 1)

Elapsed Time:  0.011993734000000034