Advertisement
eliax1996

Untitled

Dec 14th, 2023
693
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.90 KB | None | 0 0
  1. text = """
  2. O....#....
  3. O.OO#....#
  4. .....##...
  5. OO.#O....O
  6. .O.....O#.
  7. O.#..O.#.#
  8. ..O..#O..O
  9. .......O..
  10. #....###..
  11. #OO..#....
  12. """
  13.  
  14. game = tuple(tuple(line) for line in text.strip().split("\n"))
  15.  
  16.  
  17. def display(board):
  18.     for i in range(0, len(board)):
  19.         for j in range(0, len(board[i])):
  20.             print(board[i][j], end="")
  21.         print()
  22.  
  23.     print("\n\n")
  24.  
  25.  
  26. @cache
  27. def rotate_north(game_board):
  28.     board = [list(line) for line in game_board]
  29.  
  30.     print("computing new board")
  31.  
  32.     for i in range(0, len(board)):
  33.         for j in range(0, len(board[0])):
  34.             if board[i][j] != "O":
  35.                 continue
  36.  
  37.             destination = i - 1
  38.             while destination > -1 and board[destination][j] == ".":
  39.                 destination -= 1
  40.  
  41.             board[i][j], board[destination + 1][j] = board[destination + 1][j], board[i][j]
  42.  
  43.     return tuple(tuple(line) for line in board)
  44.  
  45.  
  46. @cache
  47. def rotate_south(game_board):
  48.     board = [list(line) for line in game_board]
  49.  
  50.     print("computing new board")
  51.  
  52.     for i in range(len(board) - 1, -1, -1):
  53.         for j in range(0, len(board[0])):
  54.             if board[i][j] != "O":
  55.                 continue
  56.  
  57.             destination = i + 1
  58.             while destination < len(board) and board[destination][j] == ".":
  59.                 destination += 1
  60.  
  61.             board[i][j], board[destination - 1][j] = board[destination - 1][j], board[i][j]
  62.  
  63.     return tuple(tuple(line) for line in board)
  64.  
  65.  
  66. @cache
  67. def rotate_east(game_board):
  68.     board = [list(line) for line in game_board]
  69.  
  70.     print("computing new board")
  71.  
  72.     for i in range(0, len(board)):
  73.         for j in range(len(board[0]) - 1, -1, -1):
  74.             if board[i][j] != "O":
  75.                 continue
  76.  
  77.             destination = j + 1
  78.             while destination < len(board) and board[i][destination] == ".":
  79.                 destination += 1
  80.  
  81.             board[i][j], board[i][destination - 1] = board[i][destination - 1], board[i][j]
  82.  
  83.     return tuple(tuple(line) for line in board)
  84.  
  85.  
  86. @cache
  87. def rotate_west(game_board):
  88.     board = [list(line) for line in game_board]
  89.  
  90.     print("computing new board")
  91.  
  92.     for i in range(0, len(board)):
  93.         for j in range(0, len(board[i])):
  94.             if board[i][j] != "O":
  95.                 continue
  96.  
  97.             destination = j - 1
  98.             while destination > -1 and board[i][destination] == ".":
  99.                 destination -= 1
  100.  
  101.             board[i][j], board[i][destination + 1] = board[i][destination + 1], board[i][j]
  102.  
  103.     return tuple(tuple(line) for line in board)
  104.  
  105.  
  106. @cache
  107. def cycle(board):
  108.     new_game = rotate_north(board)
  109.     new_game = rotate_west(new_game)
  110.     new_game = rotate_south(new_game)
  111.     new_game = rotate_east(new_game)
  112.     return new_game
  113.  
  114.  
  115. @cache
  116. def points(board):
  117.     tot = 0
  118.     points = len(board)
  119.     for i in range(len(board) - 1, -1, -1):
  120.         for j in range(0, len(board[i])):
  121.             if board[i][j] == "O":
  122.                 tot += points - i
  123.     return tot
  124.  
  125.  
  126. combinations_and_transformed = {}
  127. first_in_repetition = None
  128.  
  129. cycle_start = None
  130. cycle_width = None
  131.  
  132. for i in range(0, 1000000000):
  133.     new_game = cycle(game)
  134.     if game in combinations_and_transformed and combinations_and_transformed[game][1] == new_game:
  135.         cycle_start = combinations_and_transformed[game][0]
  136.         cycle_width = i - 1 - combinations_and_transformed[game][0]
  137.         break
  138.     else:
  139.         combinations_and_transformed[game] = (i - 1, new_game)  # we are storing the index of the previous result
  140.     game = new_game
  141.  
  142. print(f"cycle starts at {cycle_start}")
  143. print(f"cycle width {cycle_width}")
  144. print(f"missing {(1000000000 - cycle_start) % cycle_width}")
  145.  
  146. for i in range(0, (1000000000 - (cycle_start + cycle_width)) % cycle_width + cycle_width - 1):
  147.     print(points(game))
  148.     game = cycle(game)
  149.  
  150. print(f"points of the final position: {points(game)}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement