Advertisement
bob_f

AOC2020D12.py

Oct 4th, 2024
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.35 KB | None | 0 0
  1.  
  2. def get_puzzle_input(a_file_name: str) -> list[str]:
  3.     with open(a_file_name) as INFILE:
  4.         return [line.rstrip() for line in INFILE if not line.startswith('#')]
  5.  
  6. def get_new_facing(a_currently_facing: str, a_turn_direction: str, a_degrees: int) -> str:
  7.     TURN_LIST = 'NESW' * 3
  8.  
  9.     currently_facing_idx = TURN_LIST.find(a_currently_facing, 4)
  10.     number_of_turns = a_degrees // 90
  11.  
  12.     if a_turn_direction == 'L':
  13.         number_of_turns *= -1
  14.  
  15.     return TURN_LIST[currently_facing_idx + number_of_turns]
  16.  
  17. def get_new_pos(a_direction: str, a_pos: int, a_distance: str) -> int:
  18.     ret: int = 0
  19.  
  20.     match a_direction:
  21.         case 'N':
  22.             ret = a_pos + a_distance
  23.         case 'S':
  24.             ret = a_pos - a_distance
  25.         case 'E':
  26.             ret = a_pos + a_distance
  27.         case 'W':
  28.             ret = a_pos - a_distance
  29.  
  30.     return ret
  31.  
  32. def solve_p1(a_instructions: list[str]) -> int:
  33.  
  34.     manhatan: int = 0
  35.     x_coord: int = 0
  36.     y_coord: int = 0
  37.     facing = 'E'
  38.  
  39.     for instruction in a_instructions:
  40.         move_direction = instruction[0]
  41.         move_distance = int(instruction[1:])
  42.  
  43.         match move_direction:
  44.             case 'N':
  45.                 y_coord = get_new_pos('N', y_coord, move_distance)
  46.             case 'S':
  47.                 y_coord = get_new_pos('S', y_coord, move_distance)
  48.             case 'E':
  49.                 x_coord = get_new_pos('E', x_coord, move_distance)
  50.             case 'W':
  51.                 x_coord = get_new_pos('W', x_coord, move_distance)
  52.             case 'L':
  53.                 facing = get_new_facing(facing, 'L', move_distance)
  54.             case 'R':
  55.                 facing = get_new_facing(facing, 'R', move_distance)
  56.             case 'F':
  57.                 match facing:
  58.                     case 'N':
  59.                         y_coord = get_new_pos('N', y_coord, move_distance)
  60.                     case 'S':
  61.                         y_coord = get_new_pos('S', y_coord, move_distance)
  62.                     case 'W':
  63.                         x_coord = get_new_pos('W', x_coord, move_distance)
  64.                     case 'E':
  65.                         x_coord = get_new_pos('E', x_coord, move_distance)
  66.  
  67.     return abs(x_coord) + abs(y_coord)
  68.  
  69. instructions = get_puzzle_input('aoc_2020/aoc_2020_day_12.txt')
  70. # print(f"{instructions=}")
  71.  
  72. print(f"{solve_p1(instructions)=}")    
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement