• [^] # Re: python procédural, moche mais efficace

    Posté par (Mastodon) . En réponse au message Avent du Code, jour 8. Évalué à 3.

    Mon premier parcours est moins bourrin, je ne cherche pas à savoir pour chaque arbre s'il est visible, mais bien à noter les arbres vus depuis chaque angle de vue, donc au lieu d'avoir size=width*height traitements, j'en ai 2*(width+height).

    def check(rows, cols):
     h = -1
     for r in rows:
     for c in cols:
     if input[r][c] > h:
     visible[r * width + c] = True
     h = input[r][c]
    visible = [False for _ in range(size)]
    for r in range(height):
     check([r], range(width))
     check([r], range(width-1, -1, -1))
    for c in range(width):
     check(range(height), [c])
     check(range(height-1, -1, -1), [c])
    print(f"Visible Trees : {sum(visible)}")

    Pour la seconde partie c'est algorithmiquement équivalent :

    def look(h, rows, cols):
     n = 0
     for r in rows:
     for c in cols:
     n += 1
     if input[r][c] >= h:
     return n
     return n
    scenic = [0 for _ in range(size)]
    for r in range(1, height-1):
     for c in range(1, width-1):
     h = input[r][c]
     scenic[r * width + c] = (
     look(h, range(r-1, -1, -1), [c]) # top
     * look(h, range(r+1, height), [c]) # bottom
     * look(h, [r], range(c-1, -1, -1)) # left
     * look(h, [r], range(c+1, width)) # right
     )
    print(f"Best scenic tree : {max(scenic)}")
    • Yth.