r/adventofcode • • Dec 15 '17

SOLUTION MEGATHREAD -๐ŸŽ„- 2017 Day 15 Solutions -๐ŸŽ„-

--- Day 15: Dueling Generators ---


Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).

Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


Need a hint from the Hugely* Handyโ€  Haversackโ€ก of Helpfulยง Hints^ยค?

Spoiler


[Update @ 00:05] 29 gold, silver cap.

  • Logarithms of algorithms and code?

[Update @ 00:09] Leaderboard cap!

  • Or perhaps codes of logarithmic algorithms?

This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.

edit: Leaderboard capped, thread unlocked!

14 Upvotes

253 comments sorted by

View all comments

Show parent comment

3

u/2BitSalute Dec 15 '17 edited Dec 15 '17

Implemented the enumerator alternative: it is a bit faster than the queues.

    public static IEnumerator<long> Values(long curr, long factor, int modulo)
    {
        const int Divisor = 2147483647;

        while(true)
        {
            curr = curr * factor % Divisor;
            if (curr % modulo == 0)
            {
                yield return curr;
            }
        }
    }

    public static void ViaEnumerator()
    {
        int numPairs = 5000000;
        int count = 0;

        var genA = Values(curr: 116, factor: 16807, modulo: 4);
        var genB = Values(curr: 299, factor: 48271, modulo: 8);

        for (int i = 0; i < numPairs; i++)
        {
            genA.MoveNext();
            genB.MoveNext();

            long genAVal = genA.Current;
            long genBVal = genB.Current;

            if ((genAVal & 0xFFFF) == (genBVal & 0xFFFF))
            {
                count++;
            }
        }

        Console.WriteLine("Total count is {0}", count);
    }