Advertisement
Guest User

clustertruck

a guest
Dec 7th, 2022
442
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.43 KB | Source Code | 0 0
  1. import nums_from_string
  2. import timeit
  3.  
  4. def readInput():    
  5.     with(open('2022\day5i.txt', 'r')) as f:
  6.         crates = []
  7.         moves = []
  8.         for line in f:
  9.             row = []
  10.             if line[0] == 'm':
  11.                 moves.append(''.join(line).strip('\n'))
  12.             elif len(line) > 1 :                
  13.                 for c in range(1, len(line.strip('\n')), 4):
  14.                     if isValidChar(line[c]) or line[c].isdigit():
  15.                         row.append(line[c])
  16.                     else:
  17.                         row.append('-')
  18.            
  19.             crates.append(row) if len(row) > 1 else None
  20.         return crates, moves
  21.  
  22. def isValidChar(i):
  23.     return True if 65 <= ord(i) <= 90 else False
  24.  
  25.  
  26. def part1():
  27.  
  28.     def moveCrate(crates, move):
  29.         points = nums_from_string.get_nums(move)
  30.         for i in range(points[0]):
  31.             sX, eX = crates[-1].index(str(points[1])), crates[-1].index(str(points[2]))
  32.            
  33.             sY, eY = getPositions(crates, sX, eX)
  34.             if eY == -1:
  35.                 temp = ['-', '-', '-', '-', '-', '-', '-', '-', '-']
  36.                 temp[eX] = crates[sY][sX]
  37.                 crates.insert(0, temp)
  38.                 sY, eY = getPositions(crates, sX, eX)
  39.             else:
  40.                 crates[eY][eX] = crates[sY][sX]
  41.             crates[sY][sX] = '-'
  42.         if crates == None:
  43.             True
  44.         try:
  45.             clearEmptyRows(crates)
  46.         except:
  47.             return crates
  48.         return crates
  49.  
  50.     def clearEmptyRows(crates):
  51.         for row in crates:
  52.             if all(p == '-' for p in row):
  53.                 crates = crates.remove(row)
  54.  
  55.     def getPositions(crates, sX, eX):
  56.         for y in range(len(crates)):
  57.             if isValidChar(crates[y][sX]):
  58.                 sY = y
  59.                 break
  60.         for y in reversed(range(len(crates))):
  61.             if crates[y][eX] == '-':
  62.                 return sY, y
  63.         return sY, -1
  64.  
  65.     def getTopCrates(crates):
  66.         records = {}
  67.         for i in range(len(crates)):
  68.             for j in range(len(crates[i])):
  69.                 if isValidChar(crates[i][j]) and j not in records.keys():
  70.                     records[j] = crates[i][j]
  71.  
  72.         return records
  73.  
  74.     crates, moves = readInput()
  75.     for move in moves:
  76.         moveCrate(crates, move)
  77.        
  78.     records= getTopCrates(crates)
  79.     records = dict(sorted(records.items()))
  80.     result = ""
  81.     for v in records.values():
  82.         result += v
  83.     True
  84.  
  85. def part2():
  86.    
  87.     def moveCrates(crates, move):
  88.         points = nums_from_string.get_nums(move)
  89.         sX = points[1] - 1
  90.         eX = points[2] - 1
  91.         ePos = getEnd(crates, eX, points[0])
  92.         sPos = getStart(crates, sX, points[0])
  93.         tI = 0
  94.         if len(ePos) > 1:
  95.             ePos.sort()
  96.  
  97.         for i in range(len(ePos)):
  98.             crates[ePos[i]][eX] = crates[sPos[tI]][sX]
  99.             crates[sPos[tI]][sX]  = '-'
  100.             tI+=1
  101.  
  102.         return crates
  103.            
  104.     def getStart(crates, sX, amount):
  105.         sPos = []
  106.         for i in range(len(crates)):
  107.             if isValidChar(crates[i][sX]):
  108.                 sPos.append(i)
  109.             if len(sPos) >= amount:
  110.                 break
  111.         return sPos
  112.  
  113.     def getEnd(crates, eX, amount):
  114.         ePos = []
  115.         available = 0
  116.  
  117.         for i in range(len(crates)):
  118.             if isValidChar(crates[i][eX]):
  119.                 break
  120.             else:
  121.                 available += 1
  122.  
  123.         while(amount > available):
  124.             crates = addTopRow(crates)
  125.             available += 1
  126.  
  127.         for i in reversed(range(len(crates) - 1)):
  128.             if isValidChar(crates[i][eX]) == False:
  129.                 ePos.append((i))
  130.             if len(ePos) >= amount:
  131.                 break
  132.         return ePos
  133.  
  134.     def addTopRow(crates):
  135.         temp = ['-', '-', '-', '-', '-', '-', '-', '-', '-']
  136.         crates.insert(0, temp)
  137.         return crates
  138.  
  139.     crates, moves = readInput()
  140.  
  141.     for move in moves:
  142.         crates = moveCrates(crates, move)
  143.  
  144.     return crates
  145.  
  146. start = timeit.default_timer()
  147. print(f"Result of part 1: {part1()}")
  148. stop = timeit.default_timer()
  149. execution_time = stop - start
  150. print("Part 1 Executed in "+str(execution_time))
  151.  
  152. start = timeit.default_timer()
  153. print(f"Result of part 2: {part2()}")
  154. stop = timeit.default_timer()
  155. execution_time = stop - start
  156. print("Part 2 Executed in "+str(execution_time))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement