Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import nums_from_string
- import timeit
- def readInput():
- with(open('2022\day5i.txt', 'r')) as f:
- crates = []
- moves = []
- for line in f:
- row = []
- if line[0] == 'm':
- moves.append(''.join(line).strip('\n'))
- elif len(line) > 1 :
- for c in range(1, len(line.strip('\n')), 4):
- if isValidChar(line[c]) or line[c].isdigit():
- row.append(line[c])
- else:
- row.append('-')
- crates.append(row) if len(row) > 1 else None
- return crates, moves
- def isValidChar(i):
- return True if 65 <= ord(i) <= 90 else False
- def part1():
- def moveCrate(crates, move):
- points = nums_from_string.get_nums(move)
- for i in range(points[0]):
- sX, eX = crates[-1].index(str(points[1])), crates[-1].index(str(points[2]))
- sY, eY = getPositions(crates, sX, eX)
- if eY == -1:
- temp = ['-', '-', '-', '-', '-', '-', '-', '-', '-']
- temp[eX] = crates[sY][sX]
- crates.insert(0, temp)
- sY, eY = getPositions(crates, sX, eX)
- else:
- crates[eY][eX] = crates[sY][sX]
- crates[sY][sX] = '-'
- if crates == None:
- True
- try:
- clearEmptyRows(crates)
- except:
- return crates
- return crates
- def clearEmptyRows(crates):
- for row in crates:
- if all(p == '-' for p in row):
- crates = crates.remove(row)
- def getPositions(crates, sX, eX):
- for y in range(len(crates)):
- if isValidChar(crates[y][sX]):
- sY = y
- break
- for y in reversed(range(len(crates))):
- if crates[y][eX] == '-':
- return sY, y
- return sY, -1
- def getTopCrates(crates):
- records = {}
- for i in range(len(crates)):
- for j in range(len(crates[i])):
- if isValidChar(crates[i][j]) and j not in records.keys():
- records[j] = crates[i][j]
- return records
- crates, moves = readInput()
- for move in moves:
- moveCrate(crates, move)
- records= getTopCrates(crates)
- records = dict(sorted(records.items()))
- result = ""
- for v in records.values():
- result += v
- True
- def part2():
- def moveCrates(crates, move):
- points = nums_from_string.get_nums(move)
- sX = points[1] - 1
- eX = points[2] - 1
- ePos = getEnd(crates, eX, points[0])
- sPos = getStart(crates, sX, points[0])
- tI = 0
- if len(ePos) > 1:
- ePos.sort()
- for i in range(len(ePos)):
- crates[ePos[i]][eX] = crates[sPos[tI]][sX]
- crates[sPos[tI]][sX] = '-'
- tI+=1
- return crates
- def getStart(crates, sX, amount):
- sPos = []
- for i in range(len(crates)):
- if isValidChar(crates[i][sX]):
- sPos.append(i)
- if len(sPos) >= amount:
- break
- return sPos
- def getEnd(crates, eX, amount):
- ePos = []
- available = 0
- for i in range(len(crates)):
- if isValidChar(crates[i][eX]):
- break
- else:
- available += 1
- while(amount > available):
- crates = addTopRow(crates)
- available += 1
- for i in reversed(range(len(crates) - 1)):
- if isValidChar(crates[i][eX]) == False:
- ePos.append((i))
- if len(ePos) >= amount:
- break
- return ePos
- def addTopRow(crates):
- temp = ['-', '-', '-', '-', '-', '-', '-', '-', '-']
- crates.insert(0, temp)
- return crates
- crates, moves = readInput()
- for move in moves:
- crates = moveCrates(crates, move)
- return crates
- start = timeit.default_timer()
- print(f"Result of part 1: {part1()}")
- stop = timeit.default_timer()
- execution_time = stop - start
- print("Part 1 Executed in "+str(execution_time))
- start = timeit.default_timer()
- print(f"Result of part 2: {part2()}")
- stop = timeit.default_timer()
- execution_time = stop - start
- print("Part 2 Executed in "+str(execution_time))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement