Created
December 27, 2022 19:04
-
-
Save harsimranmaan/30f3ccfeb02291ee5b59d3f044548bf4 to your computer and use it in GitHub Desktop.
parse log
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" | |
"strings" | |
) | |
type TPP struct { | |
P1 string | |
P2 string | |
P3 string | |
} | |
func (t TPP) String() string { | |
return fmt.Sprintf("%s-%s-%s", t.P1, t.P2, t.P3) | |
} | |
func (t TPP) Next(s string) TPP { | |
return TPP{P1: t.P2, P2: t.P3, P3: s} | |
} | |
type userVisit struct { | |
user string | |
tpp TPP | |
} | |
func (u userVisit) String() string { | |
return fmt.Sprintf("%s-%s", u.user, u.tpp.String()) | |
} | |
func main() { | |
f, err := os.Open("input.txt") | |
if err != nil { | |
panic(fmt.Sprintf("err reading file %v", err)) | |
} | |
s := bufio.NewScanner(f) | |
m := make(map[string][]string) | |
//lastUserVisit:= make(map[user]TPP) | |
for s.Scan() { | |
line := s.Text() | |
lineItems := strings.Split(line, " ") | |
user := lineItems[1] | |
page := lineItems[2] | |
m[user] = append(m[user], page) | |
} | |
counters := make(map[TPP]int) | |
var max TPP | |
var maxVal int | |
for _, visits := range m { | |
tpp := TPP{} | |
for i, visit := range visits { | |
tpp = tpp.Next(visit) | |
if i < 2 { | |
continue | |
} | |
counters[tpp]++ | |
if counters[tpp] > maxVal { | |
max = tpp | |
maxVal = counters[tpp] | |
} | |
} | |
} | |
fmt.Println(max) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment