From 58d57c1c4edda34db3c188fd002eaeb3b36599cc Mon Sep 17 00:00:00 2001 From: Pavel Date: Wed, 6 Nov 2019 23:15:30 +0100 Subject: [PATCH] add tipc19py.py --- python/tipc19py.py | 188 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 188 insertions(+) create mode 100644 python/tipc19py.py diff --git a/python/tipc19py.py b/python/tipc19py.py new file mode 100644 index 0000000..afa9d3f --- /dev/null +++ b/python/tipc19py.py @@ -0,0 +1,188 @@ +from math import * +from random import * +from time import * + +def mmod(a,b): + return a%b +def getplatform(): + return 1 +def getlinechars(o=False): + c,k=2**31-1,getplatform() + if k>=0: + c=[53,o and 99 or 29,o and 509 or 21,31,32,c,c][k] + return c +na,pkl,lnm=21,[],["Bulbizarre","Herbizarre","Florizarre","Salameche","Reptincel","Dracaufeu","Carapuce","Carabaffe","Tortank","Chenipan","Chrysacier","Papilusion","Aspicot","Coconfort","Dardargnan","Roucool","Roucoups","Roucarnage","Rattata","Rattatac","Piafabec","Rapasdepic","Abo","Arbok","Pikachu","Raichu","Sabelette","Sablaireau","Nidoran F","Nidorina","Nidoqueen","Nidoran M","Nidorino","Nidoking","Melofee","Melodelfe","Goupix","Feunard","Rondoudou","Grodoudou","Nosferapti","Nosferalto","Mystherbe","Ortide","Rafflesia","Paras","Parasect","Mimitoss","Aeromite","Taupiqueur","Triopikeur","Miaouss","Persian","Psykokwak","Akwakwak","Ferosinge","Colossinge","Caninos","Arcanin","Ptitard","Tetarte","Tartard","Abra","Kadabra","Alakazam","Machoc","Machopeur","Mackogneur","Chetiflor","Boustiflor","Empiflor","Tentacool","Tentacruel","Racaillou","Gravalanch","Grolem","Ponyta","Galopa","Ramoloss","Flagadoss","Magneti","Magneton","Canarticho","Doduo","Dodrio","Otaria","Lamantine","Tadmorv","Grotadmorv","Kokiyas","Crustabri","Fantominus","Spectrum","Ectoplasma"] +mrandmax,mrand,mfmax,nn,mp=2**31-1,0,93,getlinechars(True)-na,na//2 +def mround(f): + d=mmod(abs(f),1) + return (mfloor(abs(f))+(d>=.5))*(1-2*(f<0)) +def mfloor(f): + return round(f)-(round(f)>f) +def mceil(f): + return round(f)+(round(f)=5] + return pka +def i2c(k): + return chr(k+33) +def c2i(c): + return ord(c)-33 +def clean(): + global pkl + t,s=0,0 + for l in pkl: + t+=l[1] + for l in pkl: + l[2]=f2mf(l[1]/(t or 1)) + s+=l[2] + if(l[2]<=0): + pkl.remove(l) + return clean() + return s +def pk(n,p=1,d=2): + global pkt, pkl + n-=1 + if n>=0 and nnn: + sn=sn[:nn] + print(s+sn) + st=i2c(l[0])+st+i2c(l[2]) + for k in pkt: + if(k): + t+=log(e+k*len(pkl)) + if(d): + if(d>=2): + print("Bon score ? Si oui\nenvoie code suivant\na info@tiplanet.org :") + print(""+st) + return float(t) +def setst(st): + s,pkl[:],n=0,[],len(st)//2 + for k in range(n): + s=pk(c2i(st[n-1-k])+1,c2i(st[n+k+len(st)%2]),False) + return s + +pokemons = [] + +for l in range(94): + mseed(42) + for k in range(l + 1): + mrandom() + pokemons.append(mrandint(1, mrandmax)) + +def fast_score(code): + pkt = [0.0 for l in range(21)] + for k in range(10): + p = code[19 - k] / 93.0 + for l in range(21): + if pokemons[code[k]] & (1 << l): + pkt[l] += p + size = 0 + for k in range(10): + if code[19 - k] > 0: + size += 1 + score = 0.0 + for k in pkt: + if k: + score += log(e + k * size) + return score + +def random_pokemon(code): + n = randint(0, 9) + v = randint(0, 93) + if not v in code[:10]: + code[n] = v + +def random_attack(code): + score_max = fast_score(code) + for k in range(200): + code_next = code.copy() + i = 10 + j = 10 + while i == j: + i = randint(10, 19) + j = randint(10, 19) + d = randint(-93, 93) + vi = code_next[i] + d + vj = code_next[j] - d + if vi >= 0 and vi <= 93 and vj >= 0 and vj <= 93: + code_next[i] = vi + code_next[j] = vj + score = fast_score(code_next) + if score_max < score: + code[:] = code_next + score_max = score + +seed(time()) + +code = [0 for k in range(20)] +code[19] = 93 + +score = fast_score(code) + +code_max = code.copy() +score_max = score + +# find team using simulated annealing +t = 1.0 +while t > 0.001: + t *= 0.999 + code_next = code.copy() + random_pokemon(code_next) + random_attack(code_next) + score_next = fast_score(code_next) + if score_max < score_next: + code_max[:] = code_next + score_max = score_next + print('%.5f' % score_max, ''.join([chr(code_max[k] + 33) for k in range(20)])) + delta = score_next - score; + if delta >= 0 or random() < exp(delta / t): + code[:] = code_next + score = score_next + +# find attack points using brute force +code_max = ''.join([chr(code_max[k] + 33) for k in range(20)]) +for i in range(ord('2'), 256): + for j in range(ord('e'), 256): + code = code_max.replace('2', chr(i)).replace('e', chr(j)) + score = setst(''.join(code)) + if score_max < score: + score_max = score + print('%.5f' % score, code, i, j) +