# [Community Puzzle] Target Firing

https://www.codingame.com/training/easy/target-firing

Created by @UnicornP,validated by @Schwase,@VirtualAtom and @ludowsky.
If you have any issues, feel free to ping them.

Nice little challenge, finding a good way to prioritize the ships was interesting.

1 Like

Iâm having a hard time sorting the ships to attack first.

I tried to rank the ships by the more damage they are able to make while being shot at, but it doesnât take into account the fact that every ship deals damage each turn. Thus, my ship shoots at the most hazardous ship first enven if it would be safer to take down smaller ships first.

I donât know if thatâs because Iâm missing something here, but the puzzle seems significantly harder than every other âeasyâ puzzles to me. Am I the only one to think this way?

I treated it as an Economics problem.

Perhaps not many IT people studied Economics , so explaining it a bit.

There is a Micro-Economics concept, âopportunity costâ. Briefly, because time and resources are limited, if you choose option A, you loose the opportunity to follow option B, or C, or DâŚ
A commonly quoted example is that If you choose to go to college to earn a degree, your opportunity cost could be losing 4 years of working experience plus salaries.

In game playing, if you choose to shoot down a big ship in 4 rounds, you earn the benefits of removing a big threat. You lose the opportunity of shooting down four smaller ships in each of the rounds (removing several smaller threats). You have to evaluate the benefits and its opportunity costs to make a decision.

Forget it if you find it is too hard to digest.

4 Likes

I understand the way to think, but I canât think of a way to code this nicelyâŚ

If Iâm dealing with a large ammount of ships, I donât know how explicitely tell if itâs better to focus on one âbig shipâ or to take down certain smaller ships. Everytime I start to write something, I end up making tons of comparisonsâŚ

options benefits
Option A Remove Âź threat of a big ship
Option B Remove Â˝ threat of a medium ship
Option C Remove full threat of a small ship

âThreatâ is a variable different in each case. Choosing A, B, or C - depends on var values in your ship list.

1 Like

You want to find a metric that gives you a value for each ship, and then sort the ships based on their respective value. The key is to find how to build that metric, but once you have it the rest is trivial.

3 Likes

Thanks to you two for your help! I did not find a solution yet but Iâm still trying.

Djoums, thatâs what I tried to do by taking into account the shipsâ attack and endurance. But I think my model isnât complex enough and my program often makes the wrong decision.

Constraints say HP, ARMOR, DAMAGE are >0. That is not the case.

Say you must choose between target A and B.
Letâs call T the number of turns to kill a target and D the damage a target deals each turn.
If you kill A before B:
You take a total of TAĂDA +(TA+TB)ĂDB damage.
If you kill B before A:
You take a total of TBĂDB +(TA+TB)ĂDA damage.
So which target do you choose?

5 Likes

An important part of the metric is how many turns it takes to kill a ship.
Because the more damage per turn a ship deals, the more you want to prioritize it. But the more turns it takes to kill it, the less interesting it becomes.

1 Like

Yup I understand on which elements the comparison must be focused, but the issue is that there might be a lot of ships to compare, thus I canât use the other ships in the comparison formula (because Iâm affraid of doing tons and tons of comparisons).

I guess Iâll eventually find a way to achieve 100%, Iâm stuck at 75% atm, Iâm having troubles with the swarm and close call!

EDIT: I got to 100%, but I donât think I got there in a very elegant way!

2 Likes

You donât have to compare each pair of targets, you can just set the best target to be the first one, then iterate over the targets and update the best one if you find better.
And you do that at every turn.

A more efficient way would be to sort all targets from the beginning with a custom comparison function/key.

1 Like

I noticed multiple strategies being discussed, so let me just leave this here:

There exists a purely mathematical solution to prioritising which ship to damage first. So instead of playing about with weights for the 2 factors (time to kill and health points), you could try deriving this formula. Itâs a very simple factor, and playing around with 3 or 4 ships should make it evident.

Good Luck

I managed to do this by giving each ship a score. The score is proportional to the damage of the ship and the turns required to kill it. The higher the turns the lower the score is.

1 Like

Well if you look at my previous formula, you notice that it all comes to checking if TA * DB < TB * DA which is equivalent to TA / DA < TB / DB.
From this, the sorting key becomes obvious.

How can I get the solution to this puzzle?

This is quite often a dilemma in many games: âDo I kill the strong or the weak units first?â
As it turns (no pun intended) out itâs not that simple. (Although this is quite often a solution you see implemented with weak AI bots, even in AAA games.)
A good comparison function for pairs of targets is to compare targets by the total damage you take if you chose to attack the other unit. Meaning, you need to see what is worse for you: if you focus on enemy A and let enemy B attack you until youâve killed A or if you focus on enemy B and let enemy A attack you while you deal with B.
This way you deal with targets in optimal way (HP-wise).
This is only one of the ideas you could use to solve this puzzle.

Hi all,
I am new to CodinGame, and was wondering how I can go about seeing the solutions. I have tried everything I can think of to solve it - and I would like to learn how to fix these kind of problems. I am happy to forfeit my ability to âsolveâ the puzzle in order to learn
Thanks

AFAIK thatâs not a feature on coding game, you can only see others solutions once youâve passed the validators.