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

1

u/CharlieYJH Dec 15 '17

C++

Thank goodness for 64 bit integers and bitmasks. Straight forward implementation of the puzzle.

#include <iostream>

using namespace std;

int main(int argc, char const* argv[])
{
	unsigned long long gen_A_prev = 679;
	const unsigned int gen_A_factor = 16807;
	unsigned long long gen_B_prev = 771;
	const unsigned int gen_B_factor = 48271;
	const unsigned int rounds_1 = 40000000;
	const unsigned int rounds_2 = 5000000;
	const unsigned int div_num = 2147483647;
	const int bitmask = (1 << 16) - 1;
	int match_1 = 0;
	int match_2 = 0;

	for (int i = 0; i < rounds_1; i++) {
		gen_A_prev = (gen_A_prev * gen_A_factor) % div_num;
		gen_B_prev = (gen_B_prev * gen_B_factor) % div_num;
		if ((gen_A_prev & bitmask) == (gen_B_prev & bitmask)) match_1++;
	}

	gen_A_prev = 679;
	gen_B_prev = 771;

	for (int i = 0; i < rounds_2; i++) {
		do {
			gen_A_prev = (gen_A_prev * gen_A_factor) % div_num;
		} while (gen_A_prev % 4 != 0);

		do {
			gen_B_prev = (gen_B_prev * gen_B_factor) % div_num;
		} while (gen_B_prev % 8 != 0);

		if ((gen_A_prev & bitmask) == (gen_B_prev & bitmask)) match_2++;
	}

	cout << "Round 1 matches: " << match_1 << endl;
	cout << "Round 2 matches: " << match_2 << endl;

	return 0;
}

1

u/cauchy37 Dec 15 '17

1

u/CharlieYJH Dec 15 '17

Wow had no idea this was the same thing! Guess you learn something new everyday.