-
Notifications
You must be signed in to change notification settings - Fork 0
/
usingStrongCounterV3.py
261 lines (251 loc) · 11.2 KB
/
usingStrongCounterV3.py
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
import random
from numericalSemigroupLite import *
class strongCounterV3:
def __init__(self):
pass
def coverRelations(self, gap):
if(len(gap) == 0):
return {}
linComb = [i for i in range(0, max(gap) + 1) if i not in gap]
dictionary = {};
for i in range(0, len(gap)):
covers = []
for j in range(i + 1, len(gap)):
for k in range(0, len(linComb)):
if(gap[j] - linComb[k] < gap[i]):
break
if(((gap[j] - linComb[k]) > 0) and ((gap[j] - linComb[k])%gap[i] == 0)):
covers.append(gap[j])
break
dictionary[gap[i]] = covers
return dictionary
def pretendMove(self, gaps, pretend):
covering = self.coverRelations(gaps)
return [i for i in gaps if((i not in covering[pretend]) and (i != pretend))]
def checkWeak(self, gaps):
moveCountermoveDict = {}
alreadyPaired = []
isSingle = []
for a in gaps:
if(a > 1):
moveCountermoveDict[a] = []
nextStage = self.pretendMove(gaps, a)
covers = self.coverRelations(nextStage)
for g in nextStage:
if((g > 1) and ((len(nextStage) - len(covers[g]))%2 == 0)):
if(a > g):
if(a in moveCountermoveDict[g]):
moveCountermoveDict[a].append(g)
if(len(moveCountermoveDict[g]) == 1):
break
else:
moveCountermoveDict[a].append(g)
if(len(moveCountermoveDict[a]) == 0):
isSingle.append(a)
isWeaklyLost = True
for move in moveCountermoveDict:
if (move > 1):
if(len(moveCountermoveDict[move]) == 0):
isWeaklyLost = False
break
return isWeaklyLost, moveCountermoveDict
def checkWeaklyLostPosition(self, gaps):
moveCountermoveDict = {}
alreadyPaired = []
isSingle = []
for a in gaps:
if(a not in alreadyPaired):
moveCountermoveDict[a] = []
nextStage = self.pretendMove(gaps, a)
covers = self.coverRelations(nextStage)
for g in nextStage:
if((g > 1) and ((len(nextStage) - len(covers[g]))%2 == 0) and (g not in alreadyPaired) and (g not in isSingle)):
moveCountermoveDict[a].append(g)
moveCountermoveDict[g] = [a]
alreadyPaired.append(a)
alreadyPaired.append(g)
break
else:
isSingle.append(a)
isWeaklyLost = True
for move in moveCountermoveDict:
if (move > 1):
if(len(moveCountermoveDict[move]) == 0):
isWeaklyLost = False
break
return isWeaklyLost, moveCountermoveDict
def checkWeakCleaned(self, gaps):
moveCountermoveDict = {}
alreadyPaired = []
for a in gaps:
if(a > 1):
moveCountermoveDict[a] = []
nextStage = self.pretendMove(gaps, a)
covers = self.coverRelations(nextStage)
for g in nextStage:
if((g > 1) and ((len(nextStage) - len(covers[g]))%2 == 0)):
if(a > g):
if(a in moveCountermoveDict[g]):
moveCountermoveDict[a].append(g)
if(len(moveCountermoveDict[g]) == 1):
alreadyPaired.append(a)
alreadyPaired.append(g)
break
else:
moveCountermoveDict[a].append(g)
for a in moveCountermoveDict:
if(a in alreadyPaired):
continue
for b in moveCountermoveDict[a]:
if(a not in moveCountermoveDict[b]):
moveCountermoveDict[a].pop(moveCountermoveDict[a].index(b))
isWeaklyLost = True
for move in moveCountermoveDict:
if (move > 1):
if(len(moveCountermoveDict[move]) == 0):
isWeaklyLost = False
break
return isWeaklyLost, moveCountermoveDict
def checkStronglyLostPosition(self, gaps):
strongCountermove = {}
for a in gaps:
if(a > 1):
strongCountermove[a] = []
nextStage = self.pretendMove(gaps, a)
for g in nextStage:
if(g > 1):
secondStage = self.pretendMove(nextStage, g)
weaklyLost = self.checkWeaklyLostPosition(secondStage)
if(weaklyLost[0]):
strongCountermove[a].append(g)
isStrongLost = True
for move in strongCountermove:
if(move > 1):
if(len(strongCountermove[move]) == 0):
isStrongLost = False
break
return isStrongLost, strongCountermove
def findingDaMove(self, gaps):
moves = []
bestMoves = []
for move in gaps:
if(move == 1):
continue
nextStage = self.pretendMove(gaps, move)
if(self.checkWeak(nextStage)[0]):
moves.append(move)
for potMove in moves:
isBestMove = True
nextStage = self.pretendMove(gaps, potMove)
for nextMove in nextStage:
if(nextMove == 1):
continue
secondStage = self.pretendMove(nextStage, nextMove)
if(self.checkWeaklyLostPosition(secondStage)[0]):
isBestMove = False
break
if(isBestMove):
bestMoves.append(potMove)
for best in bestMoves:
nextStage = self.pretendMove(gaps, best)
if(self.checkWeakCleaned(nextStage)[0]):
if(self.checkStronglyLostPosition(nextStage)[0]):
return (best, False)
if(len(bestMoves) > 0):
return (min(bestMoves), False)
return (max(gaps), True)
def findingGoodMove(self, gaps):
possibleMoves = []
for a in gaps:
if(a < 4):
continue
nextStage = self.pretendMove(gaps, a)
if(self.checkWeaklyLostPosition(nextStage)[0]):
possibleMoves.append(a)
if(len(possibleMoves)>0):
return random.choice(possibleMoves)
return max(gaps)
def nextMove(self, movesPlayed, remainingGaps = []):
movesPlayed = [int(i) for i in movesPlayed]
remainingMoves = [i for i in remainingGaps]
if((len(remainingMoves) < 75) and (len(remainingMoves) >= 30) and (len(movesPlayed) != 0)):
covers = self.coverRelations(remainingMoves)
reasonAbleMoves = [i for i in remainingMoves if((i > 3) and ((len(remainingMoves) - len(covers[i]))%2 == 0))]
tries = 0
alreadyTried = []
while(tries < min(len(reasonAbleMoves), 10)):
randomMove = random.choice(reasonAbleMoves)
if(randomMove in alreadyTried):
continue
else:
alreadyTried.append(randomMove)
covers = self.coverRelations(remainingGaps)
nextStage = self.pretendMove(remainingMoves, randomMove)
if(len(nextStage) < 30):
possibleBest = self.findingDaMove(nextStage)
if(possibleBest[0] == max(nextStage) and possibleBest[1]):
return randomMove
else:
if(self.checkWeaklyLostPosition(nextStage)[0]):
return randomMove
tries += 1
return random.choice(reasonAbleMoves)
elif((len(remainingMoves) < 200) and (len(remainingMoves) >= 75) and (len(movesPlayed) != 0)):
covers = self.coverRelations(remainingMoves)
reasonAbleMoves = [i for i in remainingMoves if((i > 3) and ((len(remainingMoves) - len(covers[i]))%2 == 0))]
return random.choice(reasonAbleMoves)
elif((len(remainingMoves) < 30) and (len(remainingMoves) > 0) and (len(movesPlayed) != 0)):
return self.findingDaMove(remainingMoves)[0]
#NORMAL STUFF
elif((len(remainingMoves)>0) and (len(movesPlayed) != 0)):
if((len(remainingMoves)%2) == 0):
return max(remainingMoves)
else:
linearCombos = [i for i in range(1, max(remainingMoves)) if i not in remainingMoves]
j = len(remainingMoves) - 1
while(j > 2):
for l in linearCombos:
if (((remainingMoves[j] - l) in remainingMoves) and ((remainingMoves[j] - l) > 3)):
return (remainingMoves[j] - l)
else:
continue
j -= 1
return max(remainingMoves)
elif(len(movesPlayed) == 0):
return random.choice([5, 7, 11, 13, 17, 19, 23, 29, 31, 37])
elif(len(movesPlayed) == 1):
factors = PrimeFactorization(movesPlayed[0])
if (len(factors) == 1):
possible = [i for i in range(movesPlayed[0] + 1, max(100, movesPlayed[0] + 1)) if((i%movesPlayed[0]) != 0)]
index = random.randint(0, len(possible) - 1)
return possible[index]
elif(max(factors) > 3):
return max(factors)
else:
newMove = 1
for a in factors:
newMove = newMove*a
return int(((newMove/min(factors)) + 1)*min(factors))
else:
gcd_moves = gcd_list(movesPlayed)
newSet = [int(i/gcd_moves) for i in movesPlayed]
if ((1 in newSet) and (gcd_moves > 1)):
return (gcd_moves*2 + 1)
S = NumericalSemigroup(newSet)
remainingMoves = S.gaps
if((len(remainingMoves) <= 1) and gcd_moves > 1):
return (gcd_moves*2 + 1)
else:
if((len(remainingMoves)%2) == 0):
return (max(remainingMoves)*gcd_moves)
else:
linearCombos = [i for i in range(1, max(remainingMoves)) if i not in remainingMoves]
j = len(remainingMoves) - 1
while(j > 2):
for l in linearCombos:
if (((remainingMoves[j] - l) in remainingMoves) and ((remainingMoves[j] - l) > 3)):
return ((remainingMoves[j] - l)*gcd_moves)
else:
continue
j -= 1
return (max(remainingMoves)*gcd_moves)