This is a bit more tricky than usual “easy” puzzles for some languages indeed
I improved on the best published solution by externalizing the functions, and using the new ES2020 nullish coalescing operator, check my solution.
did the same
Thank you!
I had the same issue, and after reading your comment i realized I made the same mistake.
Thanks again!
hi OverCoder,
set a level recursivity to not exceed and store the cell value.
int cellOperation(int indexCell, long level = 0)
{
bool isRef;
int arg1, arg2;
if (value[indexCell] != INT_MIN) return value[indexCell];
arg1 = getValue(args1[indexCell], isRef);
if (isRef)
arg1 = cellOperation(arg1, ++level);
if (operations[indexCell] == "VALUE")
{
if (level >= 10)
value[indexCell] = arg1;
return arg1;
}
arg2 = getValue(args2[indexCell], isRef);
if (isRef)
arg2 = cellOperation(arg2, ++level);
if (operations[indexCell] == "ADD")
{
if (level >= 10)
value[indexCell] = arg1 + arg2;
return arg1 + arg2;
}
if (operations[indexCell] == "SUB")
return arg1 - arg2;
if (operations[indexCell] == "MULT")
return arg1 * arg2;
return 0;
}
};
evaluate func helped me remove 20 lines of code and pass the “accounting is hard 1” test.
thanks.
->SUB $3 $3
it’s a special case
Hi all.
It’s a win to me
Coded in C, with recursion…
at begginning i was thinking to resolve basics cells such as NoRef or Substract to Zero…
But at the end, we have to solve it by recursion…and all with the same method to prevent side effect…
I’m an embedded softwafe engineer. In my domain, recursion is strictly forbidden (like i work with 8ko ram ^^).
i understand principle of recursion, but it’s not easy to “see where it go…”
Thank all for comments; tips… on this thread
That -0 killed me too lol!
All in all I first tried it in Java and got too distracted I think.
A few month later my second attempt was using typescript AND I wanted to try to break it down into small functions (functional programming). That MIGHT have helped me keep it simple and it is oh so simple indeed (once you got the hang to recursion n stuff ;D)!
I’m coding this puzzle using Python.
I’m programming for fun and discovering recursion.
I’ve hit a roadblock in test 08. I don’t have a working test after this one.
I guess I’ll have to use try, except. Is this correct? If so, I’m not sure how to use it… Do you have any advice to help me?
Thanks in advance for your answers.
There are usually multiple ways to solve a puzzle. My solution doesn’t contain try/except, but I guess it’s possible to use them in a working solution.
It’s difficult to comment further on your preliminary guess. It’s better if you actually write some code to test your theory, or at least a more detailed write-up of your theoretical approach for others to comment on.
Has anyone who’s completed this in python got any tips on getting the last three problems? No matter what I try it always fails due to my codes recursion depth being too much for it.
You may try adding a line of code to increase the maximum recursion depth, but there seem to be just one or two published Python solutions (out of the dozens published) which do so.
You may want to read/search this thread to get some hints on how else you may optimise your code.
I did kind of use recursion, but then in a while loop, and not in a function, so I think that is how I avoided the recursion-depth problem. I did have some time-out problems though. My final (successful) approach was to recursively start with a not-reference value, use that value to solve cells referring to that value, and to take any of those fully solved cells as next not-reference value.
I just tried changing the recursion limit and that led me to finding out that my code just isn’t optimized enough so it just times out. I’ll have to try some solutions to further optimize the code and see if that helps otherwise maybe try a new approach.
For those struggling with test cases 11-12 (accounting is hard 1 and 2):
Read the description carefully. It says VALUE can (not must) have a constant value. It can be something like “VALUE $2 _” as well as “VALUE 2 _” .
For test cases 11-12 you need to support “VALUE $i _” and for test case 13 you need memoisation.
How do i solve backward dependency? I am only reaching till fibonacci and then the test fails, I see that it is sorta refering to a value that doesn’t exist yet and i have to solve it when its done, so how do i do it?
Maybe store it somewhere (e.g. in a queue) first, and solve it later when possible?
oh alright, it wasn’t working last time but now it works. But i still get stuck at test 11 (accounting is hard, it gives me the wrong answer)
Maybe some of the hints in the discussion above will help you.