#!/usr/bin/env python3
#Advent of Code
#2022 Day 5, Part 1
#Solution by James C. (https://github.com/JamesMCo)
importos,syssys.path.append(os.path.abspath("../.."))importunittest,util.readfromutil.testsimportrunimportredefsolve(puzzle_input):defparse_input(puzzle_input):separator=puzzle_input.index("")stacks_description=puzzle_input[:separator]instructions=puzzle_input[separator+1:]# Find the number of stacks (number of stack labels just above the separator line)
number_of_stacks=len(re.findall(r"\d+",puzzle_input[separator-1]))# Create enough empty stacks in a list
stacks=[[]forstackinrange(number_of_stacks)]# Matches "[X]" or " ", followed by an optional space, finding this "number_of_stacks" times on one line
row_pattern=re.compile(r"(?:(?:\[(\w)\]| {3}) ?)"*number_of_stacks)# Reverse the stacks described in the puzzle input (i.e. work from bottom to top)
forrowinstacks_description[:-1][::-1]:fori,crateinenumerate(re.match(row_pattern,row).groups()):# If a crate exists, create equals that letter. Otherwise, it equals None.
ifcrate:stacks[i].append(crate)returnstacks,instructionsdefmanipulate_stacks(stacks,instructions):forinstructionininstructions:quant,from_stack,to_stack=[int(x)forxinre.match(r"move (\d+) from (\d+) to (\d+)",instruction).groups()]# Stacks are one-indexed in puzzle input, but zero-indexed in list
from_stack-=1to_stack-=1forcrateinrange(quant):stacks[to_stack].append(stacks[from_stack].pop())returnstacks# The top of the each stack is at the end of the list
return"".join([stack[-1]forstackinmanipulate_stacks(*parse_input(puzzle_input))])defmain():puzzle_input=util.read.as_lines_only_rstrip()crates=solve(puzzle_input)print("The crates on top of the stacks are "+str(crates)+".")classAOC_Tests(unittest.TestCase):deftest_ex1(self):returnself.assertEqual(solve([" [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"]),"CMZ")if__name__=="__main__":run(main)
#!/usr/bin/env python3
#Advent of Code
#2022 Day 5, Part 2
#Solution by James C. (https://github.com/JamesMCo)
importos,syssys.path.append(os.path.abspath("../.."))importunittest,util.readfromutil.testsimportrunimportredefsolve(puzzle_input):defparse_input(puzzle_input):separator=puzzle_input.index("")stacks_description=puzzle_input[:separator]instructions=puzzle_input[separator+1:]# Find the number of stacks (number of stack labels just above the separator line)
number_of_stacks=len(re.findall(r"\d+",puzzle_input[separator-1]))# Create enough empty stacks in a list
stacks=[[]forstackinrange(number_of_stacks)]# Matches "[X]" or " ", followed by an optional space, finding this "number_of_stacks" times on one line
row_pattern=re.compile(r"(?:(?:\[(\w)\]| {3}) ?)"*number_of_stacks)# Reverse the stacks described in the puzzle input (i.e. work from bottom to top)
forrowinstacks_description[:-1][::-1]:fori,crateinenumerate(re.match(row_pattern,row).groups()):# If a crate exists, create equals that letter. Otherwise, it equals None.
ifcrate:stacks[i].append(crate)returnstacks,instructionsdefmanipulate_stacks(stacks,instructions):forinstructionininstructions:quant,from_stack,to_stack=[int(x)forxinre.match(r"move (\d+) from (\d+) to (\d+)",instruction).groups()]# Stacks are one-indexed in puzzle input, but zero-indexed in list
from_stack-=1to_stack-=1lifted_crates=stacks[from_stack][-quant:]delstacks[from_stack][-quant:]stacks[to_stack].extend(lifted_crates)returnstacks# The top of the each stack is at the end of the list
return"".join([stack[-1]forstackinmanipulate_stacks(*parse_input(puzzle_input))])defmain():puzzle_input=util.read.as_lines_only_rstrip()crates=solve(puzzle_input)print("The crates on top of the stacks are "+str(crates)+".")classAOC_Tests(unittest.TestCase):deftest_ex1(self):returnself.assertEqual(solve([" [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"]),"MCD")if__name__=="__main__":run(main)