2015-01-18

Spiral Matrix

Have you ever tried to assign integer values to a square matrix in spiral order? Eg. for a 5x5 matrix this would look as follows:

1 2 3 4 5
16 17 18 19 6
15 24 25 20 7
14 23 22 21 8
13 12 11 10 9

If this is what you have been lying wake at night for, here comes an iterative solution using Python/Numpy:

#!/usr/bin/env python
""" create a spiral array """

import numpy

def spiral(n):

     mat = numpy.zeros([n,n])

    dirs = [
        numpy.array([0,1]),  #right
        numpy.array([1,0]),  #down
        numpy.array([0,-1]), #left
        numpy.array([-1,0]), #up
    ]

    ptr = numpy.array([0,-1])
    ptr_prime = ptr
    cur_dir_ind = 0
    for i in range(1,n*n+1):
        #adjust direction if necessary
        while(True):
                ptr_prime = ptr + dirs[cur_dir_ind]

                if ptr_prime[0]<0 or ptr_prime[0]>=n or ptr_prime[1]<0 \\ 
                   or ptr_prime[1] >= n:
                        cur_dir_ind = (cur_dir_ind + 1) % len(dirs)
                elif mat[ptr_prime[0],ptr_prime[1]] > 0.0:
                        cur_dir_ind = (cur_dir_ind + 1) % len(dirs)
                else:
                    break

        #update ptr;
        ptr = ptr_prime
        mat[ptr[0],ptr[1]] = i
    
    return mat

2015-01-17

7 Wonders

This post is about the board game 7 Wonders. Sometimes there is a bit of confusion about calculating the victory points from the green cards (scientific structures). Each of the green cards has a symbol (tablet, compass or gear). The player receives 7 victory points for a set of all three symbols and additionally the number of the symbols squared for every symbol. Moreover there is a purple 'joker' card (guild) that can count as any type of green card.

The following few lines of Python can be used to compute the maximum amount of victory points that can be obtained for a given amount of tablet, compass, gear and joker cards. It does this by recursively finding the best allocation for the purple joker cards.

 
#!/usr/bin/env python
""" compute max nr of points for science cards """

#type1 : gear
#type2 : tablet
#type3 : compass

def get_score(type1, type2, type3):

    a = type1**2+type2**2+type3**2
    b = min([type1, type2, type3]) * 7 #sets

   return a+b


def max_points(type1=0, type2=0, type3=0, joker=0):

    if joker==0:
        return get_score(type1, type2, type3)
    else:
        return max([
            max_points(type1+1,type2 , type3 , joker-1),
            max_points(type1 ,type2+1, type3 , joker-1),
            max_points(type1 ,type2 , type3+1, joker-1)
        ])