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

1

u/flup12 Dec 15 '17 edited Dec 15 '17

Yups! Lesson learned the hard way, here. Quickly churned out a Stream.iterate solution this morning which kept spending a long time until flooding the memory and going OOM. Battled it for a while, Stackoverflow didn't help me. I gave up and just rewrote into a tail recursive solution to be done with it.

Got home, read this and wrote it back to Iterator.iterate:

def next(factor: Int)(value: Long) = value * factor % 2147483647
def generator(start: Long, factor: Int) = Iterator.iterate(start)(next(factor)).drop(1)
def a = generator(703L, 16807)
def b = generator(516L, 48271)
def judge(a: Long, b: Long): Boolean = a % 65536 == b % 65536
val part1 = a.zip(b).take(40000000).count(judge.tupled)
val part2 = a.filter(_ % 4 == 0).zip(b.filter(_ % 8 == 0)).take(10000000).count(judge.tupled)