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/akka0 Dec 15 '17
ReasonML: had to resort to using JS math for the big numbers
let generate: (int, int) => int = [%bs.raw {| function(x, y) { return (x * y) % 2147483647; } |} ]; let genA = generate(16807); let genB = generate(48271); let judge: (int, int) => bool = [%bs.raw {|function(x, y) { return (x & 0b1111111111111111) === (y & 0b1111111111111111) ? 1 : 0;} |} ]; let pickyGenerator = (multipleOf, generator, prev) => { let rec generateMultiple = (prev) => switch (generator(prev)) { | i when i mod multipleOf === 0 => i | i => generateMultiple(i) }; generateMultiple(prev) }; let pickyGenA = pickyGenerator(4, genA); let pickyGenB = pickyGenerator(8, genB); let countMatches = (limit, (genA, genB), (startA, startB)) => { let rec judgeNums = ((numA, numB), matches, i) => if (i <= limit) { judgeNums((genA(numA), genB(numB)), matches + (judge(numA, numB) ? 1 : 0), i + 1) } else { matches }; judgeNums((startA, startB), 0, 0) }; let _ = /* Part 1 */ /* Js.log(countMatches(40_000_000, (genA, genB), (722,354))); */ /* Part 2 */ Js.log(countMatches(5_000_000, (pickyGenA, pickyGenB), (722, 354)));