fun findRepeatingPatterns(matrix: List<List<Int>>, patternSize: Int) {
val patternMap = mutableMapOf<String, MutableSet<Int>>()
matrix.forEachIndexed { rowIndex, row ->
for (col in 0..row.size - patternSize) {
val pattern = row.subList(col, col + patternSize).joinToString("-")
patternMap.getOrPut(pattern) { mutableSetOf() }.add(rowIndex)
}
}
println("Repeated Patterns Found:")
patternMap.forEach { (pattern, rows) ->
if (rows.size > 1) {
println("$pattern appears ${rows.size} times in rows: ${rows.joinToString(" ")}")
}
}
}
fun main() {
val matrix = listOf(
listOf(1, 2, 3, 8, 9, 7, 4, 9, 6),
listOf(1, 3, 2, 7, 8, 9, 4, 5, 6),
listOf(1, 2, 3, 8, 6, 1, 4, 9, 8),
listOf(1, 2, 3, 0, 8, 8, 4, 5, 9),
listOf(1, 2, 3, 4, 5, 6, 7, 8, 9),
listOf(1, 2, 3, 7, 0, 9, 4, 5, 7),
listOf(1, 3, 2, 4, 5, 6, 7, 8, 9)
)
val 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
2-3-8 appears 2 times in rows: 0 2
1-3-2 appears 2 times in rows: 1 6
7-8-9 appears 3 times in rows: 1 4 6
9-4-5 appears 2 times in rows: 1 5
4-5-6 appears 3 times in rows: 1 4 6
5-6-7 appears 2 times in rows: 4 6
6-7-8 appears 2 times in rows: 4 6
*/