from tools.aoc import AOCDay from typing import Any class Day(AOCDay): inputs = [ [ ("CMZ", "input5_test"), ("VJSFHWGFT", "input5_dennis"), ("WSFTMRHPP", "input5"), ], [ ("MCD", "input5_test"), ("LCTQFBVZV", "input5_dennis"), ("GSLCMFBRP", "input5"), ] ] def get_stacks_and_moves(self) -> (list, list): i = self.getInput() stacks = {} for l, line in enumerate(i): if not line: ret = [] for k in sorted(stacks.keys()): ret.append(stacks[k]) return ret, i[l+1:] this_stacks = (len(line) + 1) // 4 for stack_num in range(this_stacks): crate = line[stack_num * 4 + 1] if ord(crate) > 64: if stack_num not in stacks: stacks[stack_num] = [] stacks[stack_num].append(crate) def part1(self) -> Any: stacks, moves = self.get_stacks_and_moves() for move in moves: l = move.split(" ") count, move_from, move_to = map(int, [l[1], l[3], l[5]]) for _ in range(count): stacks[move_to - 1].insert(0, stacks[move_from - 1].pop(0)) string = "" for x in stacks: string += x.pop(0) return string def part2(self) -> Any: stacks, moves = self.get_stacks_and_moves() for move in moves: l = move.split(" ") count, move_from, move_to = map(int, [l[1], l[3], l[5]]) stacks[move_to - 1] = stacks[move_from - 1][:count] + stacks[move_to - 1] stacks[move_from - 1] = stacks[move_from - 1][count:] string = "" for x in stacks: string += x.pop(0) return string if __name__ == '__main__': day = Day(2022, 5) day.run(verbose=True)