Skip to content

Commit

Permalink
Day 5 Completed
Browse files Browse the repository at this point in the history
  • Loading branch information
SpawnTerror committed Dec 25, 2022
1 parent 7e32665 commit f9fcb81
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 7 deletions.
36 changes: 30 additions & 6 deletions day5/part1.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,52 @@
# Python 3.11.1
# AOC Day 5 Part 1

with open('day5/test.txt', 'r') as f:
# Open the file, read and split by lines
with open('day5/input.txt', 'r') as f:
inputData = f.read().splitlines()

# Enumerate lines and each string
# Empty line means end of crates
for lineNumber, stringData in enumerate(inputData):
if stringData == '':
separateLine = lineNumber


# Divide crates and movement instructions
cratesData = inputData[:separateLine]
movesData = inputData[separateLine+1:]
movementData = inputData[separateLine+1:]

# Count all the columns by finding the biggest integer in the last line
columnsNumber = int(max(cratesData[-1]))

# Define a list and answer string
movementlist = []
answer = ""

# Create a list, based on the last line in cratesData
cratesList = [[letter] for letter in cratesData[-2][1:4*columnsNumber:4]]

# Then enumerate position and letter, append to the above list
for string in cratesData[-3::-1]:
for position, letter in enumerate(string[1:4*columnsNumber:4]):
if letter.isalpha():
cratesList[position].append(letter)

for movementInstruction in movesData:
# Create movement list by taking position 1, 3 and 5 from each line
for movementInstruction in movementData:
splitIntruction = movementInstruction.split(' ')
movementlist.append([int(splitIntruction[1]),int(splitIntruction[3]),int(splitIntruction[5])])

print(cratesList)
print(movementlist)
# Move crates one by one, use pop and append
#
for movement in movementlist:
for position in range(movement[0],0,-1):
bufferCrate = cratesList[movement[1]-1].pop()
# print(f'Buffer crate = ', bufferCrate)
cratesList[movement[2]-1].append(bufferCrate)

# Get all the last letters in the list [-1]
for number in range(0, columnsNumber):
answer = answer + cratesList[number][-1]
print(f'Crates on top, counting from the left column: ', answer)


50 changes: 49 additions & 1 deletion day5/part2.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,51 @@
# SpawnTerror 2022
# Python 3.11.1
# AOC Day 5 Part 2
# AOC Day 5 Part 2

# Open the file, read and split by lines
with open('day5/input.txt', 'r') as f:
inputData = f.read().splitlines()

# Enumerate lines and each string
# Empty line means end of crates
for lineNumber, stringData in enumerate(inputData):
if stringData == '':
separateLine = lineNumber

# Divide crates and movement instructions
cratesData = inputData[:separateLine]
movementData = inputData[separateLine+1:]

# Count all the columns by finding the biggest integer in the last line
columnsNumber = int(max(cratesData[-1]))

# Define a list and answer string
movementlist = []
answer = ""

# Create a list, based on the last line in cratesData
cratesList = [[letter] for letter in cratesData[-2][1:4*columnsNumber:4]]

# Then enumerate position and letter, append to the above list
for string in cratesData[-3::-1]:
for position, letter in enumerate(string[1:4*columnsNumber:4]):
if letter.isalpha():
cratesList[position].append(letter)

# Create movement list by taking position 1, 3 and 5 from each line
for movementInstruction in movementData:
splitIntruction = movementInstruction.split(' ')
movementlist.append([int(splitIntruction[1]),int(splitIntruction[3]),int(splitIntruction[5])])

# Move crates all at once, use pop and append
for movement in movementlist:
for position in range(movement[0],0,-1):
bufferCrate = cratesList[movement[1]-1].pop(-position)
# Move all the crates at once (-position)
# print(f'Buffer crate = ', bufferCrate)
cratesList[movement[2]-1].append(bufferCrate)

# Get all the last letters in the list [-1]
for number in range(0, columnsNumber):
answer = answer + cratesList[number][-1]
print(f'Crates on top, counting from the left column: ', answer)

0 comments on commit f9fcb81

Please sign in to comment.