• # python, vite fait bien fait

    Posté par . En réponse au message Avent du Code, jour 14. Évalué à 4.

    Le parsing de l'input est presque plus long à écrire que l'application de règles : 4 boucles for imbriquées et 4 split contre 3 boucles while imbriquées.

    Voici mon code pour la partie deux. La partie est identique excepté la condition de sortie.

    import sys
    W = 1000
    H = 200
    L = [[0 for _ in range(W)] for _ in range(H)] # full or air
    def s2li(s,sep=','):
     return list(map(int,s.split(sep)))
    MAX_Y = 0
    for l in sys.stdin.read().splitlines():
     lp = l.split(" -> ")
     for f,t in zip(lp,lp[1:]):
     (a,b,c,d)= (*s2li(f),*s2li(t))
     x0 = min(a,c)
     y0 = min(b,d)
     x1 = max(a,c)
     y1 = max(b,d)
     MAX_Y = max(MAX_Y, y1)
     for x in range(x0,x1+1):
     for y in range(y0,y1+1):
     L[y][x] = 1
    for x in range(W):
     L[MAX_Y+2][x] = 1 # rock on the ground
    stop = False
    N = 0
    while not stop:
     N += 1
     y = 0
     x = 500
     more = True # can all left or right 
     while more and not stop:
     more = False
     while L[y][x]==0 : #and not stop: # if only air
     y+=1
     stop = y == MAX_Y+2
     if L[y][x-1]==0: # can fall left
     x -= 1
     more = True
     elif L[y][x+1]==0: # can fall right
     x += 1
     more = True
     else:
     stop = y == 1 # can't put more sand
     L[y-1][x] = 2 # mark sand
    print(N)

    Et une jolie nimage de la caverne remplie de sable: