[Community Puzzle] Target Firing

Tricky one - make sure you calculate each value on its own :wink:

This is a true puzzle not just a simple coding exercise. Not so sure if it should be in the hard category but I think it should have a Mathematics tag in the specification.

I won’t give any specific hints but a general one that works in real life:

  1. Generate your own random test cases, not large inputs but just enough to cover several scenarios.
  2. Write a brute force algorithm to solve them.
  3. Finally analyze your results to reveal the relationships between your entities in order to find an optimized algorithm.

There’s no reason to do steps 1 and 2 tbf, simply take a pen and paper and consider 2 different ships : figure out which one you should take out first and why, analytically. The formula is very simple once you find it.

There is no reason to do any of the 3 unless you’re stuck. Those 3 are general rules that you should be applying in your job once you’re stuck with a problem. Its complexity doesn’t matter. You should also be practising writing your own unit test cases.

“The formula is very simple once you find it.” You have a paradox right there.

This may be a silly question, but is there a name for this formula? I had the pieces for it but was not organizing it in the right way until I came across your post.

Once you have the formula, it happens to be very simple. I’m not sure we have the same definition of a paradox.

By your context your paradox is:
“There’s no reason to do steps 1 and 2 tbf, simply…”
and you continue: “This formula is simple once you find it.”
You’re implying by your context that it’s simple to figure out the formula because once you find it’s simple.

That does sound like a paradox, and if it’s not what makes you think what you wrote is an absolute truth for everyone? Do you think that everyone in this thread can find all possible test cases on a piece of paper? Even if so, don’t you think that most of them would have done so before they came to this thread for help?

If you haven’t found the solution already:
Correct a DFS will of course provide the correct results and an optimization is needed. Study the results of your DFS algorithm and analyze the relationships derived by the ship attributes in each selection. The pruning method will hopefully be revealed to you, in fact you will be surprised how much pruning you can do.

Edit: Since the puzzle’s provided test cases are (cleverly) either too large/too small/too dense will probably not be of much help, you might/need/should write your own test cases with 4-5 ships with variant attribute measures.

You only need 2 different ships to help you find the formula analytically, and a few more to verify that it’s correct. Paradox, absolute truth, wth are you talking about ??

Don’t get mad my friend. In order to realize the simplicity of the solution first one needs to discover it themselves even by taking the long way which is what is decided by themselves. There are always people that can see a solution intuitively the rest need to take the long way that’s what these practise puzzles are for. Take it easy.

Frankly I don’t see any benefit in these forums where code is forbidden but spilling the beans is not. Even the condescending tones of the style “oh it’s so easy why don’t you see it” “oh if you don’t see it then forget it” are not much of help. Have fun while you learn and teach.

I have to agree with Djoums here.

Generating a bunch of examples and trying to see patterns might help you find the solution of a problem once in a while but it won’t make you improve your solving skills for the following reasons:

  • it’s the best way of skipping the interesting part of the solution: you find it but don’t understand it

  • it won’t help you build your confidence up in your ability to solve problems (you won’t feel clever, you’ll only feel lucky); and confidence is probably 50% of what problem solving is

  • if you’re stuck on such an easy problem it doesn’t mean you’re dumb at all, but it means there’s something wrong in your way of approaching problems; mostly, you don’t ask yourself the good questions, you don’t follow obvious leads cause you’re not confident enough about your ability of dealing with them; if you never work on these difficulties you have, you’ll stagnate

Better ideas if you’re stuck would be:

  • try to solve an easier version of the problem ; it provides good leads for the actual problem, sometimes it even contains the essence of the problem and after solving the easy version you’re pretty much done; and anyway it builds your confidence up, which is always good

  • pen and paper, write all you got

  • try different approaches: analytic, visual, temporal, etc

  • sleep on it, there’s a chance you’re just tired, stuck in the wrong direction somehow, and if you try again some days later it will appear obvious

I’ll give you another example:
Famous math problem: what’s the sum 1 + 2 + 3 + … + n ?

Imagine it’s the first time you see this problem.

With your approach, you calculate the sum for small examples, you see a pattern, it seems the formula is n(n+1)/2.
You can now demonstrate it recursively if you want.
The problem looks solved.

But is it really ?

Well let’s find out, new problem: what’s the sum 1^2 + 2^2 + 3^2 + … + n^2 ?
Ok so let’s calculate the sum for small examples, there must be a pattern, there must be…
… and now good luck to find out the pattern cause the formula is n(n+1)(2n+1)/6 so it’s pretty unlikely you guess it from small values.
So now you’re definitively stuck.
You give up.
And approach you next problem with even less confidence.

But now what if you solved the first problem differently ?
There are many ways to solve it but let’s say for example that you try a visual approach:
O
OO
OOO
OOOO
OOOOO
thus you realize it all comes to counting dots in a triangle, and the formula just comes easily.

With all this confidence you try the second problem, you then try a similar visual approach, realize it all comes to counting dots in a pyramid, and now you have a chance to actually find the formula.

Also there’s a chance you remember these formulas way better if you actually “see” them, and even if you don’t remember them you’d be able to retrieve them pretty fast.

The examples of taking different approaches here are very useful and will help others so that’s great. But you cannot approach something safely if you don’t have enough test cases. What if you don’t have experience on this problem category? What if you didn’t study game theory or finance theory and cannot see the hidden property?

A way to get a grasp on a problem is to be aware of your test cases, this is a necessary step (not guaranteed see the other steps) and start looking at the test cases you might be missing or have a misunderstanding.

Your square num series here is basically a perfect example of already knowing your test cases. There is no need to find the test cases you already have them it’s all a matter of analysis either visualize, draw it on a board draw it on a napkin, nothing wrong with that. That is 3rd step I wrote above.

Now, can you say the same about the target firing puzzle? In order to get to that analysis part 3 you have to be aware of those cases. You can start drawing them in paper if you’re analytical enough you’ll get it but all it takes a little bit of biased from your side and a little bit carried away by a well crafted puzzle and you’re down the long path. This is a definition of a puzzle it’s meant to be canny.

For puzzles you’re stuck because you think you understood it the generation of random cases will show you what you’re missing and dissolve your preconceptions and go “pff, now I see it why did I think that was such and such”. A validation routine will just work the same magic you do on paper either for a simple puzzle or a more laborious enough so yeah save a tree (no, not those ones the real ones).

After all this bean spilling, I mean some even wrote about rounding the division, why people still don’t get it? It’s because the puzzle wording and the test cases provided are cleverly misleading to an elaborate solution. Again you need to be aware of your test cases.

I suggest some reading on test driven development. Also as another side note, there is nothing wrong with a little test automation and a validation routine to get you practising on your test skills not to mention it shows quality work and professionalism.

This argument of which opinion is more valid is starting to smell and unconstructive, pals. Fight outside the bar or just accept different opinions can coexist, please?

We all need a bit of internet fight once in a while :smiley:

1 Like

Hello CondinGames community , i am new to the site and new to the programming community, i have a question to this particular Puzzle, the output should be always our HP , or FLEE, but are we in charge of removing the alien ship from our Data Structure if the alien ship hp is lesser than zero?

Thank you.

Data structure exists in your own codes during runtime. You are in total control and bear total responsibility of your codes. So that, during runtime, you can delete, alter or add any data to your own structure if you think fit.

1 Like

Thanks @UnicordP - this was fun!

Like a few others, I started with DFS and tried to optimise. It took me far too long to notice that one approach (sorting the ships the right way) found the best answer first time. Could have saved hours of unnecessary attempts if I’d noticed. D’oh!

I know this is very late, but I just started this puzzle recently and came up with this exact same logic/formula. I was stuck for 2 days on the exact same Test Cases that d0mpsey mentioned trying to figure out how my logic was flawed and I finally figured it out. This formula basically calculates the amount of damage you would prevent if you killed that ship, but it does NOT factor in the fact that you might be able to kill two smaller ships in the same amount of time as killing the one larger ship, in which case the formula needs to account for the SUM of both smaller ships instead of just a one-to-one comparison.

I won’t give away exact details to not spoil the solution for anyone, but if someone came across the exact same scenario/logic as D0mpsey and myself, you should be able to make a minor adjustment to your formula and it will then pass 100%.

1 Like

It is late but its okay! I did end up giving up in the end as I jus couldn’t figure this out. You’ve said enough here for me to realise that it might actually be the solution. It just makes sense. I’m 100% going back to this tonight and finishing it up and giving it another crack. TYVM!

Genuinely, thanks!

1 Like