Advertisement
bob_f

AOC2016D21.py

Oct 26th, 2023 (edited)
780
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.80 KB | None | 0 0
  1. from collections import deque
  2.  
  3. def get_puzzle_input(a_file_name) -> list[str]:
  4.     with open(a_file_name) as INFILE:
  5.         return [line.rstrip() for line in INFILE if not line.startswith('#')]
  6.    
  7. def swap_positions(a_encrypted: list[str], a_x:int, a_y:int):
  8.     a_encrypted[a_x], a_encrypted[a_y] = a_encrypted[a_y], a_encrypted[a_x]
  9.  
  10. def swap_letter(a_encrypted: list[str], a_char_a:str, a_char_b:str):
  11.     char_a_pos = a_encrypted.index(a_char_a)
  12.     char_b_pos = a_encrypted.index(a_char_b)
  13.     swap_positions(a_encrypted, char_a_pos, char_b_pos)
  14.  
  15. def rotate_steps(a_encrypted: list[str], a_steps:int, a_direction:str):
  16.     d_encrypted = deque(a_encrypted)
  17.     d_encrypted.rotate(int(a_steps) if a_direction == 'right' else -int(a_steps))
  18.     a_encrypted.clear()
  19.     a_encrypted.extend(list(d_encrypted))
  20.  
  21. def reverse_chunk(a_encrypted: list[str], a_from:int, a_to:int):
  22.     reverse_chunk = a_encrypted[a_from:a_to + 1][::-1]
  23.     encrypted = a_encrypted[:a_from] + reverse_chunk + a_encrypted[a_to + 1:]
  24.     a_encrypted.clear()
  25.     a_encrypted.extend(encrypted)
  26.     pass
  27.  
  28. def rotate_letter(a_encrypted: list[str], a_char:str):
  29.     char_index = a_encrypted.index(a_char)
  30.     steps_to_rotate = 1 + char_index
  31.     steps_to_rotate += 1 if char_index > 3 else 0
  32.     rotate_steps(a_encrypted, steps_to_rotate, 'right')
  33.  
  34. def move_char(a_encrypted: list[str], a_char_from:int, a_char_to:int):
  35.     copy = a_encrypted.copy()
  36.     char_to_move = copy[a_char_from]
  37.     del copy[a_char_from]
  38.     a_encrypted.clear()
  39.  
  40.     a = copy[:a_char_to]
  41.     b = [char_to_move]
  42.     c = copy[a_char_to:]
  43.     d = a + b + c
  44.  
  45.     a_encrypted.extend(copy[:a_char_to] + [char_to_move] + copy[a_char_to:])
  46.     pass
  47.  
  48. def solve(a_password: str, a_operations: list[str]) -> str:
  49.     encrypted = list(a_password)
  50.  
  51.     for operation in a_operations:
  52.         match operation.split():
  53.             case ['swap', 'position', pos_x, *blah, pos_y]:
  54.                 swap_positions(encrypted, int(pos_x), int(pos_y))
  55.             case ['swap', 'letter', char_a, *blah, char_b]:
  56.                 swap_letter(encrypted, char_a, char_b)
  57.             case ['rotate', direction, steps, ('steps' | 'step')]:
  58.                 rotate_steps(encrypted, int(steps), direction)
  59.             case ['rotate', 'based', *blah, char]:
  60.                 rotate_letter(encrypted, char)
  61.             case ['reverse', 'positions', rev_from, 'through', rev_to]:
  62.                 reverse_chunk(encrypted, int(rev_from), int(rev_to))
  63.             case ['move', 'position', pos_from, *blah, pos_to]:
  64.                 move_char(encrypted, int(pos_from), int(pos_to))
  65.             case _:
  66.                 assert 1==0, 'Nah'
  67.  
  68.     return ''.join(encrypted)
  69.  
  70. operations = get_puzzle_input('AOC2016\AOC2016D21.txt')
  71. password = 'abcdefgh'
  72.  
  73. print(f'{solve(password, operations)=}')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement