all stats

jetison333's stats

guessed the most

namecorrect guessesgames togetherratio

were guessed the most by

namecorrect guessesgames togetherratio

entries

round #45

submitted at
0 likes

guesses
comments 0

post a comment


connectFour.py ASCII text, with CRLF line terminators
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
def stripList(lst):
    return [x for x in lst if not x is None]

def endState():
    for x, col in enumerate(board):
        for y, space in enumerate(col):
            if space != 0:
                if (x + 3 < len(board) and space == board[x+1][y] == board[x+2][y] == board[x+3][y]):
                    return space
                if (y + 3 < len(board[0]) and space == board[x][y+1] == board[x][y+2] == board[x][y+3]):
                    return space
                if (x + 3 < len(board) and y + 3 < len(board[0]) and space == board[x+1][y+1] == board[x+2][y+2] == board[x+3][y+3]):
                    return space
                if (x + 3 < len(board) and y - 3 >= 0 and space == board[x+1][y-1] == board[x+2][y-2] == board[x+3][y-3]):
                    return space
    return 0

def updateBoard(move, player):
    moveStack.append(move)
    if player == max:
        board[move][board[move].index(0)] = 1
    else:
        board[move][board[move].index(0)] = -1

def undo():
    move = moveStack.pop()
    if 0 in board[move]:
        board[move][board[move].index(0) - 1] = 0
    else:
        board[move][-1] = 0

def check(arr):
    return 0 in arr and ((1 in arr) ^ (-1 in arr))

def evaluate4(arr):
    if 1 in arr:
        return 10 ** (arr.count(1) - 1)
    return -(10 ** (arr.count(-1) - 1))

def evaluateBoard():
    if (winner := endState()) != 0:
        return winner * 100000
    total = 0
    for x, col in enumerate(board):
        for y, space in enumerate(col):
            if (x + 3 < len(board)):
                right = [space, board[x+1][y], board[x+2][y], board[x+3][y]]
                if check(right):
                    total += evaluate4(right)
            if (y + 3 < len(board[0])):
                up = [space, board[x][y+1], board[x][y+2], board[x][y+3]]
                if check(up):
                    total += evaluate4(up)
            if (x + 3 < len(board) and y + 3 < len(board[0])):
                upright = [space, board[x+1][y+1], board[x+2][y+2], board[x+3][y+3]]
                if check(upright):
                    total += evaluate4(upright)
            if (x + 3 < len(board) and y - 3 >= 0):
                downright = [space, board[x+1][y-1], board[x+2][y-2], board[x+3][y-3]]
                if check(downright):
                    total += evaluate4(downright)
    return total
        
def evaluateMove(move, player, depth = 0):
    global board
    if not 0 in board[move]:
        return None
    updateBoard(move, min if player == max else max)
    if (winner := endState()) != 0:
        returnVal = winner * 100000
    elif depth > 4: #tune this if it takes to long/short
        returnVal = evaluateBoard()
    else:
        moves = [evaluateMove(x, min if player == max else max, depth+1) for x in range(7)]
        if all(move is None for move in moves):
            returnVal = None
        else:
            returnVal = player(stripList(moves))
    undo()
    return returnVal

def move(player):
    moves = [evaluateMove(x, min if player == max else max) for x in range(7)]
    choosenMove = moves.index(player(stripList(moves)))
    updateBoard(choosenMove, player)
    return choosenMove

board = [[0 for _ in range(6)] for _ in range(7)]
moveStack = []

side = input()
player = max
if side == "f":
    player = min
    print(move(player) + 1)
while endState() == 0:
    updateBoard(int(input()) - 1, min if player == max else max)
    print(move(player) + 1)

round #6

guesses
comments 0

post a comment


fibadd.py ASCII text
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
def fib(n):
    a = 0
    b = 1
    if n < 0:
        return -fib(-n)
    elif n == 0:
        return 0
    elif n == 1:
        return 1
    else:
        for i in range(1, n):
            a, b = b, a + b
        return b

def closest_pos(i):
    upper = 1
    while fib(upper) <= i:
        upper = upper * 2
    lower = int(upper/2)
    while upper-lower != 1:
        mid = int((lower+upper)/2)
        if fib(mid) <= i:
            lower = mid
        else:
            upper = mid
    return (lower, upper)

dic = {}

def fib_add(i):
    if not i in dic:
        dic[i] = fib_add_(i)
    return dic[i]
    
def fib_add_(i):
    if i == 1:
        return [1]
    if i == 0:
        return []
    smaller, larger = closest_pos(i)
    smaller = [smaller] + fib_add(i - fib(smaller))
    larger = [larger] + [-x for x in fib_add(-(i - fib(larger)))]
    if len(smaller) < len(larger):
        return smaller
    else:
        return larger