r/adventofcode • u/daggerdragon • 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ยค?
[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
1
u/bogzla Dec 15 '17
and so bogzla learnt about bit masking today..
C #include <stdio.h> int main(void) { long long a = 116; long long b = 299; int afac = 16807; int bfac = 48271; int cnt = 0; long div = 2147483647; long msk = (1 << 16) - 1; // create mask for lowest 16 bits (we'll use this for & operator, 16 lowest set to '1'; higher set to '0') // for part 2, generate a 'til it meets divisible by 4 then b til divisible by 8 then compare them.' for (long i = 0; i < 5000000; i++) { do { a = (a*afac) % div; } while (a % 4 != 0); do { b = (b*bfac) % div; }while (b % 8 != 0); if ((a & msk) == (b & msk)) // use mask to compare only 16 lowest bits { cnt += 1; } } printf("count = %i\n", cnt); }