Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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