more cleaning

This commit is contained in:
Jake Pullen
2023-12-17 17:37:01 +00:00
parent 8c2a9db754
commit c7107a4d44
50 changed files with 58 additions and 58 deletions
+69
View File
@@ -0,0 +1,69 @@
with open(r'advent_of_code\2022\5\input.txt', 'r') as file:
input = file.read()
with open(r'advent_of_code\2022\5\test_input.txt', 'r') as file:
test_input = file.read()
# print(input)
pile_map = {
1: 1,
2: 5,
3: 9,
4: 13,
5: 17,
6: 21,
7: 25,
8: 29,
9: 33
}
input = test_input
line_first_move = input.find('move')
loading_area = input[:(line_first_move-2)]
# print(loading_area)
instructions = input[line_first_move:]
# print(instructions)
print(loading_area)
# test1 = loading_area[5][0]
# print(test1)
def get_top_letter(pile_number, loading_area):
column = pile_map[pile_number]
lines = loading_area.split('\n')
for line in lines:
if line[column] != ' ':
value = line[column]
return value
return None
def add_to_pile(value, pile_number, loading_area):
column = pile_map[pile_number]
lines = [list(line) for line in loading_area.split('\n')]
for line in reversed(lines):
if line[column] == ' ':
line[column] = value
break
loading_area = '\n'.join(''.join(line) for line in lines)
return loading_area
# test = add_to_pile('X',3, loading_area)
# print(test)
def move_box(from_pile, to_pile):
# find the box on the top of the pile
letter_to_move = get_top_letter(from_pile, loading_area)
# move it to the top of the other pile
add_to_pile(letter_to_move, to_pile, loading_area)
# return the new loading area
return loading_area
for line in instructions.split('\n'):
line = line.split(' ')
counter = int(line[1])
for repeat in range(counter):
move_box(line[3], line[5])
#print(line)
print(loading_area)
+6
View File
@@ -0,0 +1,6 @@
import os
with open(r'advent_of_code\2022\5\input.txt', 'r') as file:
input = file.read()
print(input)
+64
View File
@@ -0,0 +1,64 @@
# Day 5: Supply Stacks (https://adventofcode.com/2022/day/5)
# Conditions:
# - The input represents two sections seperated by an empty line, crates section and rearrangement section
# - Crates Section is a 9 by 9 table, with vertical stacks and horizontal levels; The bottom level describes a stack
# with numbers from 1 to 9; Other levels (8 of them) are filled with crates represented by capital letters surrounded
# by squared brackets; Lack of crate is represented by three empty spaces
# - Rearrangement section consists of a list of instructions; An instruction has a syntax "move [number of crates] from
# [stack x] to [stack y]"
def main():
print("Advent of Code - Day 5")
file_task_1 = open(r'advent_of_code\2022\5\test_input.txt', 'r')
file_task_2 = open(r'advent_of_code\2022\5\input.txt', 'r')
task_1(file_task_1)
#task_2(file_task_2)
def get_sections(file):
# Separate the file into two sections: instructions and levels; Furthermore get a list of stacks using the levels
levels, instructions = [section.split("\n") for section in file.read().split("\n\n")]
levels = [crate.replace(" ", " [X] ") for crate in levels[:-1]] # Replace empty space with [X]
levels = [[crate[1] for crate in level.split()] for level in levels] # Get rid of brackets
stacks = [[] for _ in range(len(levels[0]))] # Create nested lists of number of vertical stacks
for level in reversed(levels):
for index, crate in enumerate(level):
if crate != "X":
stacks[index].append(crate)
return instructions, levels, stacks
# Task 1: Get the crate names from the top of each stack after the rearrangement procedure, hereby the crates are moved
# one after another, so the top crate of one stock ends up on the bottom of another
def task_1(file):
# Iterate over instructions, get relevant values from it and move the creates
instructions, levels, stacks = get_sections(file)
#print(stacks)
for instruction in instructions:
quantity, from_stack_order, to_stack_order = [int(i) for i in instruction.split(" ") if i.isnumeric()]
while quantity != 0:
print(stacks)
stacks[to_stack_order-1].append(stacks[from_stack_order - 1].pop()) # Move crates each after another
print(stacks)
quantity -= 1
print("Task 1 result: ", end="")
[print(stack[-1], end="") for stack in stacks]
print()
# Task 2: Get the crate names from the top of each stack after the rearrangement procedure, hereby the crates are moved
# all at once, so the top crate of one stock ends up on the top of another
def task_2(file):
instructions, levels, stacks = get_sections(file)
for instruction in instructions:
quantity, from_stack_order, to_stack_order = [int(i) for i in instruction.split(" ") if i.isnumeric()]
from_stack = stacks[from_stack_order-1]
stacks[to_stack_order-1].extend(from_stack[len(from_stack)-quantity:]) # Move crates all at once
stacks[from_stack_order-1] = from_stack[:len(from_stack)-quantity]
print("Task 2 result: ", end="")
[print(stack[-1], end="") for stack in stacks]
main()
+9
View File
@@ -0,0 +1,9 @@
[D]
[N] [C]
[Z] [M] [P]
1 2 3
move 1 from 2 to 1
move 3 from 1 to 3
move 2 from 2 to 1
move 1 from 1 to 2