Advertisement
Guest User

[2024 Day 9 Part 2]

a guest
Feb 10th, 2025
89
0
243 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.57 KB | Source Code | 0 0
  1. import sys
  2. import time
  3.  
  4. class Timer:
  5.     def __init__(self):
  6.         self.start = time.time()
  7.     def __del__(self):
  8.         end = time.time()
  9.         print(f"Elapsed time: {(end - self.start)*1e6:.0f} us")
  10.  
  11. class Block:
  12.     def __init__(self,x,y=-1):
  13.         self.block_size = x
  14.         self.file_id = y
  15.     def __repr__(self):
  16.         return f"({self.block_size},{self.file_id})"
  17.  
  18. def printBlocks(blocks):
  19.     for b in blocks:
  20.         for i in range(b.block_size):
  21.             if b.file_id == -1:
  22.                 print(".", end="")
  23.             else:
  24.                 print(b.file_id, end="")
  25.     print()    
  26.  
  27. def moveBlocks(dm, j):
  28.     tmp = []
  29.     if dm[j].file_id == -1:
  30.         # print(f"Selected Block {dm[j]} is empty")
  31.         return dm
  32.  
  33.     # print("Selected Block = ", dm[j])
  34.    
  35.     for i in range(len(dm)):
  36.         if j < i:
  37.             tmp.extend(dm[i:])
  38.             break
  39.        
  40.         if dm[i].file_id != -1:
  41.             tmp.append(dm[i])
  42.             continue
  43.  
  44.         if dm[i].block_size < dm[j].block_size:
  45.             tmp.append(dm[i])
  46.             continue
  47.        
  48.         # if dm[j].file_id != -1 and dm[i].file_id == -1 and dm[i].block_size >= dm[j].block_size:
  49.         # all these statements are already True so they are redundant
  50.         if True:
  51.             diff = dm[i].block_size - dm[j].block_size
  52.             if diff > 0:
  53.                 tmp.append(Block(dm[j].block_size, dm[j].file_id))
  54.                 tmp.append(Block(diff,-1))
  55.                 dm[j].file_id = -1
  56.             else:
  57.                 tmp.append(Block(dm[j].block_size, dm[j].file_id))
  58.                 dm[j].file_id = -1
  59.                
  60.             tmp.extend(dm[i+1:])
  61.             break
  62.      
  63.     return tmp
  64.  
  65.  
  66. def part2(data):
  67.     t = Timer()
  68.     dm = []
  69.     for i in range(len(data)):
  70.         if i % 2 == 0:
  71.             dm.append(Block(data[i], int(i/2)))
  72.         else:
  73.             dm.append(Block(data[i], -1))
  74.    
  75.     # printBlocks(dm)
  76.  
  77.     for j in reversed(range(len(dm))):
  78.         dm = moveBlocks(dm, j)
  79.         # printBlocks(dm)
  80.  
  81.     result = 0
  82.     pos = 0
  83.     for b in dm:
  84.         for i in range(b.block_size):
  85.             if b.file_id != -1:
  86.                 result += (b.file_id*pos)
  87.             pos += 1  
  88.     print(f"Part 2: {result}")
  89.     return
  90.    
  91.  
  92.  
  93. if __name__ == "__main__":
  94.  
  95.     if len(sys.argv) != 2:
  96.         print("Please provide the target number as an argument.")
  97.         sys.exit(1)
  98.  
  99.     with open(sys.argv[1], "r") as f:
  100.         s = f.readlines()[0].strip()
  101.  
  102.     data = [int(i) for i in s]
  103.     part2(data)
  104.  
  105.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement