Skip to content

Instantly share code, notes, and snippets.

@jargnar
Last active December 9, 2018 14:09
Show Gist options
  • Save jargnar/cbb0ed442303625b08174eb87b1944b2 to your computer and use it in GitHub Desktop.
Save jargnar/cbb0ed442303625b08174eb87b1944b2 to your computer and use it in GitHub Desktop.
Find duplicated accumulator from a stream
package main
import (
"bufio"
"fmt"
"os"
"strconv"
"github.com/AndreasBriese/bbloom"
)
func duplicatedFrequency(frequencies []string) string {
var register int
var registerStr string
bf := bbloom.New(float64(1<<18), float64(0.001))
for {
for _, freq := range frequencies {
val, _ := strconv.Atoi(freq)
register = register + val
registerStr = strconv.Itoa(register)
if bf.Has([]byte(registerStr)) {
return registerStr
}
bf.Add([]byte(registerStr))
}
}
}
func main() {
fp, _ := os.Open("01.txt")
defer fp.Close()
var frequencies []string
fileScanner := bufio.NewScanner(fp)
for fileScanner.Scan() {
frequencies = append(frequencies, fileScanner.Text())
}
dup := duplicatedFrequency(frequencies)
fmt.Println(dup)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment