def test_part01(self): # Arrange file_path = os.path.join(sys.path[0], "input_sample01.txt") initial_waypoint_offset = Point(10, -1) instructions = build_instructions_from_file(file_path, part02_instruction_factory) initial_nav_state = NavigationState(Point(0, 0), Direction.East, initial_waypoint_offset) # Act final_nav_state = execute_instructions(initial_nav_state, instructions) total_distance = final_nav_state.location.manhatten_distance_from(initial_nav_state.location) # Assert self.assertEqual(total_distance, 286)
value = int(input[1:]) if instruction_type == 'N': return AbsoluteMoveInstruction(Direction.North, value) elif instruction_type == 'S': return AbsoluteMoveInstruction(Direction.South, value) elif instruction_type == 'E': return AbsoluteMoveInstruction(Direction.East, value) elif instruction_type == 'W': return AbsoluteMoveInstruction(Direction.West, value) elif instruction_type == 'F': return ForwardMoveInstruction(value) elif instruction_type == 'L': return TurnInstruction(False, value) elif instruction_type == 'R': return TurnInstruction(True, value) if __name__ == '__main__': file_path = os.path.join(sys.path[0], "input.txt") instructions = build_instructions_from_file(file_path, part01_instruction_factory) initial_nav_state = NavigationState(Point(0, 0), Direction.East, None) final_nav_state = execute_instructions(initial_nav_state, instructions) total_distance = final_nav_state.location.manhatten_distance_from( initial_nav_state.location) print('total_distance', total_distance)
def perform(self, nav_state: NavigationState) -> NavigationState: new_nav_state = nav_state.move_waypoint(self.direction, self.value) return new_nav_state
elif instruction_type == 'S': return AbsoluteWaypointMoveInstruction(Direction.South, value) elif instruction_type == 'E': return AbsoluteWaypointMoveInstruction(Direction.East, value) elif instruction_type == 'W': return AbsoluteWaypointMoveInstruction(Direction.West, value) elif instruction_type == 'F': return MoveTowardWaypointInstruction(value) elif instruction_type == 'L': return TurnWaypointInstruction(False, value) elif instruction_type == 'R': return TurnWaypointInstruction(True, value) if __name__ == '__main__': file_path = os.path.join(sys.path[0], "input.txt") initial_waypoint_offset = Point(10, -1) instructions = build_instructions_from_file(file_path, part02_instruction_factory) initial_nav_state = NavigationState(Point(0, 0), Direction.East, initial_waypoint_offset) final_nav_state = execute_instructions(initial_nav_state, instructions) total_distance = final_nav_state.location.manhatten_distance_from( initial_nav_state.location) print('final location', final_nav_state.location.x, final_nav_state.location.y) print('total_distance', total_distance)