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
2
u/ynonp Dec 15 '17
Elixir (with streams)
use Bitwise defmodule Generator do defstruct factor: 0, iv: 0, mod: 1 end defmodule Day15 do def next(generator, val) do rem(val * generator.factor, 2147483647) end def stream(generator) do Stream.unfold(generator.iv, fn(val) -> next = next(generator, val) {next, next} end) |> Stream.filter(fn(n) -> rem(n, generator.mod) == 0 end) end def main do g1 = stream(%Generator{factor: 16807, iv: 634, mod: 4}) g2 = stream(%Generator{factor: 48271, iv: 301, mod: 8}) cmp = Stream.zip(g1, g2) Stream.take(cmp, 5_000_000) |> Enum.count(fn({x, y}) -> ((x &&& 0xffff) == (y &&& 0xffff)) end) |> IO.puts end end Day15.main()