I’m starting to doubt my abilities as a programmer as more and more of the problems’ second parts are winning over me.

I reused most of the code I did in problem number 16 for this problem. It helped a lot for the first part of the problem. The only difference is that I set the instruction pointer to the correct register before starting the execution of the program.

This worked well for the first part of the problem, but the simple change of starting with [1, 0, 0, 0, 0, 0] instead of [0, 0, 0, 0, 0, 0] for the second part made my program take a lot longer to execute. After a couple of vain attempts at optimization, I decided to go look on the advent subreddit, I found out most people reverse engineered their code into a simpler format.

After watching the register during the execution and the code for my program, I started out with this code:

long r0 = 0;
long r1 = 10551383;
long r2 = 1;
long r4;

do
{
    r4 = 1;
    do
    {
        if ((r2 * r4) == r1) r0 += r2;
        r4++;
    }
    while (r4 <= r1);
    r2++;
}
while (r2 <= r1);
return r0.ToString();

But even this took a while to compute, so I analyzed the inner loop a bit more and found out it’s just checking if there is a factor of r1/r2 and adds it to r0. I simplified the code to this and found my solution with it:

 long r0 = 0;
 const int r1 = 10551383;
 long r2 = 1;

do
{
    var r4 = (decimal) r1 / r2;
    if (r4 == Math.Floor(r4)) r0 += r2;
    r2++;
}
while (r2 <= r1);
return r0.ToString();

Finding the solution helped me regain a little confidence that I can solve the other problems I skipped previously with some similar work.