Sunday, October 1, 2017

Waffle Stacking

Waffle Stacking

HP CodeWars 2013, Problem 20: Waffle Stacking

http://hpcodewars.org/past/cw16/problems/Prob20--WaffleStacking.pdf

In [1]:
import itertools

# hardcoded test data set!
vtop = (2,2,3,2,1)
vrow0 = (4,1)
vrow1 = (1,4)
vrow2 = (3,2)
vrow3 = (2,2)
vrow4 = (3,2)
vbottom = (3,2,1,3,4)
In [2]:
vcol0 = (vtop[0],vbottom[0])
vcol1 = (vtop[1],vbottom[1])
vcol2 = (vtop[2],vbottom[2])
vcol3 = (vtop[3],vbottom[3])
vcol4 = (vtop[4],vbottom[4])
In [3]:
def count_visible(c):
    vis = 1
    rvis = 1
    rc = c[::-1]
    for i in range(1,5):
        if (c[i]==max(c[:i+1])):
            vis += 1
        if (rc[i]==max(rc[:i+1])):
            rvis += 1
            
    return(vis,rvis)
In [4]:
waffle_lookup = {}

for config in itertools.permutations([1,2,3,4,5]):
    waffle_lookup[config]=count_visible(config)
    
In [5]:
for config in itertools.product([k for k in waffle_lookup if waffle_lookup[k] == vrow0],
                                [k for k in waffle_lookup if waffle_lookup[k] == vrow1],
                                [k for k in waffle_lookup if waffle_lookup[k] == vrow2],
                                [k for k in waffle_lookup if waffle_lookup[k] == vrow3],
                                [k for k in waffle_lookup if waffle_lookup[k] == vrow4]):

    col0 = (config[0][0],config[1][0],config[2][0],config[3][0],config[4][0])
    if len(set(col0))<5:
        continue
        
    col1 = (config[0][1],config[1][1],config[2][1],config[3][1],config[4][1])
    if len(set(col1))<5:
        continue
        
    col2 = (config[0][2],config[1][2],config[2][2],config[3][2],config[4][2])
    if len(set(col2))<5:
        continue
        
    col3 = (config[0][3],config[1][3],config[2][3],config[3][3],config[4][3])
    if len(set(col3))<5:
        continue
        
    col4 = (config[0][4],config[1][4],config[2][4],config[3][4],config[4][4])
    if len(set(col4))<5:
        continue
        
    if count_visible(col0)==vcol0 and count_visible(col1)==vcol1 and count_visible(col2)==vcol2 and \
        count_visible(col3)==vcol3 and count_visible(col4)==vcol4:    
        for i in range(5):
            a,b,c,d,e=config[i]
            print(a,b,c,d,e)
1 3 2 4 5
5 2 4 3 1
2 1 3 5 4
4 5 1 2 3
3 4 5 1 2