Skip to content

Instantly share code, notes, and snippets.

@blwsh
Created May 3, 2019 17:58
Show Gist options
  • Save blwsh/300b51890f00b3393480dedb09000e06 to your computer and use it in GitHub Desktop.
Save blwsh/300b51890f00b3393480dedb09000e06 to your computer and use it in GitHub Desktop.
package main
import (
"bufio"
"encoding/csv"
"fmt"
"io"
"log"
"os"
"time"
)
type Log struct {
Columns []LogValue `json:"columns"`
}
type LogValue struct {
Value string `json:"value"`
Difference int16 `json:"difference"`
}
// How far the algorithm should look back when determining difference.
var distance = 20
func main() {
start := time.Now()
file, err := os.Open("./input-lg.csv")
if err != nil {
panic("Unable to read file.")
} else {
// Start reading the CSV
reader := csv.NewReader(bufio.NewReader(file))
// An array with a len no greater than the value of var distance.
var logs []Log
var lookback []Log
for {
line, err := reader.Read()
if err == io.EOF {
break
} else if err != nil {
log.Fatal(err)
}
newLog := Log{}
// For each column, create a log
for key, value := range line {
diff := calculateDifference(key, value, lookback)
// Create the log value
value := LogValue{
Value: value,
Difference: diff,
}
// And add it as a new log item
newLog.Columns = append(newLog.Columns, value)
}
// Add the new log to the logs array
logs = append(logs, newLog)
if len(lookback) >= distance {
lookback = append([]Log{newLog}, lookback[:len(lookback)-1]...)
} else {
lookback = append(lookback, newLog)
}
logs = append(logs, newLog)
}
fmt.Println(logs)
fmt.Println(time.Since(start))
}
}
func calculateDifference(index int, value string, lookback []Log) int16 {
difference := int16(0)
for _, lookbackLog := range lookback {
if lookbackLog.Columns[index].Value == value {
difference = difference + 1
}
}
return difference
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment