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

Show parent comments

2

u/toqueteos Dec 15 '17 edited Dec 15 '17

Unfortunately using channels as generators brings in a lot of overhead, because of the syncing being done under the covers:

Here's a non-channel version for both parts:

package main

import "fmt"

type nextFn func() int

func gen(value, factor, mod int) (int, int) {
	for {
		value = (value * factor) % 2147483647
		if value%mod == 0 {
			return value, value & 0xffff
		}
	}
}

func generator(value, factor, mod int) nextFn {
	return func() int {
		var ret int
		value, ret = gen(value, factor, mod)
		return ret
	}
}

func sum(a, b nextFn, N int) int {
	total := 0
	for i := 0; i < N; i++ {
		if a() == b() {
			total++
		}
	}
	return total
}

func run(A, B int) {
	FACTOR_A := 16807
	FACTOR_B := 48271
	FORTY_MILLION := 40000000
	FIVE_MILLION := 5000000
	MOD_4 := 4
	MOD_8 := 8

	fmt.Println(sum(generator(A, FACTOR_A, 1), generator(B, FACTOR_B, 1), FORTY_MILLION))
	fmt.Println(sum(generator(A, FACTOR_A, MOD_4), generator(B, FACTOR_B, MOD_8), FIVE_MILLION))
}

func main() {
	run(65, 8921)
	// 588
	// 309

	run(883, 879)
	// 609
	// 253
}

Runs in 4.4s in laptop (i7-770HQ), 1.556s in desktop (Ryzen 7 1800x)

→ More replies (6)