Advertisement
Guest User

Untitled

a guest
Dec 14th, 2023
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.23 KB | Source Code | 0 0
  1. import time
  2.  
  3. filename = "data"
  4.  
  5. try:
  6.     with open(filename, "r") as file:
  7.         lines = file.read().splitlines()
  8.         print("File opened successfully.\n")
  9. except:
  10.     input("File '{filename}' not found. Program will now exit.\n")
  11.     exit()
  12.    
  13. for i, line in enumerate(lines):
  14.     lines[i] = [char for char in line]
  15.  
  16. y_range = range(len(lines))
  17. x_range = range(len(lines[0]))
  18. x_max = max(x_range)
  19. y_max = max(y_range)
  20. def tilt(_dir):
  21.  
  22.     if -1 in _dir:
  23.         y = 0
  24.        
  25.         for line in lines:
  26.             x = 0
  27.             for char in line:
  28.                 if char not in "#.":
  29.                     _y = y
  30.                     _x = x
  31.                     while ((_y + _dir[0]) in y_range) and ((_x + _dir[1]) in x_range) and lines[_y + _dir[0]][_x + _dir[1]] == '.':
  32.                         lines[_y][_x] = '.'
  33.                         lines[_y + _dir[0]][_x + _dir[1]] = 'O'
  34.                         _y += _dir[0]
  35.                         _x += _dir[1]
  36.                 x += 1
  37.             y += 1
  38.     else:
  39.         y = y_max
  40.         for line in reversed(lines):
  41.             x = x_max
  42.             for char in reversed(line):
  43.                 if char not in "#.":
  44.                     _y = y
  45.                     _x = x
  46.                     while ((_y + _dir[0]) in y_range) and ((_x + _dir[1]) in x_range) and lines[_y + _dir[0]][_x + _dir[1]] == '.':
  47.                         lines[_y][_x] = '.'
  48.                         lines[_y + _dir[0]][_x + _dir[1]] = 'O'
  49.                         _y += _dir[0]
  50.                         _x += _dir[1]
  51.                 x -= 1
  52.             y -= 1
  53.                
  54. def cycle():
  55.     tilt([-1, 0])
  56.     tilt([0, -1])
  57.     tilt([1, 0])
  58.     tilt([0, 1])
  59.  
  60. def print_map():
  61.     for line in lines:
  62.         string = ""
  63.         for char in line:
  64.             string += char
  65.         print(string)
  66.     print("\n")
  67.  
  68. def print_index(i):
  69.     for line in states[i]:
  70.         string = ""
  71.         for char in line:
  72.             string += char
  73.         print(string)
  74.     print("\n")
  75.  
  76. def copy_2d_list(_list):
  77.     new = []
  78.     for sub_list in _list:
  79.         new.append(sub_list.copy())
  80.     return new
  81.  
  82. #save the current data for use in part 2
  83. lines2 = copy_2d_list(lines)
  84.  
  85. start = time.time()
  86. tilt([-1, 0])
  87. result = 0
  88. distance = len(lines)
  89. for line in lines:
  90.     for char in line:
  91.         if char == "O": result += distance
  92.     distance -= 1
  93. end = time.time()
  94. time_needed = end-start
  95. print(f"Part 1:\n Time needed: {round(time_needed, 3)}s\n Load: {result}\n")
  96.  
  97. #load the saved data
  98. lines = copy_2d_list(lines2)
  99.  
  100. start = time.time()
  101.  
  102. cycles_max = 1000000000
  103. states = []
  104. cycles = 0
  105.  
  106. while True:
  107.     cycle()
  108.     if lines in states:
  109.         cycle_start = states.index(lines)
  110.         cycle_end = cycles
  111.         cycle_length = cycle_end - cycle_start
  112.         break
  113.     states.append(copy_2d_list(lines))
  114.     cycles += 1
  115.  
  116. left_over = (cycles_max - cycle_start) % cycle_length
  117. lines = states[cycle_start + left_over-1]
  118.  
  119. result = 0
  120. distance = len(lines)
  121. for line in lines:
  122.     for char in line:
  123.         if char == "O": result += distance
  124.     distance -= 1
  125.  
  126. end = time.time()
  127. time_needed = end-start
  128. input(f"Part 2:\n Time needed: {round(time_needed, 3)}s\n Load: {result}\n")
  129.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement