Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def get_puzzle_input(a_file_name: str) -> list[str]:
- with open(a_file_name) as INFILE:
- return [line.rstrip() for line in INFILE if not line.startswith('#')]
- def get_new_facing(a_currently_facing: str, a_turn_direction: str, a_degrees: int) -> str:
- TURN_LIST = 'NESW' * 3
- currently_facing_idx = TURN_LIST.find(a_currently_facing, 4)
- number_of_turns = a_degrees // 90
- if a_turn_direction == 'L':
- number_of_turns *= -1
- return TURN_LIST[currently_facing_idx + number_of_turns]
- def get_new_pos(a_direction: str, a_pos: int, a_distance: str) -> int:
- ret: int = 0
- match a_direction:
- case 'N':
- ret = a_pos + a_distance
- case 'S':
- ret = a_pos - a_distance
- case 'E':
- ret = a_pos + a_distance
- case 'W':
- ret = a_pos - a_distance
- return ret
- def solve_p1(a_instructions: list[str]) -> int:
- manhatan: int = 0
- x_coord: int = 0
- y_coord: int = 0
- facing = 'E'
- for instruction in a_instructions:
- move_direction = instruction[0]
- move_distance = int(instruction[1:])
- match move_direction:
- case 'N':
- y_coord = get_new_pos('N', y_coord, move_distance)
- case 'S':
- y_coord = get_new_pos('S', y_coord, move_distance)
- case 'E':
- x_coord = get_new_pos('E', x_coord, move_distance)
- case 'W':
- x_coord = get_new_pos('W', x_coord, move_distance)
- case 'L':
- facing = get_new_facing(facing, 'L', move_distance)
- case 'R':
- facing = get_new_facing(facing, 'R', move_distance)
- case 'F':
- match facing:
- case 'N':
- y_coord = get_new_pos('N', y_coord, move_distance)
- case 'S':
- y_coord = get_new_pos('S', y_coord, move_distance)
- case 'W':
- x_coord = get_new_pos('W', x_coord, move_distance)
- case 'E':
- x_coord = get_new_pos('E', x_coord, move_distance)
- return abs(x_coord) + abs(y_coord)
- instructions = get_puzzle_input('aoc_2020/aoc_2020_day_12.txt')
- # print(f"{instructions=}")
- print(f"{solve_p1(instructions)=}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement