• # python en 44 LoC, 110ms et 10MB RAM

    Posté par . En réponse au message Advent of Code, jour 14. Évalué à 2. Dernière modification le 16 décembre 2023 à 02:22.

    import sys
    I = sys.stdin.read().splitlines()
    G = [list(l) for l in I]
    R = len(G)
    C = len(G[0])
    def rotate(G, F):
     for r in range(R):
     for c in range(C):
     F[c][R-1-r] = G[r][c]
    def tilt(G):
     for x in range(C):
     for y in range(R):
     if G[y][x] == "O":
     for j in range(y-1,0-1,-1):
     if G[j][x] == '.':
     G[j][x] = "O"
     G[j+1][x] = "."
     else:
     break
    score = lambda: sum((R-y)*row.count('O') for y,row in enumerate(G))
    cache = {}
    F = [['?' for _ in range(C)] for _ in range(R)] # pre-allocate saves 10% time (no GC)
    end = 1_000_000_000
    i = 0
    while i<end:
     i += 1
     for j in range(4):
     tilt(G)
     if i == 1 and j == 0: print(score()) # part 1
     rotate(G, F); F, G = G, F
     h = ''.join(''.join(row) for row in G)
     if h in cache:
     period = i-cache[h]
     skip = (end-i) // period
     i += skip * period
     cache[h] = i
    print(score())