diff options
| author | Tomasz Kramkowski <tomasz@kramkow.ski> | 2022-12-07 12:57:36 +0000 | 
|---|---|---|
| committer | Tomasz Kramkowski <tomasz@kramkow.ski> | 2022-12-07 12:57:36 +0000 | 
| commit | d9a97e9bf28cf91f1a7e57f1298b7c742e25ed56 (patch) | |
| tree | 999464f801480565733c5223b020b2d762140b00 /5.py | |
| download | aoc2022-d9a97e9bf28cf91f1a7e57f1298b7c742e25ed56.tar.gz aoc2022-d9a97e9bf28cf91f1a7e57f1298b7c742e25ed56.tar.xz aoc2022-d9a97e9bf28cf91f1a7e57f1298b7c742e25ed56.zip  | |
days 1-7
Diffstat (limited to '5.py')
| -rw-r--r-- | 5.py | 29 | 
1 files changed, 29 insertions, 0 deletions
@@ -0,0 +1,29 @@ +from copy import deepcopy +with open('5.in.test') as f: +    stacks, instructions = f.read().rstrip().split('\n\n') +layers = stacks.split('\n') +stacks = [list() for _ in range((len(layers[-1]) + 1) // 4)] +for layer in reversed(layers[:-1]): +    for i in range(len(stacks)): +        c = layer[i * 4 + 1] +        if c != ' ': +            stacks[i].append(c) +instructions = [i.split(' ') for i in instructions.split('\n')] +instructions = [tuple(int(i) for i in (i[1], i[3], i[5])) for i in instructions] +def part1(stacks, instructions): +    stacks = deepcopy(stacks) +    for count, src, dst in instructions: +        for _ in range(count): +            stacks[dst-1].append(stacks[src-1].pop()) +    return ''.join(s[-1] for s in stacks) +def part2(stacks, instructions): +    stacks = deepcopy(stacks) +    for count, src, dst in instructions: +        tmp = list() +        for _ in range(count): +            tmp.append(stacks[src-1].pop()) +        for _ in range(count): +            stacks[dst-1].append(tmp.pop()) +    return ''.join(s[-1] for s in stacks) +print(part1(stacks, instructions)) +print(part2(stacks, instructions))  | 
