// ,  
init_arr %hod (1) 645, 206 1331506      //  
init_arr %LevVerh (1) 649, 259     //   

//   
init_arr %imya (1)  6 10676223
init_arr %imya (2)  5 403967
init_arr %imya (3) Ƹ 3 6680575
init_arr %imya (4)  1 10805161
init_arr %imya (5)  1 11322314

set #wait 700    // ,   ,    

set #workwin 0   // 0 -   , 1-  

if  #workwin = 0
    set #handle 2
    init_arr %workwindow (1) 0 0             //  !!
else
    init_arr %workwindow (1) 0 0           //      
    set #handle findwindow ()
end_if

gosub begin

:start
gosub poisk                   //  
gosub line3                   //   3,   3
gosub showresult              //    
gosub hod                     //  

// 
left #x1 #y1 5 5 5 5
wait 200
left #x2 #y2 5 5 5 5
wait 200
left 133, 575 20 20 20 20     //    ,      
wait 2000
goto start
End_script

:begin
set linedelay 0
if  workwindow = 0
    log    
    End_script
end_if
log open
wait 100
set windowpos 0 0 500 400 loghandle
log clear
log mode compact


set $regexp3_1 "(\d)\1"
set $regexp3_2 "(\d)\d\1(?!\1)"

set #startX %LevVerh [1 1] + %workwindow [1 1]
set #startY %LevVerh [1 2] + %workwindow [1 2]
set #stepX 41
set #stepY 41
set #endX #startX + #stepX * 5
set #endY #startY + #stepY * 5
set #hodx %hod [1 1] + %workwindow [1 1]
set #hody %hod [1 2] + %workwindow [1 2]
return

:poisk
set size(%left)
set size(%left2)
set #result 0
if  #workwin = 0
    while_not #hodx #hody %hod [1 3]    //    
        wait 100
    end_while
else
    while_not color (#hodx #hody #handle) = %hod [1 3]    //   
        wait 100
    end_while
end_if
wait 300

//  
set #a findcolor (#startX #startY #endX #endY #stepX #stepY (%imya [1 3] %imya [2 3] %imya [3 3] %imya [4 3] %imya [5 3]) %arr #handle)
//save_array %arr C:\Setkafind.txt
if  #a > 0
    set #z 0
    for #i 1 6
        for #j 1 6
            set #z #z + 1
            switch %arr [#z 3]
                case %imya [1 3]: set %a [#i #j] 1
                    break
                case %imya [2 3]: set %a [#i #j] 2
                    break
                case %imya [3 3]: set %a [#i #j] 3
                    break
                case %imya [4 3]: set %a [#i #j] 4
                    break
                case %imya [5 3]: set %a [#i #j] 5
            end_switch
        end_for
    end_for
    //save_array %a C:\Setka1111.txt    // 
else
    goto start
end_if
//   
wait #wait
set #resultat 0

set #a1 findcolor (#startX #startY #endX #endY #stepX #stepY (%imya [1 3] %imya [2 3] %imya [3 3] %imya [4 3] %imya [5 3]) %arr1 #handle)
//save_array %arr C:\Users\abc\Desktop\Setkafind1.txt
if  #a1 > 0
    set #z 0
    for #i 1 6
        for #j 1 6
            set #z #z + 1
            switch %arr1 [#z 3]
                case %imya [1 3]: set %a1 [#i #j] 1
                    break
                case %imya [2 3]: set %a1 [#i #j] 2
                    break
                case %imya [3 3]: set %a1 [#i #j] 3
                    break
                case %imya [4 3]: set %a1 [#i #j] 4
                    break
                case %imya [5 3]: set %a1 [#i #j] 5
            end_switch
        end_for
    end_for
    //save_array %a1 C:\Users\abc\Desktop\Setka1.txt    // 
end_if

//  
for #i 1 6
    set #z 0
    for #j 1 6
        set #z #z + 1
        if  %a [#i #j] != %a1 [#i #j]
            set #resultat 1
            break 2
        end_if
    end_for
end_for
if  #resultat = 1      //    
    goto poisk
end_if

for #i 1 size(%a)
    init_arr %b (#i)  %a[1 #i] %a[2 #i] %a[3 #i] %a[4 #i] %a[5 #i] %a[6 #i]
end_for
log clear
set timer
return

proc line3_1 %a #i $string $newstring #del $regexp #flagcolrow
    set #n regexp (#pos $hitstring $newstring $regexp)
    if  #n > 0
        set #pos #del + #pos
        set $newstring delete($string 1 #pos)
        if  size($newstring) > 1
            if  #flagcolrow = 0
                call line3_1 %a #i $string $newstring #pos $regexp 0
            else
                call line3_1 %a #i $string $newstring #pos $regexp 1
            end_if
        end_if
    end_if
    if  #n > 0
        set #z copy($hitstring 1 1)
        set #pos2 #pos + 2
        set #pos3 #pos + 3
        set #pos4 #pos + 4
        set #pos_1 #pos - 1
        set #pos_2 #pos - 2
        set #pos_3 #pos - 3
        if  #i = 1
            if  #pos < 3 and %a [#i #pos3] = #z and %a [#i #pos4] = #z and %a [2 #pos2] = #z
                call colrow #i #pos2 2 #pos2 #flagcolrow 5 current_script #z
            end_if
            if  #pos < 4 and %a [#i #pos3] = #z and %a [#i #pos4] != #z and %a [2 #pos2] = #z
                call colrow #i #pos2 2 #pos2 #flagcolrow 4 current_script #z
            end_if
            if  #pos < 4 and %a [#i #pos3] = #z
                call colrow #i #pos3 #i #pos2 #flagcolrow 3 current_script #z
            end_if
            if  #pos < 5 and %a [#i #pos3] != #z and %a [2 #pos2] = #z
                call colrow #i #pos2 2 #pos2 #flagcolrow 3 current_script #z
            end_if
            if  #pos = 3
                if  %a [#i #pos_2] = #z and %a [2 #pos_1] = #z
                    call colrow 2 #pos_1 #i #pos_1 #flagcolrow 4 current_script #z
                end_if
            end_if
            if  #pos > 3
                if  %a [#i #pos_2] = #z and %a [#i #pos_3] != #z and %a [2 #pos_1] = #z
                    call colrow #i #pos_1 2 #pos_1 #flagcolrow 4 current_script #z
                end_if
            end_if
            if  #pos > 2
                if  %a [#i #pos_2] = #z
                    call colrow #i #pos_2 #i #pos_1 #flagcolrow 3 current_script #z
                end_if
                if  %a [#i #pos_2] != #z and %a [2 #pos_1] = #z
                    call colrow 2 #pos_1 #i #pos_1 #flagcolrow 3 current_script #z
                end_if
            end_if
            if  #pos = 2
                if  %a [2 #pos_1] = #z
                    call colrow #i #pos_1 2 #pos_1 #flagcolrow 3 current_script #z
                end_if
            end_if
        else
            if  #i = 6
                if  #pos < 3 and %a [#i #pos3] = #z and %a [#i #pos4] = #z and %a [5 #pos2] = #z
                    call colrow #i #pos2 5 #pos2 #flagcolrow 5 current_script #z
                end_if
                if  #pos < 4 and %a [#i #pos3] = #z and %a [#i #pos4] != #z and %a [5 #pos2] = #z
                    call colrow #i #pos2 5 #pos2 #flagcolrow 4 current_script #z
                end_if
                if  #pos < 4 and %a [#i #pos3] = #z
                    call colrow #i #pos3 #i #pos2 #flagcolrow 3 current_script #z
                end_if
                if  #pos < 5 and %a [#i #pos3] != #z and %a [5 #pos2] = #z
                    call colrow #i #pos2 5 #pos2 #flagcolrow 3 current_script #z
                end_if
                if  #pos = 3
                    if  %a [#i #pos_2] = #z and %a [5 #pos_1] = #z
                        call colrow 5 #pos_1 #i #pos_1 #flagcolrow 4 current_script #z
                    end_if
                end_if
                if  #pos > 3
                    if  %a [#i #pos_2] = #z and %a [#i #pos_3] != #z and %a [5 #pos_1] = #z
                        call colrow #i #pos_1 5 #pos_1 #flagcolrow 4 current_script #z
                    end_if
                end_if
                if  #pos > 2
                    if  %a [#i #pos_2] = #z
                        call colrow #i #pos_2 #i #pos_1 #flagcolrow 3 current_script #z
                    end_if
                    if  %a [#i #pos_2] != #z and %a [5 #pos_1] = #z
                        call colrow #i #pos_1 5 #pos_1 #flagcolrow 3 current_script #z
                    end_if
                end_if
                if  #pos = 2
                    if  %a [5 #pos_1] = #z
                        call colrow 5 #pos_1 #i #pos_1 #flagcolrow 3 current_script #z
                    end_if
                end_if
            else                // 2345
                set #col1 #i + 1
                set #col2 #i - 1
                if  #pos < 3 and %a [#i #pos3] = #z and %a [#i #pos4] = #z and %a [#col1 #pos2] = #z
                    call colrow #i #pos2 #col1 #pos2 #flagcolrow 5 current_script #z
                end_if
                if  #pos < 3 and %a [#i #pos3] = #z and %a [#i #pos4] = #z and %a [#col2 #pos2] = #z
                    call colrow #i #pos2 #col2 #pos2 #flagcolrow 5 current_script #z
                end_if
                if  #pos < 4 and %a [#i #pos3] = #z and %a [#i #pos4] != #z and %a [#col1 #pos2] = #z
                    call colrow #i #pos2 #col1 #pos2 #flagcolrow 4 current_script #z
                end_if
                if  #pos < 4 and %a [#i #pos3] = #z and %a [#i #pos4] != #z and %a [#col2 #pos2] = #z
                    call colrow #i #pos2 #col2 #pos2 #flagcolrow 4 current_script #z
                end_if
                if  #pos < 4 and %a [#i #pos3] = #z
                    call colrow #i #pos3 #i #pos2 #flagcolrow 3 current_script #z
                end_if
                if  #pos < 5 and %a [#i #pos3] != #z and %a [#col1 #pos2] = #z
                    call colrow #i #pos2 #col1 #pos2 #flagcolrow 3 current_script #z
                end_if
                if  #pos < 5 and %a [#i #pos3] != #z and %a [#col2 #pos2] = #z

                    call colrow #i #pos2 #col2 #pos2 #flagcolrow 3 current_script #z
                end_if
                if  #pos = 3
                    if  %a [#i #pos_2] = #z and %a [#col1 #pos_1] = #z
                        call colrow #col1 #pos_1 #i #pos_1 #flagcolrow 4 current_script #z
                    end_if
                    if  %a [#i #pos_2] = #z and %a [#col2 #pos_1] = #z
                        call colrow #col2 #pos_1 #i #pos_1 #flagcolrow 4 current_script #z
                    end_if
                end_if
                if  #pos > 3
                    if  %a [#i #pos_2] = #z and %a [#i #pos_3] != #z and %a [#col1 #pos_1] = #z
                        call colrow #i #pos_1 #col1 #pos_1 #flagcolrow 4 current_script #z
                    end_if
                    if  %a [#i #pos_2] = #z and %a [#i #pos_3] != #z and %a [#col2 #pos_1] = #z
                        call colrow #i #pos_1 #col2 #pos_1 #flagcolrow 4 current_script #z
                    end_if
                end_if
                if  #pos > 2
                    if  %a [#i #pos_2] = #z
                        call colrow #i #pos_2 #i #pos_1 #flagcolrow 3 current_script #z
                    end_if
                    if  %a [#i #pos_2] != #z and %a [#col1 #pos_1] = #z
                        call colrow #col1 #pos_1 #i #pos_1 #flagcolrow 3 current_script #z
                    end_if
                    if  %a [#i #pos_2] != #z and %a [#col2 #pos_1] = #z
                        call colrow #col2 #pos_1 #i #pos_1 #flagcolrow 3 current_script #z
                    end_if
                end_if
                if  #pos = 2
                    if  %a [#col1 #pos_1] = #z
                        call colrow #col1 #pos_1 #i #pos_1 #flagcolrow 3 current_script #z
                    end_if
                    if  %a [#col2 #pos_1] = #z
                        call colrow #col2 #pos_1 #i #pos_1  #flagcolrow 3 current_script #z
                    end_if
                end_if
            end_if
        end_if
    end_if
end_proc

proc line3_2 %a #i $string $newstring #del $regexp #flagcolrow
    set #n regexp (#pos $hitstring $newstring $regexp)
    if  #n > 0
        set #pos #del + #pos
        set $newstring delete($string 1 #pos)
        if  size($newstring) > 2
            if  #flagcolrow = 0
                call line3_2 %a #i $string $newstring #pos $regexp 0
            else
                call line3_2 %a #i $string $newstring #pos $regexp 1
            end_if
        end_if
    end_if
    if  #n > 0
        set #z copy($hitstring 1 1)
        set #pos1 #pos + 1
        if  #pos = 1
            set #pos2 #pos + 1
        else
            set #pos2 #pos - 1
        end_if
        if  #i = 1
            if  %a [2 #pos1] = #z and %a [#i #pos2] != #z
                call colrow #i #pos1 2 #pos1 #flagcolrow 3 current_script #z
            end_if
        else
            if  #i = 6
                if  %a [5 #pos1] = #z and %a [#i #pos2] != #z
                    call colrow #i #pos1 5 #pos1 #flagcolrow 3 current_script #z
                end_if
            else
                set #col1 #i + 1
                set #col2 #i - 1
                if  %a [#col1 #pos1] = #z and %a [#i #pos2] != #z
                    call colrow #i #pos1 #col1 #pos1 #flagcolrow 3 current_script #z
                end_if
                if  %a [#col2 #pos1] = #z and %a [#i #pos2] != #z
                    call colrow #i #pos1 #col2 #pos1 #flagcolrow 3 current_script #z
                end_if
            end_if
        end_if
    end_if
end_proc

proc colrow #col1 #row1 #col2 #row2 #flagcolrow #number #cur #z
    set #result.#cur 1
    log mode compact
    if  #flagcolrow = 0
        set #size size(%left.#cur)
        set #size #size + 1
        set %left.#cur [#size 1] #row1
        set %left.#cur [#size 2] #col1
        set %left.#cur [#size 3] #row2
        set %left.#cur [#size 4] #col2
        set %left.#cur [#size 5] #z
        set %left.#cur [#size 6] #number
        set %left.#cur [#size 7] %imya.#cur [#z 1]
    else
        set #size size(%left.#cur)
        set #size #size + 1
        set %left.#cur [#size 1] #col1
        set %left.#cur [#size 2] #row1
        set %left.#cur [#size 3] #col2
        set %left.#cur [#size 4] #row2
        set %left.#cur [#size 5] #z
        set %left.#cur [#size 6] #number
        set %left.#cur [#size 7] %imya.#cur [#z 1]
    end_if
end_proc

:showresult
if  size(%left) > 0
    for #i 1 size(%left)
        log   %left [#i 2] %left [#i 1]  %left [#i 4] %left [#i 3]      %left [#i 6]    %left [#i 7]
    end_for
else
    log   
end_if
sort_array %left 6 dec
//save_array %left C:\Users\abc\Desktop\Resultleft.txt
return

:line3
for #i 1 size(%a)
    set $string %a [#i]
    call line3_1 %a #i $string $string 0 $regexp3_1 0
    call line3_2 %a #i $string $string 0 $regexp3_2 0
end_for
set %a %b
for #i 1 size(%a)
    set $string %a [#i]
    call line3_1 %a #i $string $string 0 $regexp3_1 1
    call line3_2 %a #i $string $string 0 $regexp3_2 1
end_for
return

:hod
if  %left [1 6] > 4
    sort_array %left 5
    sort_array %left 6 dec
    set #summa1 %left [1 6] * %imya [%left [1 5] 2]
    log
    log : %left [1 2] %left [1 1]  %left [1 4] %left [1 3]      %left [1 6] %left [1 7]    : #summa1
    init_arr %xy (1) %left [1 2] %left [1 1] %left [1 4] %left [1 3]
else
    for #i 1 eval (size(%left) - 1)
        set #hod1 %left[#i 2]%left[#i 1]%left[#i 4]%left[#i 3]
        set #jj #i + 1
        for #j #jj size(%left)
            set #hod2 %left[#j 4]%left[#j 3]%left[#j 2]%left[#j 1]
            set #hod3 %left[#j 2]%left[#j 1]%left[#j 4]%left[#j 3]
            if  #hod1 = #hod2 or #hod1 = #hod3
                set #zz #zz + 1
                set #summa %left [#i 6] * %imya [%left [#i 5] 2] + %left [#j 6] * %imya [%left [#j 5] 2]
                init_arr %left2 (#zz) %left[#i 2] %left[#i 1] %left[#i 4] %left[#i 3] %left[#i 6] %imya[%left[#i 5] 1] %left[#j 6] %imya[%left[#j 5] 1] #summa
                if  %left2 [#zz 6] = %left2 [#zz 8]
                    set %left2 [#zz 9] #summa - #summa / 6
                end_if
            end_if
        end_for
    end_for
    for #k 1 size(%left)
        set #summa2 %left [#k 6] * %imya [%left [#k 5] 2]
        set %left [#k 8] #summa2
    end_for
    sort_array %left 8 dec
    sort_array %left2 5 dec
    if  #zz > 0
        if  %left [1 8] < %left2 [1 9]
            log
            log : %left2 [1 1] %left2 [1 2]  %left2 [1 3] %left2 [1 4]      %left2 [1 5] %left2 [1 6]  %left2 [1 7] %left2 [1 8]    : %left2 [1 9]
            init_arr %xy (1) %left2 [1 1] %left2 [1 2] %left2 [1 3] %left2 [1 4]
        else
            log
            log : %left [1 2] %left [1 1]  %left [1 4] %left [1 3]      %left [1 6] %left [1 7]    : %left [1 8]
            init_arr %xy (1) %left [1 2] %left [1 1] %left [1 4] %left [1 3]
        end_if
    else
        log
        log : %left [1 2] %left [1 1]  %left [1 4] %left [1 3]      %left [1 6] %left [1 7]    : %left [1 8]
        init_arr %xy (1) %left [1 2] %left [1 1] %left [1 4] %left [1 3]
    end_if
end_if
set #x1 (#startX + %xy [1 2] * #stepX) - #stepX - %workwindow [1 1]
set #y1 (#startY + %xy [1 1] * #stepY) - #stepY - %workwindow [1 2]
set #x2 (#startX + %xy [1 4] * #stepX) - #stepX - %workwindow [1 1]
set #y2 (#startY + %xy [1 3] * #stepY) - #stepY - %workwindow [1 2]
set #timer timer
log
log    : #timer 
return