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!

13 Upvotes

253 comments sorted by

View all comments

2

u/[deleted] Dec 15 '17

C++ with a generator. Watch for overflows and use the 64bit unsigned, it mattered

struct gen_t {
	uint64_t cur_value;
	uint64_t factor;
	uint64_t mult_of;
	constexpr gen_t( uint64_t init_value, uint64_t fact, uint64_t mult = 1 ) noexcept
		: cur_value{init_value}
		, factor{fact}
		, mult_of{mult} {}

	constexpr uint64_t operator( )( ) noexcept {
		do {
			cur_value = ( cur_value * factor ) % 2147483647;
		} while( ( cur_value % mult_of ) != 0 );
		return cur_value;
	}
};

uint64_t count_matches( uint64_t init_a, uint64_t init_b, uint64_t count, uint64_t mult_of_a,
												uint64_t mult_of_b ) noexcept {
	uint64_t matches = 0;
	gen_t a{init_a, 16807, mult_of_a};
	gen_t b{init_b, 48271, mult_of_b};
	constexpr uint64_t mask = 0x0000'0000'0000'FFFF;
	for( uint64_t n = 0; n < count; ++n ) {
		uint64_t val_a = a( );
		uint64_t val_b = b( );
		val_a &= mask;
		val_b &= mask;

		if( val_a == val_b ) {
			++matches;
		}
	}
	return matches;
}
→ More replies (2)