You mentioned that you're not a good programmer, so here's what the code could look like:
- Determine the enemies in range.
- Sort enemies into lists (arrays) based on priority (regular enemies or boss enemy).
- Attack boss enemy if there are no regular enemies.
- Otherwise check which enemy is closest to the exit (check y pos with y pos exits). Then target the enemy closest to the exit.
Note that this only works if the exits are all at the same y position. Otherwise split the list of regular enemies into a list per exit (check if the x pos is equal to the x pos of the exit (probably use a small threshold)). Then check foreach list which enemy is closest to its exit.