Guest User

Untitled

a guest
Dec 6th, 2022
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.94 KB | None | 0 0
  1. # author: https://tweakers.net/gallery/369377/
  2. import os
  3. from multiprocessing import Pool
  4. from time import perf_counter
  5. t1_start = perf_counter()
  6. filename = os.path.join(os.path.dirname(__file__), "input.txt")
  7.  
  8. def read_input():
  9.     f = open(filename, "r")
  10.     input = f.read().rstrip('\n')
  11.     f.close()
  12.     return input
  13.  
  14. def find_packet(j):    
  15.     input = read_input()
  16.     buffer = []
  17.     for idx, i in enumerate(input):
  18.         buffer.insert(0, i)
  19.         if len(buffer) > j:
  20.             buffer.pop()
  21.             if len(set(buffer)) == j:
  22.                 return idx+1 if idx > 1 else 1
  23.  
  24. def solve_b():
  25.     total = 0
  26.     with Pool() as pool:
  27.         for result in pool.map(find_packet, range(1,95)):
  28.             total += result  # type: ignore
  29.     return total
  30.  
  31.  
  32. if __name__=="__main__":
  33.     print("Part B solution: ", solve_b())
  34.     t1_stop = perf_counter()
  35.     print("Solution reached in:", round(t1_stop-t1_start, 5), 's')
Advertisement
Add Comment
Please, Sign In to add comment