atlemu.py

Created by andreanx

Created on October 14, 2019

2.48 KB

Script for the TI-Planet & Planète Casio back to school 2019 contest. Check https://tiplanet.org/forum/viewtopic.php?p=246627#p246627


from math import log10,sqrt
from kandinsky import color,fill_rect,draw_string
def init():
  global atmu,n,j,s,ma,mb,mc,w,fp,fm
  atmu,n,j,s,w,fp,fm=[],9,0,0,5,0,0
  ma,mb,mc=[[0]*n for k in range(n)],[[0]*n for k in range(n)],[[0]*n for k in range(n)]
def dr():
  global s,j,w
  sw=320
  d,h=min(sw//n,221//n),(color(0,0,0),color(255,255,255))
  b=sw-1-n*d
  for i in range(0,n*d+1,d):
    fill_rect(b,i,n*d,1,h[0])
    fill_rect(b+i,0,1,n*d,h[0])
  for y in range(0,n):
    for x in range(0,n):
      t=255-(255*abs(ma[y][x])//w)
      fill_rect(b+x*d+1,y*d+1,d-1,d-1,ma[y][x]<0 and (t,t,255) or ma[y][x]>0 and (255,t,t) or h[1])
  draw_string(" ATLEMU ",1,1,color(255,255,0),color(127,127,127))
  draw_string("Muen\ndo(1,c,l)",0,19,color(0,0,255),h[1])
  draw_string("Atlante\ndo(-1,c,l)",0,53,color(255,0,0),h[1])
  draw_string("Passer\ndo(0)",0,87,color(255,0,255),h[1])
  draw_string("An: "+str(j)+"\nScore:\n"+str(s)[:10],0,n*d-49)
def do(*ar):
  global j,gp,gm,fp,fm,s
  j,k,v,(sc,sl)=j+1,ar[0],(len(ar)%2) or ar[len(ar)-1],(len(ar)>=3) and (ar[1],ar[2]) or (0,0)
  k,gp,gm=k and k//abs(k),fp,fm
  for y in range(n):mb[y]=mc[y].copy()
  for y in range(n):
    for x in range(n):
      o,ma[y][x]=ma[y][x],ma[y][x]+w*(ma[y][x]==0 and x==sc and y==sl)*((k>0)-(k<0))+(ma[y][x]<=0 and (x-sc or y-sl or k==0) and mb[y][x]//10==3)*((ma[y][x]==0)*w-ma[y][x]*(not(not(ma[y][x]))))-(ma[y][x]>=0 and (x-sc or y-sl or k==0) and mb[y][x]-10*(mb[y][x]//10)==3)*((ma[y][x]==0)*w+ma[y][x]*(not(not(ma[y][x]))))
      if o and ma[y][x]==o:
        ls=(-1,w>abs(ma[y][x]))
        ma[y][x]=ma[y][x]+(ma[y][x]>0)*ls[mb[y][x]//10==3 or mb[y][x]//10==4]-(ma[y][x]<0)*ls[mb[y][x]-10*(mb[y][x]//10)==3 or mb[y][x]-10*(mb[y][x]//10)==2]
      if ma[y][x]-o:
        fp,fm=fp+(ma[y][x]>0 and not(o))-(o>0 and not(ma[y][x])),fm+(ma[y][x]<0 and not(o))-(o<0 and not(ma[y][x]))
        if not(o) and ma[y][x] or o and not(ma[y][x]):
          for dl in range(-1,2):
            for dc in range(-1,2):
              if dl or dc:mc[y+dl+(dl<0 and y==0)*n-(dl>0 and y==n-1)*n][x+dc+(dc<0 and x==0)*n-(dc>0 and x==n-1)*n]+=(ma[y][x]<0)-(o<0 and 0==ma[y][x])+10*((ma[y][x]>0)-(o>0 and 0==ma[y][x]))
  if max(fp,gp)*max(fm,gm):s=s/(1+abs(k)*log10(sqrt(j)))+fp*fm*min(fp,gp)*min(fm,gm)/max(fp,gp)/max(fm,gm)
  atmu.append((sc*k+k,sl*k+k))
  if v:
    dr()
    print("Bon score ? Envoie la liste\n'atmu' a info@tiplanet.org")
  return s
def st(l,v=True):
  init()
  for p in l:s=do((p[0]>0)-(p[0]<0),abs(p[0])-1,abs(p[1])-1,v)
  dr()
  return s
init()
dr()