Tricky one - make sure you calculate each value on its own
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:
- Generate your own random test cases, not large inputs but just enough to cover several scenarios.
- Write a brute force algorithm to solve them.
- 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?
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.
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%.
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!