The River I

Thanks. This helped me get the last secret test case.

Friends, my code:

r_1 = int(input())
r_2 = int(input())
#print(r_1, r_2)
n = 10 ** 4
r_1Next = r_1
r_2Next = r_2
i = 0
while i < n: 
    if r_1 < r_2:
        for j in range(len(str(r_1))):
            r_1Next += int(str(r_1)[j])
        r_1 = r_1Next
        i += 1
    elif r_2 < r_1:
        for j in range(len(str(r_2))):
            r_2Next += int(str(r_2)[j])
        r_2 = r_2Next
        i += 1
    elif r_2 == r_1:
        i = n

Why I get only 88%?:frowning:

make that: while r_1 != r_2.
Then delete everything related to i and n and move the print statement out of the loop.

Hey all,
I don’t succeed to pass the last submit validator (88%). Any hint?

long long sumDigits(int n){

int sum = 0;     
while (n != 0) {
    sum += n%10;
    n = n/10;

return sum;

int main()
long long r1;
cin >> r1; cin.ignore();
long long r2;
cin >> r2; cin.ignore();

long long r = r1;    
vector<long long> vals;    

while(r1 != r2){

if(find(vals.begin(), vals.end(), min(r1,r2)) != vals.end()){
    r = min(r1,r2);
r1 += sumDigits(r1);
r2 += sumDigits(r2);
r = r1;        

cout << r << endl;

I ran your code on the last validator @AndreaGiordano


Process has timed out. This may mean that your solution is not optimized enough to handle some cases.

Hint: you can solve it without vector<long long> vals;

1 Like

Oh my god it was so easy…

Is it possible to solve the last one in python?? Because i cannot optimize my program enough

Yes it is easily solvable in python …
It tooks me 5 minutes and only 12 lines to do it …

Could you give me some advise, whatever to help me a bit?

Without knowing what your current method is it’s hard to hint, but the hint given by euler above is probably applicable (that you don’t need to store all the values).

i use one list and two ‘for’ methods to find next r_1 and r_2 and store this values in two lists. Then i compare one list with another. i understand there is an easier way but i’m still thinking

i’m using an infinite loop as well

I’ve solved it finally but still 88%
idk how to make it better

My advice would be to solve it on paper. Write what your program does and compare it with how you would solve it without code. Perhaps you’re doing too many operations.

also @dwarfie

I’m not sure what’s the goal of your post beside making @Denya feel miserable. :thinking:

this was not the case, the question implied that the puzzle could be insolvable in python with its solution that could no longer be optimized.
I just wanted to make it appear that not only it was possible but that he should rather have a different approach …

I probably should not answer or say “put your code in the trash you did anything” rather than answer the question …

the real problem is questions that does not mean anything … how to help when the post is “that does not work”, “that’s insolvable” or (my favourite) “there’s a bug in the puzzle” …
i helped 2 other people yesterday on chat with that kind of question and i did the same way and none were offended by my comments

1 Like

in my experience, people don’t often realize how badly can readers take their written comments; even if you didn’t want to convey a negative impression. Some people won’t get offended, others will.

Here’s a piece of advice on how to help people who write such comments:

  • ask them to write what they tried to solve the puzzle so we can help them understand the issue in their reasoning
  • take a guess, and tell them about the classic mistakes people make in this puzzle
  • offer another way to think about the puzzle (what I did above)


huh, finally i’ve really done it(hint about not storing all the values was perfect)Thanks for help!

1 Like

Hello there!

I solved the puzzle in a fairly straight forward manner, it passed all the tests in a breeze, however, when I submitted the code, it failed the 8th validator test, called “Primes”. It does pass the “Primes” test in the IDE though. I did some further testing with other primes combinations and it works just fine:

1004 & 1013 results in 1114
10069 & 10079 results in 11117
10391 & 12007 results in 12028

Evidently testing all prime number combinations would be impossible. I don’t have a supply of prime rivers that are guaranteed to meet either (I googled for it lol), and the prime cases I have tested work just fine, so I don’t really know how to go about testing this to get it to 100%, any help would be greatly appreciated!

The straight-forward manner is probably not performant enough and your program timeouts on Validator 8. There is a “trick” to avoid unnecessary computations. Perhaps you can share what’s the logic of your code.

Your comment prompted me to check for performance issues and unnecesary calculations. A slight optimization on sum of the digits did the trick. Thanks a lot for the help!

1 Like