program PatternFinder;
uses SysUtils; // IntToStr
const
MAX_ROWS = 7;
MAX_COLS = 9;
PATTERN_SIZE = 3;
MAX_PATTERNS = 100;
type
TPattern = record
Pattern: string;
Rows: array[1..MAX_ROWS] of Integer;
Count: Integer;
end;
var
Matrix: array[1..MAX_ROWS, 1..MAX_COLS] of Integer = (
(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)
);
procedure FindRepeatingPatterns;
var
Patterns: array[1..MAX_PATTERNS] of TPattern;
PatternCount, Row, Col, I, J: Integer;
PatternStr: string;
Found: Boolean;
begin
PatternCount := 0;
for Row := 1 to MAX_ROWS do
begin
for Col := 1 to (MAX_COLS - PATTERN_SIZE + 1) do
begin
PatternStr := '';
for I := 0 to PATTERN_SIZE - 1 do
PatternStr := PatternStr + IntToStr(Matrix[Row, Col + I]) + '-';
Found := False;
for I := 1 to PatternCount do
begin
if Patterns[I].Pattern = PatternStr then
begin
Inc(Patterns[I].Count);
Patterns[I].Rows[Patterns[I].Count] := Row;
Found := True;
Break;
end;
end;
if not Found then
begin
Inc(PatternCount);
Patterns[PatternCount].Pattern := PatternStr;
Patterns[PatternCount].Rows[1] := Row;
Patterns[PatternCount].Count := 1;
end;
end;
end;
Writeln('Repeated Patterns Found:');
for I := 1 to PatternCount do
begin
if Patterns[I].Count > 1 then
begin
Write(Patterns[I].Pattern, ' appears ', Patterns[I].Count, ' times in rows: ');
for J := 1 to Patterns[I].Count do
Write(Patterns[I].Rows[J], ' ');
Writeln;
end;
end;
end;
begin
FindRepeatingPatterns;
end.
(*
run:
Repeated Patterns Found:
1-2-3- appears 5 times in rows: 1 3 4 5 6
2-3-8- appears 2 times in rows: 1 3
1-3-2- appears 2 times in rows: 2 7
7-8-9- appears 3 times in rows: 2 5 7
9-4-5- appears 2 times in rows: 2 6
4-5-6- appears 3 times in rows: 2 5 7
5-6-7- appears 2 times in rows: 5 7
6-7-8- appears 2 times in rows: 5 7
*)