package main
import (
"fmt"
"strings"
)
func findRepeatingPatterns(matrix [][]int, patternSize int) {
patternMap := make(map[string]map[int]bool)
for rowIndex, row := range matrix {
for col := 0; col <= len(row)-patternSize; col++ {
patternSlice := row[col : col+patternSize]
pattern := strings.TrimSuffix(strings.Join(strings.Fields(fmt.Sprint(patternSlice)), "-"), "")
if _, exists := patternMap[pattern]; !exists {
patternMap[pattern] = make(map[int]bool)
}
patternMap[pattern][rowIndex] = true
}
}
fmt.Println("Repeated Patterns Found:")
for pattern, rows := range patternMap {
if len(rows) > 1 {
fmt.Printf("%s appears %d times in rows: ", pattern, len(rows))
for row := range rows {
fmt.Printf("%d ", row)
}
fmt.Println()
}
}
}
func main() {
matrix := [][]int{
{1, 2, 3, 8, 9, 7, 4, 9, 6},
{1, 3, 2, 7, 8, 9, 4, 5, 6},
{1, 2, 3, 8, 6, 1, 4, 9, 8},
{1, 2, 3, 0, 8, 8, 4, 5, 9},
{1, 2, 3, 4, 5, 6, 7, 8, 9},
{1, 2, 3, 7, 0, 9, 4, 5, 7},
{1, 3, 2, 4, 5, 6, 7, 8, 9},
}
patternSize := 3 // Looking for sequences of 3 numbers
findRepeatingPatterns(matrix, patternSize)
}
/*
run:
Repeated Patterns Found:
[1-2-3] appears 5 times in rows: 0 2 3 4 5
[6-7-8] appears 2 times in rows: 4 6
[2-3-8] appears 2 times in rows: 0 2
[9-4-5] appears 2 times in rows: 1 5
[4-5-6] appears 3 times in rows: 1 4 6
[1-3-2] appears 2 times in rows: 1 6
[7-8-9] appears 3 times in rows: 1 4 6
[5-6-7] appears 2 times in rows: 4 6
*/