My Advent of Code solutions in Python. kevinyap.ca/2019/12/going-fast-in-advent-of-code/
advent-of-code python
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Add 2022/05

+40
+40
2022/day05.py
··· 1 + import fileinput 2 + import copy 3 + from utils import parse_nums 4 + 5 + INPUT = ''.join(fileinput.input()) 6 + board, moves = INPUT.split('\n\n') 7 + 8 + board = board.splitlines() 9 + bottom = board[-1] 10 + num_of_stacks = max(int(x) for x in bottom.split()) 11 + STACKS = [[] for _ in range(num_of_stacks)] 12 + 13 + for line in board[::-1]: 14 + for i, crate in enumerate(line[1::4]): 15 + if crate.isupper(): 16 + STACKS[i].append(crate) 17 + 18 + MOVES = [parse_nums(line) for line in moves.splitlines()] 19 + 20 + 21 + def simulate(part_2=False): 22 + stacks = copy.deepcopy(STACKS) 23 + for amt, frm, to in MOVES: 24 + frm -= 1 25 + to -= 1 26 + 27 + crates_to_move = [] 28 + for _ in range(amt): 29 + crate = stacks[frm].pop() 30 + crates_to_move.append(crate) 31 + 32 + if not part_2: 33 + crates_to_move.reverse() 34 + 35 + stacks[to].extend(crates_to_move) 36 + 37 + return ''.join(s[-1] for s in stacks) 38 + 39 + print("Part 1:", simulate()) 40 + print("Part 2:", simulate(part_2=True))