Last active
December 9, 2018 14:09
-
-
Save jargnar/cbb0ed442303625b08174eb87b1944b2 to your computer and use it in GitHub Desktop.
Find duplicated accumulator from a stream
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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