Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from collections import deque
- def get_puzzle_input(a_file_name) -> list[str]:
- with open(a_file_name) as INFILE:
- return [line.rstrip() for line in INFILE if not line.startswith('#')]
- def swap_positions(a_encrypted: list[str], a_x:int, a_y:int):
- a_encrypted[a_x], a_encrypted[a_y] = a_encrypted[a_y], a_encrypted[a_x]
- def swap_letter(a_encrypted: list[str], a_char_a:str, a_char_b:str):
- char_a_pos = a_encrypted.index(a_char_a)
- char_b_pos = a_encrypted.index(a_char_b)
- swap_positions(a_encrypted, char_a_pos, char_b_pos)
- def rotate_steps(a_encrypted: list[str], a_steps:int, a_direction:str):
- d_encrypted = deque(a_encrypted)
- d_encrypted.rotate(int(a_steps) if a_direction == 'right' else -int(a_steps))
- a_encrypted.clear()
- a_encrypted.extend(list(d_encrypted))
- def reverse_chunk(a_encrypted: list[str], a_from:int, a_to:int):
- reverse_chunk = a_encrypted[a_from:a_to + 1][::-1]
- encrypted = a_encrypted[:a_from] + reverse_chunk + a_encrypted[a_to + 1:]
- a_encrypted.clear()
- a_encrypted.extend(encrypted)
- pass
- def rotate_letter(a_encrypted: list[str], a_char:str):
- char_index = a_encrypted.index(a_char)
- steps_to_rotate = 1 + char_index
- steps_to_rotate += 1 if char_index > 3 else 0
- rotate_steps(a_encrypted, steps_to_rotate, 'right')
- def move_char(a_encrypted: list[str], a_char_from:int, a_char_to:int):
- copy = a_encrypted.copy()
- char_to_move = copy[a_char_from]
- del copy[a_char_from]
- a_encrypted.clear()
- a = copy[:a_char_to]
- b = [char_to_move]
- c = copy[a_char_to:]
- d = a + b + c
- a_encrypted.extend(copy[:a_char_to] + [char_to_move] + copy[a_char_to:])
- pass
- def solve(a_password: str, a_operations: list[str]) -> str:
- encrypted = list(a_password)
- for operation in a_operations:
- match operation.split():
- case ['swap', 'position', pos_x, *blah, pos_y]:
- swap_positions(encrypted, int(pos_x), int(pos_y))
- case ['swap', 'letter', char_a, *blah, char_b]:
- swap_letter(encrypted, char_a, char_b)
- case ['rotate', direction, steps, ('steps' | 'step')]:
- rotate_steps(encrypted, int(steps), direction)
- case ['rotate', 'based', *blah, char]:
- rotate_letter(encrypted, char)
- case ['reverse', 'positions', rev_from, 'through', rev_to]:
- reverse_chunk(encrypted, int(rev_from), int(rev_to))
- case ['move', 'position', pos_from, *blah, pos_to]:
- move_char(encrypted, int(pos_from), int(pos_to))
- case _:
- assert 1==0, 'Nah'
- return ''.join(encrypted)
- operations = get_puzzle_input('AOC2016\AOC2016D21.txt')
- password = 'abcdefgh'
- print(f'{solve(password, operations)=}')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement