[ create a new paste ] login | about

Link: http://codepad.org/hU00IsXU    [ raw code | output | fork ]

k4st - Python, pasted on Nov 7:
NO_PLAYER = 0
PLAYER_1 = 1
PLAYER_2 = 2

def search(seq):
    
    num_since_start = 0 # number of same-color chips + empties in the sequence
    num_in_line = 0 # number of same-color chips in the sequence
    num_consecutive_empty = 0 # number of consecutive empty cells
    i = 0
    
    for player_id in seq:
        
        # have we found 4 chips such that we can make a win?
        if num_since_start >= 5:
            if num_in_line >= 4 and num_in_line >= (num_since_start - 2):
                yield (i-5, i)

            elif num_in_line <= -4 and (num_since_start + num_in_line) <= 2:
                yield (i-5, i)
        
        # have we found 3 chips in a row that might be used to construct a 
        # double threat?
        if num_since_start > 4 and num_consecutive_empty == 1:
            if num_in_line == 3:
                yield (i-num_since_start, i)
            elif num_in_line == -3:
                yield (i-num_since_start, i)
        
        if player_id is NO_PLAYER:
        	num_since_start += 1
        	num_consecutive_empty += 1
	
        	if num_consecutive_empty > 1:
        		num_in_line = 0
        		num_since_start = 1 # reset
    		elif num_since_start > 6 and num_in_line < 4:
    		    num_since_start = 1 # reset
	
        elif player_id is PLAYER_1:
        	if num_in_line < 0:
        		num_in_line = 1
        		if num_consecutive_empty >= 1:
        		    num_since_start = 2 # reset
        		else:
        		    num_since_start = 1
        	else:
        		num_in_line += 1
        		num_since_start += 1
        		
        	num_consecutive_empty = 0
	
        else:
        	if num_in_line > 0:
        		num_in_line = -1
        		if num_consecutive_empty >= 1:
        		    num_since_start = 2 # reset
        		else:
        		    num_since_start = 1
        	else:
        		num_in_line -= 1
        		num_since_start += 1
        		
        	num_consecutive_empty = 0
        
        i += 1
        print "id:", player_id, "since start:", num_since_start, "in line:", num_in_line, "consecutive empty:", num_consecutive_empty
    
    # have we found 4 chips such that we can make a win?
    if num_since_start >= 5 and (num_since_start - num_in_line) > 0:
        if num_in_line >= 4 and num_in_line >= (num_since_start - 2):
            yield (i-5, i)

        elif num_in_line <= -4 and (num_since_start + num_in_line) <= 2:
            yield (i-5, i)

    # have we found 3 chips in a row that might be used to construct a 
    # double threat?
    if num_since_start > 4 and num_consecutive_empty == 1:
        if num_in_line == 3:
            yield (i-num_since_start, i)
        elif num_in_line == -3:
            yield (i-num_since_start, i)

    raise StopIteration()

def do(func, ls):
    for (start, end) in func(ls):
        print ls[start:end]
    print

do(search, (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,))
do(search, (0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0,))
do(search, (0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,))
do(search, (0, 1, 2, 2, 2, 2, 0, 0, 0, 0, 0,))
do(search, (0, 1, 2, 1, 1, 1, 1, 0, 0, 0, 0,))
do(search, (0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1,))
do(search, (0, 0, 0, 1, 0, 1, 1, 1, 2, 0, 0,))
do(search, (0, 0, 2, 1, 1, 0, 1, 1, 2, 0, 0,))
do(search, (1, 1, 1, 1, 2, 2, 2, 2, 0, 0, 0,))
do(search, (0, 1, 1, 1, 1, 2, 2, 2, 2, 0, 0,))
do(search, (0, 1, 1, 1, 1, 0, 2, 2, 2, 0, 0,))
do(search, (0, 1, 1, 1, 1, 0, 1, 2, 2, 0, 0,))
do(search, (0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 0,))
do(search, (0, 1, 1, 1, 0, 0, 2, 2, 0, 2, 0,))
do(search, (0, 1, 1, 1, 0, 0, 0, 2, 2, 0, 2,))
do(search, (2, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 2,))
do(search, (0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1,))
do(search, (2, 1, 1, 1, 0, 0, 0,))


Output:
id: 0 since start: 1 in line: 0 consecutive empty: 1
id: 0 since start: 1 in line: 0 consecutive empty: 2
id: 0 since start: 1 in line: 0 consecutive empty: 3
id: 0 since start: 1 in line: 0 consecutive empty: 4
id: 0 since start: 1 in line: 0 consecutive empty: 5
id: 0 since start: 1 in line: 0 consecutive empty: 6
id: 0 since start: 1 in line: 0 consecutive empty: 7
id: 0 since start: 1 in line: 0 consecutive empty: 8
id: 0 since start: 1 in line: 0 consecutive empty: 9
id: 0 since start: 1 in line: 0 consecutive empty: 10
id: 0 since start: 1 in line: 0 consecutive empty: 11

id: 0 since start: 1 in line: 0 consecutive empty: 1
id: 1 since start: 2 in line: 1 consecutive empty: 0
id: 0 since start: 3 in line: 1 consecutive empty: 1
id: 1 since start: 4 in line: 2 consecutive empty: 0
id: 1 since start: 5 in line: 3 consecutive empty: 0
id: 1 since start: 6 in line: 4 consecutive empty: 0
(1, 0, 1, 1, 1)
id: 0 since start: 7 in line: 4 consecutive empty: 1
id: 0 since start: 1 in line: 0 consecutive empty: 2
id: 0 since start: 1 in line: 0 consecutive empty: 3
id: 0 since start: 1 in line: 0 consecutive empty: 4
id: 0 since start: 1 in line: 0 consecutive empty: 5

id: 0 since start: 1 in line: 0 consecutive empty: 1
id: 1 since start: 2 in line: 1 consecutive empty: 0
id: 0 since start: 3 in line: 1 consecutive empty: 1
id: 1 since start: 4 in line: 2 consecutive empty: 0
id: 0 since start: 5 in line: 2 consecutive empty: 1
id: 1 since start: 6 in line: 3 consecutive empty: 0
id: 0 since start: 1 in line: 3 consecutive empty: 1
id: 1 since start: 2 in line: 4 consecutive empty: 0
id: 0 since start: 3 in line: 4 consecutive empty: 1
id: 1 since start: 4 in line: 5 consecutive empty: 0
id: 0 since start: 5 in line: 5 consecutive empty: 1

id: 0 since start: 1 in line: 0 consecutive empty: 1
id: 1 since start: 2 in line: 1 consecutive empty: 0
id: 2 since start: 1 in line: -1 consecutive empty: 0
id: 2 since start: 2 in line: -2 consecutive empty: 0
id: 2 since start: 3 in line: -3 consecutive empty: 0
id: 2 since start: 4 in line: -4 consecutive empty: 0
id: 0 since start: 5 in line: -4 consecutive empty: 1
(2, 2, 2, 2, 0)
id: 0 since start: 1 in line: 0 consecutive empty: 2
id: 0 since start: 1 in line: 0 consecutive empty: 3
id: 0 since start: 1 in line: 0 consecutive empty: 4
id: 0 since start: 1 in line: 0 consecutive empty: 5

id: 0 since start: 1 in line: 0 consecutive empty: 1
id: 1 since start: 2 in line: 1 consecutive empty: 0
id: 2 since start: 1 in line: -1 consecutive empty: 0
id: 1 since start: 1 in line: 1 consecutive empty: 0
id: 1 since start: 2 in line: 2 consecutive empty: 0
id: 1 since start: 3 in line: 3 consecutive empty: 0
id: 1 since start: 4 in line: 4 consecutive empty: 0
id: 0 since start: 5 in line: 4 consecutive empty: 1
(1, 1, 1, 1, 0)
id: 0 since start: 1 in line: 0 consecutive empty: 2
id: 0 since start: 1 in line: 0 consecutive empty: 3
id: 0 since start: 1 in line: 0 consecutive empty: 4

id: 0 since start: 1 in line: 0 consecutive empty: 1
id: 1 since start: 2 in line: 1 consecutive empty: 0
id: 2 since start: 1 in line: -1 consecutive empty: 0
id: 0 since start: 2 in line: -1 consecutive empty: 1
id: 1 since start: 2 in line: 1 consecutive empty: 0
id: 2 since start: 1 in line: -1 consecutive empty: 0
id: 0 since start: 2 in line: -1 consecutive empty: 1
id: 1 since start: 2 in line: 1 consecutive empty: 0
id: 2 since start: 1 in line: -1 consecutive empty: 0
id: 0 since start: 2 in line: -1 consecutive empty: 1
id: 1 since start: 2 in line: 1 consecutive empty: 0

id: 0 since start: 1 in line: 0 consecutive empty: 1
id: 0 since start: 1 in line: 0 consecutive empty: 2
id: 0 since start: 1 in line: 0 consecutive empty: 3
id: 1 since start: 2 in line: 1 consecutive empty: 0
id: 0 since start: 3 in line: 1 consecutive empty: 1
id: 1 since start: 4 in line: 2 consecutive empty: 0
id: 1 since start: 5 in line: 3 consecutive empty: 0
id: 1 since start: 6 in line: 4 consecutive empty: 0
(1, 0, 1, 1, 1)
id: 2 since start: 1 in line: -1 consecutive empty: 0
id: 0 since start: 2 in line: -1 consecutive empty: 1
id: 0 since start: 1 in line: 0 consecutive empty: 2

id: 0 since start: 1 in line: 0 consecutive empty: 1
id: 0 since start: 1 in line: 0 consecutive empty: 2
id: 2 since start: 2 in line: -1 consecutive empty: 0
id: 1 since start: 1 in line: 1 consecutive empty: 0
id: 1 since start: 2 in line: 2 consecutive empty: 0
id: 0 since start: 3 in line: 2 consecutive empty: 1
id: 1 since start: 4 in line: 3 consecutive empty: 0
id: 1 since start: 5 in line: 4 consecutive empty: 0
(1, 1, 0, 1, 1)
id: 2 since start: 1 in line: -1 consecutive empty: 0
id: 0 since start: 2 in line: -1 consecutive empty: 1
id: 0 since start: 1 in line: 0 consecutive empty: 2

id: 1 since start: 1 in line: 1 consecutive empty: 0
id: 1 since start: 2 in line: 2 consecutive empty: 0
id: 1 since start: 3 in line: 3 consecutive empty: 0
id: 1 since start: 4 in line: 4 consecutive empty: 0
id: 2 since start: 1 in line: -1 consecutive empty: 0
id: 2 since start: 2 in line: -2 consecutive empty: 0
id: 2 since start: 3 in line: -3 consecutive empty: 0
id: 2 since start: 4 in line: -4 consecutive empty: 0
id: 0 since start: 5 in line: -4 consecutive empty: 1
(2, 2, 2, 2, 0)
id: 0 since start: 1 in line: 0 consecutive empty: 2
id: 0 since start: 1 in line: 0 consecutive empty: 3

id: 0 since start: 1 in line: 0 consecutive empty: 1
id: 1 since start: 2 in line: 1 consecutive empty: 0
id: 1 since start: 3 in line: 2 consecutive empty: 0
id: 1 since start: 4 in line: 3 consecutive empty: 0
id: 1 since start: 5 in line: 4 consecutive empty: 0
(0, 1, 1, 1, 1)
id: 2 since start: 1 in line: -1 consecutive empty: 0
id: 2 since start: 2 in line: -2 consecutive empty: 0
id: 2 since start: 3 in line: -3 consecutive empty: 0
id: 2 since start: 4 in line: -4 consecutive empty: 0
id: 0 since start: 5 in line: -4 consecutive empty: 1
(2, 2, 2, 2, 0)
id: 0 since start: 1 in line: 0 consecutive empty: 2

id: 0 since start: 1 in line: 0 consecutive empty: 1
id: 1 since start: 2 in line: 1 consecutive empty: 0
id: 1 since start: 3 in line: 2 consecutive empty: 0
id: 1 since start: 4 in line: 3 consecutive empty: 0
id: 1 since start: 5 in line: 4 consecutive empty: 0
(0, 1, 1, 1, 1)
id: 0 since start: 6 in line: 4 consecutive empty: 1
(1, 1, 1, 1, 0)
id: 2 since start: 2 in line: -1 consecutive empty: 0
id: 2 since start: 3 in line: -2 consecutive empty: 0
id: 2 since start: 4 in line: -3 consecutive empty: 0
id: 0 since start: 5 in line: -3 consecutive empty: 1
(0, 2, 2, 2, 0)
id: 0 since start: 1 in line: 0 consecutive empty: 2

id: 0 since start: 1 in line: 0 consecutive empty: 1
id: 1 since start: 2 in line: 1 consecutive empty: 0
id: 1 since start: 3 in line: 2 consecutive empty: 0
id: 1 since start: 4 in line: 3 consecutive empty: 0
id: 1 since start: 5 in line: 4 consecutive empty: 0
(0, 1, 1, 1, 1)
id: 0 since start: 6 in line: 4 consecutive empty: 1
(1, 1, 1, 1, 0)
id: 1 since start: 7 in line: 5 consecutive empty: 0
(1, 1, 1, 0, 1)
id: 2 since start: 1 in line: -1 consecutive empty: 0
id: 2 since start: 2 in line: -2 consecutive empty: 0
id: 0 since start: 3 in line: -2 consecutive empty: 1
id: 0 since start: 1 in line: 0 consecutive empty: 2

id: 0 since start: 1 in line: 0 consecutive empty: 1
id: 1 since start: 2 in line: 1 consecutive empty: 0
id: 1 since start: 3 in line: 2 consecutive empty: 0
id: 1 since start: 4 in line: 3 consecutive empty: 0
id: 0 since start: 5 in line: 3 consecutive empty: 1
(0, 1, 1, 1, 0)
id: 0 since start: 1 in line: 0 consecutive empty: 2
id: 1 since start: 2 in line: 1 consecutive empty: 0
id: 1 since start: 3 in line: 2 consecutive empty: 0
id: 0 since start: 4 in line: 2 consecutive empty: 1
id: 1 since start: 5 in line: 3 consecutive empty: 0
id: 0 since start: 6 in line: 3 consecutive empty: 1
(0, 1, 1, 0, 1, 0)

id: 0 since start: 1 in line: 0 consecutive empty: 1
id: 1 since start: 2 in line: 1 consecutive empty: 0
id: 1 since start: 3 in line: 2 consecutive empty: 0
id: 1 since start: 4 in line: 3 consecutive empty: 0
id: 0 since start: 5 in line: 3 consecutive empty: 1
(0, 1, 1, 1, 0)
id: 0 since start: 1 in line: 0 consecutive empty: 2
id: 2 since start: 2 in line: -1 consecutive empty: 0
id: 2 since start: 3 in line: -2 consecutive empty: 0
id: 0 since start: 4 in line: -2 consecutive empty: 1
id: 2 since start: 5 in line: -3 consecutive empty: 0
id: 0 since start: 6 in line: -3 consecutive empty: 1
(0, 2, 2, 0, 2, 0)

id: 0 since start: 1 in line: 0 consecutive empty: 1
id: 1 since start: 2 in line: 1 consecutive empty: 0
id: 1 since start: 3 in line: 2 consecutive empty: 0
id: 1 since start: 4 in line: 3 consecutive empty: 0
id: 0 since start: 5 in line: 3 consecutive empty: 1
(0, 1, 1, 1, 0)
id: 0 since start: 1 in line: 0 consecutive empty: 2
id: 0 since start: 1 in line: 0 consecutive empty: 3
id: 2 since start: 2 in line: -1 consecutive empty: 0
id: 2 since start: 3 in line: -2 consecutive empty: 0
id: 0 since start: 4 in line: -2 consecutive empty: 1
id: 2 since start: 5 in line: -3 consecutive empty: 0

id: 2 since start: 1 in line: -1 consecutive empty: 0
id: 0 since start: 2 in line: -1 consecutive empty: 1
id: 1 since start: 2 in line: 1 consecutive empty: 0
id: 1 since start: 3 in line: 2 consecutive empty: 0
id: 1 since start: 4 in line: 3 consecutive empty: 0
id: 0 since start: 5 in line: 3 consecutive empty: 1
(0, 1, 1, 1, 0)
id: 0 since start: 1 in line: 0 consecutive empty: 2
id: 0 since start: 1 in line: 0 consecutive empty: 3
id: 1 since start: 2 in line: 1 consecutive empty: 0
id: 1 since start: 3 in line: 2 consecutive empty: 0
id: 1 since start: 4 in line: 3 consecutive empty: 0
id: 0 since start: 5 in line: 3 consecutive empty: 1
(0, 1, 1, 1, 0)
id: 2 since start: 2 in line: -1 consecutive empty: 0

id: 0 since start: 1 in line: 0 consecutive empty: 1
id: 0 since start: 1 in line: 0 consecutive empty: 2
id: 0 since start: 1 in line: 0 consecutive empty: 3
id: 0 since start: 1 in line: 0 consecutive empty: 4
id: 0 since start: 1 in line: 0 consecutive empty: 5
id: 0 since start: 1 in line: 0 consecutive empty: 6
id: 0 since start: 1 in line: 0 consecutive empty: 7
id: 1 since start: 2 in line: 1 consecutive empty: 0
id: 1 since start: 3 in line: 2 consecutive empty: 0
id: 1 since start: 4 in line: 3 consecutive empty: 0
id: 1 since start: 5 in line: 4 consecutive empty: 0
(0, 1, 1, 1, 1)

id: 2 since start: 1 in line: -1 consecutive empty: 0
id: 1 since start: 1 in line: 1 consecutive empty: 0
id: 1 since start: 2 in line: 2 consecutive empty: 0
id: 1 since start: 3 in line: 3 consecutive empty: 0
id: 0 since start: 4 in line: 3 consecutive empty: 1
id: 0 since start: 1 in line: 0 consecutive empty: 2
id: 0 since start: 1 in line: 0 consecutive empty: 3



Create a new paste based on this one


Comments: