Dig a border trench












57












$begingroup$


Background: Too many illegal immigrants from Blandia are crossing the border to Astan. The emperor of Astan has tasked you with digging a trench to keep them out, and Blandia must pay for the expenses. Since all typists have been furloughed until the trench is arranged, your code must be as short as possible.*



Task: Given a 2D map of the border between Astan and Blandia, make the Blands pay (with land) for a border trench.



For example: With Astanian cells marked A, Blandic cells marked B and trench cells marked + (the map frames are only for clarity):



┌──────────┐ ┌──────────┐
│AAAAAAAAAA│ │AAAAAAAAAA│
│ABAAAAAABA│ │A+AAAAAA+A│
│ABBBAABABA│ │A+++AA+A+A│
│ABBBAABABA│ │A+B+AA+A+A│
│ABBBBABABA│→│A+B++A+A+A│
│ABBBBABBBB│ │A+BB+A++++│
│ABBBBABBBB│ │A+BB+A+BBB│
│ABBBBBBBBB│ │A+BB+++BBB│
│BBBBBBBBBB│ │++BBBBBBBB│
└──────────┘ └──────────┘


Details: The map will have at least three rows and three columns. The top row will be entirely Astanian and the bottom row will be entirely Blandic.

 You may use any three values to represent Astanian territory, Blandic territory, and border trench, as long as input and output are consistent.



Automaton formulation: A Blandic cell with at least one Astanian cell in its Moore neighbourhood becomes a border trench cell.



Test cases



[
"AAAAAAAAAA",
"ABAAAAAABA",
"ABBBAABABA",
"ABBBAABABA",
"ABBBBABABA",
"ABBBBABBBB",
"ABBBBABBBB",
"ABBBBBBBBB",
"BBBBBBBBBB"
]


becomes:



[
"AAAAAAAAAA",
"A+AAAAAA+A",
"A+++AA+A+A",
"A+B+AA+A+A",
"A+B++A+A+A",
"A+BB+A++++",
"A+BB+A+BBB",
"A+BB+++BBB",
"++BBBBBBBB"
]




[
"AAA",
"AAA",
"BBB"
]


becomes:



[
"AAA",
"AAA",
"+++"
]




[
"AAAAAAAAAA",
"AAAABBBAAA",
"AAAABBBAAA",
"AAAABBBAAA",
"AAAAAAAAAA",
"BBBBBBABBB",
"BBBBBBAABB",
"BBBAAAAABB",
"BBBBBBBBBB"
]


becomes:



[
"AAAAAAAAAA",
"AAAA+++AAA",
"AAAA+B+AAA",
"AAAA+++AAA",
"AAAAAAAAAA",
"++++++A+++",
"BB++++AA+B",
"BB+AAAAA+B",
"BB+++++++B"
]




* DISCLAIMER: ANY RESEMBLANCE TO ACTUAL GEOPOLITICS IS PURELY COINCIDENTAL!










share|improve this question











$endgroup$








  • 21




    $begingroup$
    Political satire in the form of code golf, I love it :o)
    $endgroup$
    – Sok
    Jan 16 at 17:17






  • 4




    $begingroup$
    -1 for that <sup><sub><sup><sub><sup><sub><sup><sub> :-P
    $endgroup$
    – Luis Mendo
    Jan 16 at 19:44






  • 24




    $begingroup$
    python, 4 bytes: pass The plans to build a border trench lead to a government shutdown and nothing happens.
    $endgroup$
    – TheEspinosa
    Jan 16 at 21:43








  • 3




    $begingroup$
    @TheEspinosa No no, the shutdown is until the trench is arranged.
    $endgroup$
    – Adám
    Jan 16 at 21:45






  • 1




    $begingroup$
    I upvoted just because of the background story. Didn't even continue reading.
    $endgroup$
    – pipe
    Jan 17 at 20:16
















57












$begingroup$


Background: Too many illegal immigrants from Blandia are crossing the border to Astan. The emperor of Astan has tasked you with digging a trench to keep them out, and Blandia must pay for the expenses. Since all typists have been furloughed until the trench is arranged, your code must be as short as possible.*



Task: Given a 2D map of the border between Astan and Blandia, make the Blands pay (with land) for a border trench.



For example: With Astanian cells marked A, Blandic cells marked B and trench cells marked + (the map frames are only for clarity):



┌──────────┐ ┌──────────┐
│AAAAAAAAAA│ │AAAAAAAAAA│
│ABAAAAAABA│ │A+AAAAAA+A│
│ABBBAABABA│ │A+++AA+A+A│
│ABBBAABABA│ │A+B+AA+A+A│
│ABBBBABABA│→│A+B++A+A+A│
│ABBBBABBBB│ │A+BB+A++++│
│ABBBBABBBB│ │A+BB+A+BBB│
│ABBBBBBBBB│ │A+BB+++BBB│
│BBBBBBBBBB│ │++BBBBBBBB│
└──────────┘ └──────────┘


Details: The map will have at least three rows and three columns. The top row will be entirely Astanian and the bottom row will be entirely Blandic.

 You may use any three values to represent Astanian territory, Blandic territory, and border trench, as long as input and output are consistent.



Automaton formulation: A Blandic cell with at least one Astanian cell in its Moore neighbourhood becomes a border trench cell.



Test cases



[
"AAAAAAAAAA",
"ABAAAAAABA",
"ABBBAABABA",
"ABBBAABABA",
"ABBBBABABA",
"ABBBBABBBB",
"ABBBBABBBB",
"ABBBBBBBBB",
"BBBBBBBBBB"
]


becomes:



[
"AAAAAAAAAA",
"A+AAAAAA+A",
"A+++AA+A+A",
"A+B+AA+A+A",
"A+B++A+A+A",
"A+BB+A++++",
"A+BB+A+BBB",
"A+BB+++BBB",
"++BBBBBBBB"
]




[
"AAA",
"AAA",
"BBB"
]


becomes:



[
"AAA",
"AAA",
"+++"
]




[
"AAAAAAAAAA",
"AAAABBBAAA",
"AAAABBBAAA",
"AAAABBBAAA",
"AAAAAAAAAA",
"BBBBBBABBB",
"BBBBBBAABB",
"BBBAAAAABB",
"BBBBBBBBBB"
]


becomes:



[
"AAAAAAAAAA",
"AAAA+++AAA",
"AAAA+B+AAA",
"AAAA+++AAA",
"AAAAAAAAAA",
"++++++A+++",
"BB++++AA+B",
"BB+AAAAA+B",
"BB+++++++B"
]




* DISCLAIMER: ANY RESEMBLANCE TO ACTUAL GEOPOLITICS IS PURELY COINCIDENTAL!










share|improve this question











$endgroup$








  • 21




    $begingroup$
    Political satire in the form of code golf, I love it :o)
    $endgroup$
    – Sok
    Jan 16 at 17:17






  • 4




    $begingroup$
    -1 for that <sup><sub><sup><sub><sup><sub><sup><sub> :-P
    $endgroup$
    – Luis Mendo
    Jan 16 at 19:44






  • 24




    $begingroup$
    python, 4 bytes: pass The plans to build a border trench lead to a government shutdown and nothing happens.
    $endgroup$
    – TheEspinosa
    Jan 16 at 21:43








  • 3




    $begingroup$
    @TheEspinosa No no, the shutdown is until the trench is arranged.
    $endgroup$
    – Adám
    Jan 16 at 21:45






  • 1




    $begingroup$
    I upvoted just because of the background story. Didn't even continue reading.
    $endgroup$
    – pipe
    Jan 17 at 20:16














57












57








57


8



$begingroup$


Background: Too many illegal immigrants from Blandia are crossing the border to Astan. The emperor of Astan has tasked you with digging a trench to keep them out, and Blandia must pay for the expenses. Since all typists have been furloughed until the trench is arranged, your code must be as short as possible.*



Task: Given a 2D map of the border between Astan and Blandia, make the Blands pay (with land) for a border trench.



For example: With Astanian cells marked A, Blandic cells marked B and trench cells marked + (the map frames are only for clarity):



┌──────────┐ ┌──────────┐
│AAAAAAAAAA│ │AAAAAAAAAA│
│ABAAAAAABA│ │A+AAAAAA+A│
│ABBBAABABA│ │A+++AA+A+A│
│ABBBAABABA│ │A+B+AA+A+A│
│ABBBBABABA│→│A+B++A+A+A│
│ABBBBABBBB│ │A+BB+A++++│
│ABBBBABBBB│ │A+BB+A+BBB│
│ABBBBBBBBB│ │A+BB+++BBB│
│BBBBBBBBBB│ │++BBBBBBBB│
└──────────┘ └──────────┘


Details: The map will have at least three rows and three columns. The top row will be entirely Astanian and the bottom row will be entirely Blandic.

 You may use any three values to represent Astanian territory, Blandic territory, and border trench, as long as input and output are consistent.



Automaton formulation: A Blandic cell with at least one Astanian cell in its Moore neighbourhood becomes a border trench cell.



Test cases



[
"AAAAAAAAAA",
"ABAAAAAABA",
"ABBBAABABA",
"ABBBAABABA",
"ABBBBABABA",
"ABBBBABBBB",
"ABBBBABBBB",
"ABBBBBBBBB",
"BBBBBBBBBB"
]


becomes:



[
"AAAAAAAAAA",
"A+AAAAAA+A",
"A+++AA+A+A",
"A+B+AA+A+A",
"A+B++A+A+A",
"A+BB+A++++",
"A+BB+A+BBB",
"A+BB+++BBB",
"++BBBBBBBB"
]




[
"AAA",
"AAA",
"BBB"
]


becomes:



[
"AAA",
"AAA",
"+++"
]




[
"AAAAAAAAAA",
"AAAABBBAAA",
"AAAABBBAAA",
"AAAABBBAAA",
"AAAAAAAAAA",
"BBBBBBABBB",
"BBBBBBAABB",
"BBBAAAAABB",
"BBBBBBBBBB"
]


becomes:



[
"AAAAAAAAAA",
"AAAA+++AAA",
"AAAA+B+AAA",
"AAAA+++AAA",
"AAAAAAAAAA",
"++++++A+++",
"BB++++AA+B",
"BB+AAAAA+B",
"BB+++++++B"
]




* DISCLAIMER: ANY RESEMBLANCE TO ACTUAL GEOPOLITICS IS PURELY COINCIDENTAL!










share|improve this question











$endgroup$




Background: Too many illegal immigrants from Blandia are crossing the border to Astan. The emperor of Astan has tasked you with digging a trench to keep them out, and Blandia must pay for the expenses. Since all typists have been furloughed until the trench is arranged, your code must be as short as possible.*



Task: Given a 2D map of the border between Astan and Blandia, make the Blands pay (with land) for a border trench.



For example: With Astanian cells marked A, Blandic cells marked B and trench cells marked + (the map frames are only for clarity):



┌──────────┐ ┌──────────┐
│AAAAAAAAAA│ │AAAAAAAAAA│
│ABAAAAAABA│ │A+AAAAAA+A│
│ABBBAABABA│ │A+++AA+A+A│
│ABBBAABABA│ │A+B+AA+A+A│
│ABBBBABABA│→│A+B++A+A+A│
│ABBBBABBBB│ │A+BB+A++++│
│ABBBBABBBB│ │A+BB+A+BBB│
│ABBBBBBBBB│ │A+BB+++BBB│
│BBBBBBBBBB│ │++BBBBBBBB│
└──────────┘ └──────────┘


Details: The map will have at least three rows and three columns. The top row will be entirely Astanian and the bottom row will be entirely Blandic.

 You may use any three values to represent Astanian territory, Blandic territory, and border trench, as long as input and output are consistent.



Automaton formulation: A Blandic cell with at least one Astanian cell in its Moore neighbourhood becomes a border trench cell.



Test cases



[
"AAAAAAAAAA",
"ABAAAAAABA",
"ABBBAABABA",
"ABBBAABABA",
"ABBBBABABA",
"ABBBBABBBB",
"ABBBBABBBB",
"ABBBBBBBBB",
"BBBBBBBBBB"
]


becomes:



[
"AAAAAAAAAA",
"A+AAAAAA+A",
"A+++AA+A+A",
"A+B+AA+A+A",
"A+B++A+A+A",
"A+BB+A++++",
"A+BB+A+BBB",
"A+BB+++BBB",
"++BBBBBBBB"
]




[
"AAA",
"AAA",
"BBB"
]


becomes:



[
"AAA",
"AAA",
"+++"
]




[
"AAAAAAAAAA",
"AAAABBBAAA",
"AAAABBBAAA",
"AAAABBBAAA",
"AAAAAAAAAA",
"BBBBBBABBB",
"BBBBBBAABB",
"BBBAAAAABB",
"BBBBBBBBBB"
]


becomes:



[
"AAAAAAAAAA",
"AAAA+++AAA",
"AAAA+B+AAA",
"AAAA+++AAA",
"AAAAAAAAAA",
"++++++A+++",
"BB++++AA+B",
"BB+AAAAA+B",
"BB+++++++B"
]




* DISCLAIMER: ANY RESEMBLANCE TO ACTUAL GEOPOLITICS IS PURELY COINCIDENTAL!







code-golf matrix cellular-automata






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 16 at 18:09







Adám

















asked Jan 16 at 16:54









AdámAdám

29.6k271194




29.6k271194








  • 21




    $begingroup$
    Political satire in the form of code golf, I love it :o)
    $endgroup$
    – Sok
    Jan 16 at 17:17






  • 4




    $begingroup$
    -1 for that <sup><sub><sup><sub><sup><sub><sup><sub> :-P
    $endgroup$
    – Luis Mendo
    Jan 16 at 19:44






  • 24




    $begingroup$
    python, 4 bytes: pass The plans to build a border trench lead to a government shutdown and nothing happens.
    $endgroup$
    – TheEspinosa
    Jan 16 at 21:43








  • 3




    $begingroup$
    @TheEspinosa No no, the shutdown is until the trench is arranged.
    $endgroup$
    – Adám
    Jan 16 at 21:45






  • 1




    $begingroup$
    I upvoted just because of the background story. Didn't even continue reading.
    $endgroup$
    – pipe
    Jan 17 at 20:16














  • 21




    $begingroup$
    Political satire in the form of code golf, I love it :o)
    $endgroup$
    – Sok
    Jan 16 at 17:17






  • 4




    $begingroup$
    -1 for that <sup><sub><sup><sub><sup><sub><sup><sub> :-P
    $endgroup$
    – Luis Mendo
    Jan 16 at 19:44






  • 24




    $begingroup$
    python, 4 bytes: pass The plans to build a border trench lead to a government shutdown and nothing happens.
    $endgroup$
    – TheEspinosa
    Jan 16 at 21:43








  • 3




    $begingroup$
    @TheEspinosa No no, the shutdown is until the trench is arranged.
    $endgroup$
    – Adám
    Jan 16 at 21:45






  • 1




    $begingroup$
    I upvoted just because of the background story. Didn't even continue reading.
    $endgroup$
    – pipe
    Jan 17 at 20:16








21




21




$begingroup$
Political satire in the form of code golf, I love it :o)
$endgroup$
– Sok
Jan 16 at 17:17




$begingroup$
Political satire in the form of code golf, I love it :o)
$endgroup$
– Sok
Jan 16 at 17:17




4




4




$begingroup$
-1 for that <sup><sub><sup><sub><sup><sub><sup><sub> :-P
$endgroup$
– Luis Mendo
Jan 16 at 19:44




$begingroup$
-1 for that <sup><sub><sup><sub><sup><sub><sup><sub> :-P
$endgroup$
– Luis Mendo
Jan 16 at 19:44




24




24




$begingroup$
python, 4 bytes: pass The plans to build a border trench lead to a government shutdown and nothing happens.
$endgroup$
– TheEspinosa
Jan 16 at 21:43






$begingroup$
python, 4 bytes: pass The plans to build a border trench lead to a government shutdown and nothing happens.
$endgroup$
– TheEspinosa
Jan 16 at 21:43






3




3




$begingroup$
@TheEspinosa No no, the shutdown is until the trench is arranged.
$endgroup$
– Adám
Jan 16 at 21:45




$begingroup$
@TheEspinosa No no, the shutdown is until the trench is arranged.
$endgroup$
– Adám
Jan 16 at 21:45




1




1




$begingroup$
I upvoted just because of the background story. Didn't even continue reading.
$endgroup$
– pipe
Jan 17 at 20:16




$begingroup$
I upvoted just because of the background story. Didn't even continue reading.
$endgroup$
– pipe
Jan 17 at 20:16










20 Answers
20






active

oldest

votes


















30












$begingroup$


Wolfram Language (Mathematica), 15 bytes



2#-#~Erosion~1&


Try it online!



Or (39 bytes):



MorphologicalPerimeter[#,Padding->1]+#&


Try it online!



What else would we expect from Mathematica? Characters used are {Astan -> 0, Blandia -> 1, Trench -> 2}.






share|improve this answer











$endgroup$





















    9












    $begingroup$


    MATL, 11 8 bytes



    Inspired by @flawr's Octave answer and @lirtosiast's Mathematica answer.



    EG9&3ZI-


    The input is a matrix with Astan represented by 0 and Blandia by 1. Trench is represented in the output by 2.



    Try it online!



    How it works



    E       % Implicit input. Multiply by 2, element-wise
    G % Push input again
    9 % Push 9
    &3ZI % Erode with neighbourhood of 9 elements (that is, 3×3)
    - % Subtract, element-wise. Implicit display





    share|improve this answer











    $endgroup$





















      7












      $begingroup$


      K (ngn/k), 23 bytes



      {x+x&2{++/'3'0,x,0}/~x}


      Try it online!



      uses 0 1 2 for "AB+"



      { } function with argument x



      ~ logical not



      2{ }/ twice do




      • 0,x,0 surround with 0-s (top and bottom of the matrix)


      • 3' triples of consecutive rows


      • +/' sum each


      • + transpose



      x& logical and of x with



      x+ add x to






      share|improve this answer











      $endgroup$





















        7












        $begingroup$

        JavaScript (ES7),  84  82 bytes



        Saved 2 bytes thanks to @Shaggy



        Takes input as a matrix of integers, with $3$ for Astan and $0$ for Blandia. Returns a matrix with the additional value $1$ for the trench.





        a=>(g=x=>a.map(t=(r,Y)=>r.map((v,X)=>1/x?t|=(x-X)**2+(y-Y)**2<v:v||g(X,y=Y)|t)))()


        Try it online!



        Commented



        a => (                      // a = input matrix
        g = x => // g = function taking x (initially undefined)
        a.map(t = // initialize t to a non-numeric value
        (r, Y) => // for each row r at position Y in a:
        r.map((v, X) => // for each value v at position X in r:
        1 / x ? // if x is defined (this is a recursive call):
        t |= // set the flag t if:
        (x - X) ** 2 + // the squared Euclidean distance
        (y - Y) ** 2 // between (x, y) and (X, Y)
        < v // is less than v (3 = Astan, 0 = Blandia)
        : // else (this is the initial call to g):
        v || // yield v unchanged if it's equal to 3 (Astan)
        g(X, y = Y) // otherwise, do a recursive call with (X, Y) = (x, y)
        | t // and yield the flag t (0 = no change, 1 = trench)
        ) // end of inner map()
        ) // end of outer map()
        )() // initial call to g





        share|improve this answer











        $endgroup$













        • $begingroup$
          82 bytes?
          $endgroup$
          – Shaggy
          Jan 16 at 17:28






        • 4




          $begingroup$
          @Shaggy Not enough questions lately. I don't know how to golf anymore. :D Thanks!
          $endgroup$
          – Arnauld
          Jan 16 at 17:32










        • $begingroup$
          I was only thinking the same thing earlier!
          $endgroup$
          – Shaggy
          Jan 16 at 17:32



















        5












        $begingroup$


        PowerShell, 220 bytes



        It's not as small as the other submissions, but I thought I'd add it for reference.
        [FORE!]





        function m($i,$m){($i-1)..($i+1)|?{$_-in0..$m}}
        $h=$a.count-1;$w=$a[0].length-1
        foreach($i in 0..$h){-join$(foreach($j in 0..$w){if ($a[$i][$j]-eq'B'-and($a[(m $i $h)]|?{$_[(m $j $w)]-match'A'})){'+'}else{$a[$i][$j]}})}


        Try it online!






        share|improve this answer








        New contributor




        Keith S Garner is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.






        $endgroup$









        • 1




          $begingroup$
          Welcome to PPCG. Nice first answer, with TIO link even! Don't worry about code length; each language competes against itself. Btw, you can save one byte by removing the first line break with no ill effects.
          $endgroup$
          – Adám
          Jan 20 at 20:35










        • $begingroup$
          Can the final line become 0..$h|%{-join$(foreach($j in 0..$w){if ($a[$_][$j]-eq'B'-and($a[(m $_ $h)]|?{$_[(m $j $w)]-match'A'})){'+'}else{$a[$_][$j]}})} for 207 bytes?
          $endgroup$
          – Gabriel Mills
          Jan 20 at 22:09



















        4












        $begingroup$


        APL (Dyalog Unicode), 11 bytesSBCS





        ⊢⌈{2∊⍵}⌺3 3


        this is based on @dzaima's 12-byte solution in chat. credit to @Adám himself for thinking of using in the dfn, and to @H.PWiz for reminding us to use the same encoding for input and output



        Try it online!



        represents 'AB+' as 2 0 1 respectively



        { }⌺3 3 apply a function to each overlapping 3×3 region of the input, including regions extending 1 unit outside the matrix, padded with 0s



        2∊⍵ is a 2 present in the argument? return a 0/1 boolean



        ⊢⌈ per-element max of that and the original matrix






        share|improve this answer











        $endgroup$













        • $begingroup$
          Of course, switching to Stencil would save you more than half of your bytes.
          $endgroup$
          – Adám
          Jan 16 at 22:22










        • $begingroup$
          @Adám that would be an answer in a different language, so not comparable or competing against this answer. and i don't find golfing in special-purpose languages particularly interesting, sorry
          $endgroup$
          – ngn
          Jan 16 at 23:24










        • $begingroup$
          @Adám an alias for display which i forgot to remove. removed now
          $endgroup$
          – ngn
          Jan 17 at 2:12





















        4












        $begingroup$


        Octave, 37 31 26 bytes



        This function performs a morphological erosion on the Astan (1-b) part of the "image" using conv2 imerode, and then uses some arithmetic to make all three areas different symbols. Thanks @LuisMendo for -5 bytes!





        @(b)2*b-imerode(b,ones(3))


        Try it online!






        share|improve this answer











        $endgroup$









        • 2




          $begingroup$
          -1 for no convolution :-P
          $endgroup$
          – Luis Mendo
          Jan 16 at 19:46










        • $begingroup$
          @LuisMendo An earlier version did include a convolution:)
          $endgroup$
          – flawr
          Jan 16 at 20:59










        • $begingroup$
          Thanks, updated!
          $endgroup$
          – flawr
          Jan 17 at 9:15



















        2












        $begingroup$


        Charcoal, 20 bytes



        ≔⪫θ⸿θPθFθ⎇∧№KMA⁼Bι+ι


        Try it online! Link is to verbose version of code. Explanation:



        ≔⪫θ⸿θ


        Join the input array with carriage returns rather than the usual newlines. This is needed so that the characters can be printed individually.



        Pθ


        Print the input string without moving the cursor.



        Fθ


        Loop over each character of the input string.



        ⎇∧№KMA⁼Bι


        If the Moore neighbourhood contains an A, and the current character is a B...



        +


        ... then overwrite the B with a +...



        ι


        ... otherwise print the current character (or move to the next line if the current character is a carriage return).






        share|improve this answer









        $endgroup$





















          2












          $begingroup$


          J, 28 bytes



          >.3 3(2 e.,);._3(0|:@,|.)^:4


          Try it online!



          'AB+' -> 2 0 1



          Inspired by ngn's APL solution. 12 bytes just to pad the matrix with zeroes...






          share|improve this answer









          $endgroup$





















            2












            $begingroup$

            Javascript, 126 118 bytes



            _=>_.map(x=>x.replace(/(?<=A)B|B(?=A)/g,0)).map((x,i,a)=>[...x].map((v,j)=>v>'A'&&(a[i-1][j]<v|(a[i+1]||x)[j]<v)?0:v))


            Pass in one of the string arrays from the question, and you'll get an array of strings character arrays (thanks @Shaggy!) out using 0 for the trench. Can probably be golfed more (without switching over to numerical arrays), but I can't think of anything at the moment.






            share|improve this answer











            $endgroup$













            • $begingroup$
              I think this works for 120 bytes.
              $endgroup$
              – Shaggy
              Jan 18 at 12:43










            • $begingroup$
              Or 116 bytes returning an array of character arrays.
              $endgroup$
              – Shaggy
              Jan 18 at 12:43






            • 1




              $begingroup$
              @Shaggy Your golf doesn't work, sadly - it doesn't catch places where A's and B's are diagonal to each-other. On the other hand, it does point out some really simple golfs that I missed...
              $endgroup$
              – M Dirr
              Jan 18 at 15:07



















            1












            $begingroup$


            Retina 0.8.2, 92 80 bytes



            (?<=¶(.)*)B(?=.*¶(?<-1>.)*(?(1)_)A|(?<=¶(?(1)_)(?<-1>.)*A.*¶.*))
            a
            iT`Ba`+`.?a.?


            Try it online! Loosely based on my answer to Will I make it out in time? Explanation: Any Bs immediately above or below As are turned into as. This then reduces the problem to checking Bs to the left or right of As or as. The as themselves also need to get turned into +s of course, but fortunately the i flag to T only affects the regex match, not the actual transliteration, so the As remain unaffected.






            share|improve this answer











            $endgroup$





















              1












              $begingroup$


              C# (Visual C# Interactive Compiler), 187 bytes





              a=>a.Select((b,i)=>b.Select((c,j)=>{int k=0;for(int x=Math.Max(0,i-1);x<Math.Min(i+2,a.Count);x++)for(int y=Math.Max(0,j-1);y<Math.Min(j+2,a[0].Count);)k+=a[x][y++];return k>1&c<1?9:c;}))


              Instead of chaining Take()s, Skip()s, and Select()s, instead this uses double for loops to find neighbors. HUGE byte decrease, from 392 bytes to 187. Linq isn't always the shortest!



              Try it online!






              share|improve this answer











              $endgroup$





















                1












                $begingroup$

                Perl 5, 58 46 bytes



                $m=($n=/$/m+"@+")-2;s/A(|.{$m,$n})KB|B(?=(?1)A)/+/s&&redo


                TIO



                -12 bytes thanks to @Grimy



                /.
                /;s/A(|.{@{-}}.?.?)KB|B(?=(?1)A)/+/s&&redo


                TIO





                • -p like -n but print also


                • -00 paragraph mode

                • to get the width-1 /.n/ matches the last character of first line


                • @{-} special array the position of start of match of previous matched groups, coerced as string (first element)


                • s/../+/s&&redo replace match by + while matches



                  • /s flag, so that . matches also newline character




                • A(|.{@{-}}.?.?)KB matches



                  • AB or A followed by (width-1) to (width+1) characters folowed by B


                  • K to keep the left of B unchanged




                • B(?=(?1)A),



                  • (?1) dirverting recursive, to reference previous expression (|.{$m,$o})


                  • (?=..) lookahead, to match without consuming input








                share|improve this answer











                $endgroup$













                • $begingroup$
                  -9 bytes with /. /,@m=@-while s/A(|.{@m}.?.?)KB|B(?=(?1)A)/+/s (literal newline in the first regex). TIO
                  $endgroup$
                  – Grimy
                  Jan 18 at 10:32






                • 1




                  $begingroup$
                  Down to 46: /. /;s/A(|.{@{-}}.?.?)KB|B(?=(?1)A)/+/s&&redo. TIO
                  $endgroup$
                  – Grimy
                  Jan 18 at 10:50










                • $begingroup$
                  thanks, i also had the idea, but discarded because was thinking to catastrophic backtracking however for code golf performance is not important
                  $endgroup$
                  – Nahuel Fouilleul
                  Jan 18 at 11:14



















                1












                $begingroup$

                JavaScript, 85 bytes



                Threw this together late last night and forgot about it. Probably still room for some improvement somewhere.



                Input and output is as an array of digit arrays, using 3 for Astan, 0 for Blandia & 1 for the trench.



                a=>a.map((x,i)=>x.map((y,j)=>o.map(v=>o.map(h=>y|=1&(a[i+v]||x)[j+h]))|y),o=[-1,0,1])


                Try it online (For convenience, maps from & back to the I/O format used in the challenge)






                share|improve this answer











                $endgroup$





















                  1












                  $begingroup$

                  Java 8, 169 145 bytes





                  m->{for(int i=m.length,j,k;i-->0;)for(j=m[i].length;j-->0;)for(k=9;m[i][j]==1&k-->0;)try{m[i][j]=m[i+k/3-1][j+k%3-1]<1?2:1;}catch(Exception e){}}


                  -24 bytes thanks to @OlivierGrégoire.



                  Uses 0 instead of A and 1 instead of B, with the input being a 2D integer-matrix. Modifies the input-matrix instead of returning a new one to save bytes.



                  The cells are checked the same as in my answer for the All the single eights challenge.



                  Try it online.



                  Explanation:



                  m->{                            // Method with integer-matrix parameter and no return-type
                  for(int i=m.length,j,k;i-->0;)// Loop over the rows
                  for(j=m[i].length;j-->0;) // Inner loop over the columns
                  for(k=9;m[i][j]==1& // If the current cell contains a 1:
                  k-->0;) // Inner loop `k` in the range (9, 0]:
                  try{m[i][j]= // Set the current cell to:
                  m[i+k/3-1] // If `k` is 0, 1, or 2: Look at the previous row
                  // Else-if `k` is 6, 7, or 8: Look at the next row
                  // Else (`k` is 3, 4, or 5): Look at the current row
                  [j+k%3-1] // If `k` is 0, 3, or 6: Look at the previous column
                  // Else-if `k` is 2, 5, or 8: Look at the next column
                  // Else (`k` is 1, 4, or 7): Look at the current column
                  <1? // And if this cell contains a 0:
                  2 // Change the current cell from a 1 to a 2
                  : // Else:
                  1; // Leave it a 1
                  }catch(Exception e){}} // Catch and ignore ArrayIndexOutOfBoundsExceptions
                  // (try-catch saves bytes in comparison to if-checks)





                  share|improve this answer











                  $endgroup$









                  • 1




                    $begingroup$
                    I haven't checked much, but is there anything wrong with m[i+k/3-1][j+k%3-1]? 145 bytes
                    $endgroup$
                    – Olivier Grégoire
                    Jan 18 at 12:31










                  • $begingroup$
                    @OlivierGrégoire Dang, that's so much easier.. Thanks!
                    $endgroup$
                    – Kevin Cruijssen
                    Jan 18 at 12:44










                  • $begingroup$
                    I think it's also valid for your answers of previous challenges given that they seem to have the same structure
                    $endgroup$
                    – Olivier Grégoire
                    Jan 18 at 12:51










                  • $begingroup$
                    @OlivierGrégoire Yeah, I was about to golf those as well with your suggestion, but then another comment (and a question at work) came in between. Will do so in a moment.
                    $endgroup$
                    – Kevin Cruijssen
                    Jan 18 at 13:00



















                  1












                  $begingroup$


                  PowerShell, 86 80 bytes





                  $p="(.?.?.{$(($args|% i*f '
                  ')-1)})?"
                  $args-replace"(?s)(?<=A$p)B|B(?=$p`A)",'+'


                  Try it online!



                  The map is a string with newlines. This script replaces B to + with regexp (?<=A(.?.?.{$MapWidth-1})?)B|B(?=(.?.?.{$MapWidth-1})?A).



                  Less golfed Test script:



                  $f = {
                  $l=($args|% indexOf "`n")-1
                  $p="(.?.?.{$l})?"
                  $args-replace"(?s)(?<=A$p)B|B(?=$p`A)",'+'
                  }

                  @(
                  ,(@"
                  AAAAAAAAAA
                  ABAAAAAABA
                  ABBBAABABA
                  ABBBAABABA
                  ABBBBABABA
                  ABBBBABBBB
                  ABBBBABBBB
                  ABBBBBBBBB
                  BBBBBBBBBB
                  "@,@"
                  AAAAAAAAAA
                  A+AAAAAA+A
                  A+++AA+A+A
                  A+B+AA+A+A
                  A+B++A+A+A
                  A+BB+A++++
                  A+BB+A+BBB
                  A+BB+++BBB
                  ++BBBBBBBB
                  "@)
                  ,(@"
                  AAA
                  AAA
                  BBB
                  "@,@"
                  AAA
                  AAA
                  +++
                  "@)
                  ,(@"
                  AAAAAAAAAA
                  AAAABBBAAA
                  AAAABBBAAA
                  AAAABBBAAA
                  AAAAAAAAAA
                  BBBBBBABBB
                  BBBBBBAABB
                  BBBAAAAABB
                  BBBBBBBBBB
                  "@,@"
                  AAAAAAAAAA
                  AAAA+++AAA
                  AAAA+B+AAA
                  AAAA+++AAA
                  AAAAAAAAAA
                  ++++++A+++
                  BB++++AA+B
                  BB+AAAAA+B
                  BB+++++++B
                  "@)
                  ) | % {
                  $map,$expected = $_
                  $result = &$f $map
                  $result-eq$expected
                  #$result # uncomment this line to display results
                  }


                  Output:



                  True
                  True
                  True





                  share|improve this answer











                  $endgroup$





















                    0












                    $begingroup$


                    Ruby, 102 bytes





                    ->a{a+=?.*s=a.size
                    (s*9).times{|i|a[j=i/9]>?A&&a[j-1+i%3+~a.index($/)*(i/3%3-1)]==?A&&a[j]=?+}
                    a[0,s]}


                    Try it online!



                    input/output as a newline separated string






                    share|improve this answer









                    $endgroup$





















                      0












                      $begingroup$


                      Python 2, 123 119 bytes





                      lambda m:[[[c,'+'][c=='B'and'A'in`[x[j-(j>0):j+2]for x in m[i-(i>0):i+2]]`]for j,c in e(l)]for i,l in e(m)];e=enumerate


                      Try it online!



                      I/O is a list of lists






                      share|improve this answer











                      $endgroup$





















                        0












                        $begingroup$


                        05AB1E, 29 bytes



                        _2FIн¸.øVgN+FYN._3£})εøO}ø}*Ā+


                        Matrices aren't really 05AB1E's strong suit (nor are they my strong suit).. Can definitely be golfed some more, though.

                        Inspired by @ngn's K (ngn/k) answer, so also uses I/O of a 2D integer matrix with 012 for AB+ respectively.



                        Try it online. (The footer in the TIO is to pretty-print the output. Feel free to remove it to see the matrix output.)



                        Explanation:





                        _                # Inverse the values of the (implicit) input-matrix (0→1 and 1→0)
                        # i.e. [[0,0,0,0],[0,1,0,0],[0,1,1,1],[1,1,1,1]]
                        # → [[1,1,1,1],[1,0,1,1],[1,0,0,0],[0,0,0,0]]
                        2F # Loop `n` 2 times in the range [0, 2):
                        Iн # Take the input-matrix, and only leave the first inner list of 0s
                        # i.e. [[0,0,0,0],[0,1,0,0],[0,1,1,1],[1,1,1,1]] → [0,0,0,0]
                        ¸ # Wrap it into a list
                        # i.e. [0,0,0,0] → [[0,0,0,0]]
                        .ø # Surround the inverted input with the list of 0s
                        # i.e. [[1,1,1,1],[1,0,1,1],[1,0,0,0],[0,0,0,0]] and [0,0,0,0]
                        # → [[0,0,0,0],[1,1,1,1],[1,0,1,1],[1,0,0,0],[0,0,0,0],[0,0,0,0]]
                        V # Pop and store it in variable `Y`
                        g # Take the length of the (implicit) input-matrix
                        # i.e. [[0,0,0,0],[0,1,0,0],[0,1,1,1],[1,1,1,1]] → 4
                        N+ # Add `n` to it
                        # i.e. 4 and n=0 → 4
                        # i.e. 4 and n=1 → 5
                        F # Inner loop `N` in the range [0, length+`n`):
                        Y # Push matrix `Y`
                        N._ # Rotate it `N` times towards the left
                        # i.e. [[0,0,0,0],[1,1,1,1],[1,0,1,1],[1,0,0,0],[0,0,0,0],[0,0,0,0]] and N=2
                        # → [[1,0,1,1],[1,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[1,1,1,1]]
                        3£ # And only leave the first three inner lists
                        # i.e. [[1,0,1,1],[1,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[1,1,1,1]]
                        # → [[1,0,1,1],[1,0,0,0],[0,0,0,0]]
                        } # After the inner loop:
                        ) # Wrap everything on the stack into a list
                        # → [[[0,0,0,0],[1,1,1,1],[1,0,1,1]],[[1,1,1,1],[1,0,1,1],[1,0,0,0]],[[1,0,1,1],[1,0,0,0],[0,0,0,0]],[[1,0,0,0],[0,0,0,0],[0,0,0,0]]]
                        €ø # Zip/transpose (swapping rows/columns) each matrix in the list
                        # → [[[0,1,1],[0,1,0],[0,1,1],[0,1,1]],[[1,1,1],[1,0,0],[1,1,0],[1,1,0]],[[1,1,0],[0,0,0],[1,0,0],[1,0,0]],[[1,0,0],[0,0,0],[0,0,0],[0,0,0]]]
                        O # And take the sum of each inner list
                        # → [[2,1,2,2],[3,1,2,2],[2,0,1,1],[1,0,0,0]]
                        ø # Zip/transpose; swapping rows/columns the entire matrix again
                        # i.e. [[2,1,2,2],[3,1,2,2],[2,0,1,1],[1,0,0,0]]
                        # → [[2,3,2,1],[1,1,0,0],[2,2,1,0],[2,2,1,0]]
                        } # After the outer loop:
                        # i.e. [[3,5,5,4,2],[4,6,5,4,2],[2,3,2,2,1],[1,1,0,0,0]]
                        * # Multiple each value with the input-matrix at the same positions,
                        # which implicitly removes the trailing values
                        # i.e. [[3,5,5,4,2],[4,6,5,4,2],[2,3,2,2,1],[1,1,0,0,0]]
                        # and [[0,0,0,0],[0,1,0,0],[0,1,1,1],[1,1,1,1]]
                        # → [[0,0,0,0],[0,1,0,0],[0,2,1,0],[2,2,1,0]]
                        Ā # Truthify each value (0 remains 0; everything else becomes 1)
                        # i.e. [[0,0,0,0],[0,1,0,0],[0,2,1,0],[2,2,1,0]]
                        # → [[0,0,0,0],[0,1,0,0],[0,1,1,1],[1,1,0,0]]
                        + # Then add each value with the input-matrix at the same positions
                        # i.e. [[0,0,0,0],[0,1,0,0],[0,1,1,1],[1,1,0,0]]
                        # and [[0,0,0,0],[0,1,0,0],[0,1,1,1],[1,1,1,1]]
                        # → [[0,0,0,0],[0,2,0,0],[0,2,2,2],[2,2,1,1]]
                        # (and output the result implicitly)





                        share|improve this answer









                        $endgroup$





















                          0












                          $begingroup$

                          TSQL, 252 bytes



                          Splitting the string is very costly, if the string was split and already in a table the byte count would be 127 characters. Script included in the bottom and completely different. Sorry for taking up this much space.



                          Golfed:



                          WITH C as(SELECT x,x/z r,x%z c,substring(@,x+1,1)v
                          FROM spt_values CROSS APPLY(SELECT number x,charindex(char(10),@)z)z
                          WHERE'P'=type)SELECT @=stuff(@,x+1,1,'+')FROM c WHERE
                          exists(SELECT*FROM c d WHERE abs(r-c.r)<2and
                          abs(c-c.c)<2and'AB'=v+c.v)PRINT @


                          Ungolfed:



                          DECLARE @ varchar(max)=
                          'AAAAAAAAAA
                          ABAAAAAABA
                          ABBBAABABA
                          ABBBAABABA
                          ABBBBABABA
                          ABBBBABBBB
                          ABBBBABBBB
                          ABBBBBBBBB
                          BBBBBBBBBB';

                          WITH C as
                          (
                          SELECT x,x/z r,x%z c,substring(@,x+1,1)v
                          FROM spt_values
                          CROSS APPLY(SELECT number x,charindex(char(10),@)z)z
                          WHERE'P'=type
                          )
                          SELECT
                          @=stuff(@,x+1,1,'+')
                          FROM c
                          WHERE exists(SELECT*FROM c d
                          WHERE abs(r-c.r)<2
                          and abs(c-c.c)<2 and'AB'=v+c.v)
                          PRINT @


                          Try it out



                          TSQL, 127 bytes(Using table variable as input)



                          Execute this script in management studio - use "query"-"result to text" to make it readable



                          --populate table variable
                          USE master
                          DECLARE @v varchar(max)=
                          'AAAAAAAAAA
                          ABAAAAAABA
                          ABBBAABABA
                          ABBBAABABA
                          ABBBBABABA
                          ABBBBABBBB
                          ABBBBABBBB
                          ABBBBBBBBB
                          BBBBBBBBBB'

                          DECLARE @ table(x int, r int, c int, v char)

                          INSERT @
                          SELECT x+1,x/z,x%z,substring(@v,x+1,1)
                          FROM spt_values
                          CROSS APPLY(SELECT number x,charindex(char(10),@v)z)z
                          WHERE'P'=type and len(@v)>number

                          -- query(127 characters)

                          SELECT string_agg(v,'')FROM(SELECT
                          iif(exists(SELECT*FROM @
                          WHERE abs(r-c.r)<2and abs(c-c.c)<2and'AB'=v+c.v),'+',v)v
                          FROM @ c)z


                          Try it out - warning output is selected and not readable. Would be readable with print, but that is not possible using this method






                          share|improve this answer











                          $endgroup$













                          • $begingroup$
                            What makes you think that you can't take a table as argument?
                            $endgroup$
                            – Adám
                            Jan 18 at 12:27










                          • $begingroup$
                            @Adám not a bad idea to create the code using a table as argument as well - I will get right on it
                            $endgroup$
                            – t-clausen.dk
                            Jan 18 at 12:52










                          • $begingroup$
                            @Adám I guess I was wrong, in order to make the script work for 120 characters, I would need both the table and the varchar as input, I did rewrite it. It took 151 characters
                            $endgroup$
                            – t-clausen.dk
                            Jan 18 at 13:25











                          Your Answer





                          StackExchange.ifUsing("editor", function () {
                          return StackExchange.using("mathjaxEditing", function () {
                          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
                          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
                          });
                          });
                          }, "mathjax-editing");

                          StackExchange.ifUsing("editor", function () {
                          StackExchange.using("externalEditor", function () {
                          StackExchange.using("snippets", function () {
                          StackExchange.snippets.init();
                          });
                          });
                          }, "code-snippets");

                          StackExchange.ready(function() {
                          var channelOptions = {
                          tags: "".split(" "),
                          id: "200"
                          };
                          initTagRenderer("".split(" "), "".split(" "), channelOptions);

                          StackExchange.using("externalEditor", function() {
                          // Have to fire editor after snippets, if snippets enabled
                          if (StackExchange.settings.snippets.snippetsEnabled) {
                          StackExchange.using("snippets", function() {
                          createEditor();
                          });
                          }
                          else {
                          createEditor();
                          }
                          });

                          function createEditor() {
                          StackExchange.prepareEditor({
                          heartbeatType: 'answer',
                          autoActivateHeartbeat: false,
                          convertImagesToLinks: false,
                          noModals: true,
                          showLowRepImageUploadWarning: true,
                          reputationToPostImages: null,
                          bindNavPrevention: true,
                          postfix: "",
                          imageUploader: {
                          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                          allowUrls: true
                          },
                          onDemand: true,
                          discardSelector: ".discard-answer"
                          ,immediatelyShowMarkdownHelp:true
                          });


                          }
                          });














                          draft saved

                          draft discarded


















                          StackExchange.ready(
                          function () {
                          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f178809%2fdig-a-border-trench%23new-answer', 'question_page');
                          }
                          );

                          Post as a guest















                          Required, but never shown

























                          20 Answers
                          20






                          active

                          oldest

                          votes








                          20 Answers
                          20






                          active

                          oldest

                          votes









                          active

                          oldest

                          votes






                          active

                          oldest

                          votes









                          30












                          $begingroup$


                          Wolfram Language (Mathematica), 15 bytes



                          2#-#~Erosion~1&


                          Try it online!



                          Or (39 bytes):



                          MorphologicalPerimeter[#,Padding->1]+#&


                          Try it online!



                          What else would we expect from Mathematica? Characters used are {Astan -> 0, Blandia -> 1, Trench -> 2}.






                          share|improve this answer











                          $endgroup$


















                            30












                            $begingroup$


                            Wolfram Language (Mathematica), 15 bytes



                            2#-#~Erosion~1&


                            Try it online!



                            Or (39 bytes):



                            MorphologicalPerimeter[#,Padding->1]+#&


                            Try it online!



                            What else would we expect from Mathematica? Characters used are {Astan -> 0, Blandia -> 1, Trench -> 2}.






                            share|improve this answer











                            $endgroup$
















                              30












                              30








                              30





                              $begingroup$


                              Wolfram Language (Mathematica), 15 bytes



                              2#-#~Erosion~1&


                              Try it online!



                              Or (39 bytes):



                              MorphologicalPerimeter[#,Padding->1]+#&


                              Try it online!



                              What else would we expect from Mathematica? Characters used are {Astan -> 0, Blandia -> 1, Trench -> 2}.






                              share|improve this answer











                              $endgroup$




                              Wolfram Language (Mathematica), 15 bytes



                              2#-#~Erosion~1&


                              Try it online!



                              Or (39 bytes):



                              MorphologicalPerimeter[#,Padding->1]+#&


                              Try it online!



                              What else would we expect from Mathematica? Characters used are {Astan -> 0, Blandia -> 1, Trench -> 2}.







                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited Jan 16 at 21:47

























                              answered Jan 16 at 17:48









                              lirtosiastlirtosiast

                              16.1k437108




                              16.1k437108























                                  9












                                  $begingroup$


                                  MATL, 11 8 bytes



                                  Inspired by @flawr's Octave answer and @lirtosiast's Mathematica answer.



                                  EG9&3ZI-


                                  The input is a matrix with Astan represented by 0 and Blandia by 1. Trench is represented in the output by 2.



                                  Try it online!



                                  How it works



                                  E       % Implicit input. Multiply by 2, element-wise
                                  G % Push input again
                                  9 % Push 9
                                  &3ZI % Erode with neighbourhood of 9 elements (that is, 3×3)
                                  - % Subtract, element-wise. Implicit display





                                  share|improve this answer











                                  $endgroup$


















                                    9












                                    $begingroup$


                                    MATL, 11 8 bytes



                                    Inspired by @flawr's Octave answer and @lirtosiast's Mathematica answer.



                                    EG9&3ZI-


                                    The input is a matrix with Astan represented by 0 and Blandia by 1. Trench is represented in the output by 2.



                                    Try it online!



                                    How it works



                                    E       % Implicit input. Multiply by 2, element-wise
                                    G % Push input again
                                    9 % Push 9
                                    &3ZI % Erode with neighbourhood of 9 elements (that is, 3×3)
                                    - % Subtract, element-wise. Implicit display





                                    share|improve this answer











                                    $endgroup$
















                                      9












                                      9








                                      9





                                      $begingroup$


                                      MATL, 11 8 bytes



                                      Inspired by @flawr's Octave answer and @lirtosiast's Mathematica answer.



                                      EG9&3ZI-


                                      The input is a matrix with Astan represented by 0 and Blandia by 1. Trench is represented in the output by 2.



                                      Try it online!



                                      How it works



                                      E       % Implicit input. Multiply by 2, element-wise
                                      G % Push input again
                                      9 % Push 9
                                      &3ZI % Erode with neighbourhood of 9 elements (that is, 3×3)
                                      - % Subtract, element-wise. Implicit display





                                      share|improve this answer











                                      $endgroup$




                                      MATL, 11 8 bytes



                                      Inspired by @flawr's Octave answer and @lirtosiast's Mathematica answer.



                                      EG9&3ZI-


                                      The input is a matrix with Astan represented by 0 and Blandia by 1. Trench is represented in the output by 2.



                                      Try it online!



                                      How it works



                                      E       % Implicit input. Multiply by 2, element-wise
                                      G % Push input again
                                      9 % Push 9
                                      &3ZI % Erode with neighbourhood of 9 elements (that is, 3×3)
                                      - % Subtract, element-wise. Implicit display






                                      share|improve this answer














                                      share|improve this answer



                                      share|improve this answer








                                      edited Jan 16 at 23:44

























                                      answered Jan 16 at 21:32









                                      Luis MendoLuis Mendo

                                      74.2k887291




                                      74.2k887291























                                          7












                                          $begingroup$


                                          K (ngn/k), 23 bytes



                                          {x+x&2{++/'3'0,x,0}/~x}


                                          Try it online!



                                          uses 0 1 2 for "AB+"



                                          { } function with argument x



                                          ~ logical not



                                          2{ }/ twice do




                                          • 0,x,0 surround with 0-s (top and bottom of the matrix)


                                          • 3' triples of consecutive rows


                                          • +/' sum each


                                          • + transpose



                                          x& logical and of x with



                                          x+ add x to






                                          share|improve this answer











                                          $endgroup$


















                                            7












                                            $begingroup$


                                            K (ngn/k), 23 bytes



                                            {x+x&2{++/'3'0,x,0}/~x}


                                            Try it online!



                                            uses 0 1 2 for "AB+"



                                            { } function with argument x



                                            ~ logical not



                                            2{ }/ twice do




                                            • 0,x,0 surround with 0-s (top and bottom of the matrix)


                                            • 3' triples of consecutive rows


                                            • +/' sum each


                                            • + transpose



                                            x& logical and of x with



                                            x+ add x to






                                            share|improve this answer











                                            $endgroup$
















                                              7












                                              7








                                              7





                                              $begingroup$


                                              K (ngn/k), 23 bytes



                                              {x+x&2{++/'3'0,x,0}/~x}


                                              Try it online!



                                              uses 0 1 2 for "AB+"



                                              { } function with argument x



                                              ~ logical not



                                              2{ }/ twice do




                                              • 0,x,0 surround with 0-s (top and bottom of the matrix)


                                              • 3' triples of consecutive rows


                                              • +/' sum each


                                              • + transpose



                                              x& logical and of x with



                                              x+ add x to






                                              share|improve this answer











                                              $endgroup$




                                              K (ngn/k), 23 bytes



                                              {x+x&2{++/'3'0,x,0}/~x}


                                              Try it online!



                                              uses 0 1 2 for "AB+"



                                              { } function with argument x



                                              ~ logical not



                                              2{ }/ twice do




                                              • 0,x,0 surround with 0-s (top and bottom of the matrix)


                                              • 3' triples of consecutive rows


                                              • +/' sum each


                                              • + transpose



                                              x& logical and of x with



                                              x+ add x to







                                              share|improve this answer














                                              share|improve this answer



                                              share|improve this answer








                                              edited Jan 16 at 18:53

























                                              answered Jan 16 at 17:53









                                              ngnngn

                                              7,09112559




                                              7,09112559























                                                  7












                                                  $begingroup$

                                                  JavaScript (ES7),  84  82 bytes



                                                  Saved 2 bytes thanks to @Shaggy



                                                  Takes input as a matrix of integers, with $3$ for Astan and $0$ for Blandia. Returns a matrix with the additional value $1$ for the trench.





                                                  a=>(g=x=>a.map(t=(r,Y)=>r.map((v,X)=>1/x?t|=(x-X)**2+(y-Y)**2<v:v||g(X,y=Y)|t)))()


                                                  Try it online!



                                                  Commented



                                                  a => (                      // a = input matrix
                                                  g = x => // g = function taking x (initially undefined)
                                                  a.map(t = // initialize t to a non-numeric value
                                                  (r, Y) => // for each row r at position Y in a:
                                                  r.map((v, X) => // for each value v at position X in r:
                                                  1 / x ? // if x is defined (this is a recursive call):
                                                  t |= // set the flag t if:
                                                  (x - X) ** 2 + // the squared Euclidean distance
                                                  (y - Y) ** 2 // between (x, y) and (X, Y)
                                                  < v // is less than v (3 = Astan, 0 = Blandia)
                                                  : // else (this is the initial call to g):
                                                  v || // yield v unchanged if it's equal to 3 (Astan)
                                                  g(X, y = Y) // otherwise, do a recursive call with (X, Y) = (x, y)
                                                  | t // and yield the flag t (0 = no change, 1 = trench)
                                                  ) // end of inner map()
                                                  ) // end of outer map()
                                                  )() // initial call to g





                                                  share|improve this answer











                                                  $endgroup$













                                                  • $begingroup$
                                                    82 bytes?
                                                    $endgroup$
                                                    – Shaggy
                                                    Jan 16 at 17:28






                                                  • 4




                                                    $begingroup$
                                                    @Shaggy Not enough questions lately. I don't know how to golf anymore. :D Thanks!
                                                    $endgroup$
                                                    – Arnauld
                                                    Jan 16 at 17:32










                                                  • $begingroup$
                                                    I was only thinking the same thing earlier!
                                                    $endgroup$
                                                    – Shaggy
                                                    Jan 16 at 17:32
















                                                  7












                                                  $begingroup$

                                                  JavaScript (ES7),  84  82 bytes



                                                  Saved 2 bytes thanks to @Shaggy



                                                  Takes input as a matrix of integers, with $3$ for Astan and $0$ for Blandia. Returns a matrix with the additional value $1$ for the trench.





                                                  a=>(g=x=>a.map(t=(r,Y)=>r.map((v,X)=>1/x?t|=(x-X)**2+(y-Y)**2<v:v||g(X,y=Y)|t)))()


                                                  Try it online!



                                                  Commented



                                                  a => (                      // a = input matrix
                                                  g = x => // g = function taking x (initially undefined)
                                                  a.map(t = // initialize t to a non-numeric value
                                                  (r, Y) => // for each row r at position Y in a:
                                                  r.map((v, X) => // for each value v at position X in r:
                                                  1 / x ? // if x is defined (this is a recursive call):
                                                  t |= // set the flag t if:
                                                  (x - X) ** 2 + // the squared Euclidean distance
                                                  (y - Y) ** 2 // between (x, y) and (X, Y)
                                                  < v // is less than v (3 = Astan, 0 = Blandia)
                                                  : // else (this is the initial call to g):
                                                  v || // yield v unchanged if it's equal to 3 (Astan)
                                                  g(X, y = Y) // otherwise, do a recursive call with (X, Y) = (x, y)
                                                  | t // and yield the flag t (0 = no change, 1 = trench)
                                                  ) // end of inner map()
                                                  ) // end of outer map()
                                                  )() // initial call to g





                                                  share|improve this answer











                                                  $endgroup$













                                                  • $begingroup$
                                                    82 bytes?
                                                    $endgroup$
                                                    – Shaggy
                                                    Jan 16 at 17:28






                                                  • 4




                                                    $begingroup$
                                                    @Shaggy Not enough questions lately. I don't know how to golf anymore. :D Thanks!
                                                    $endgroup$
                                                    – Arnauld
                                                    Jan 16 at 17:32










                                                  • $begingroup$
                                                    I was only thinking the same thing earlier!
                                                    $endgroup$
                                                    – Shaggy
                                                    Jan 16 at 17:32














                                                  7












                                                  7








                                                  7





                                                  $begingroup$

                                                  JavaScript (ES7),  84  82 bytes



                                                  Saved 2 bytes thanks to @Shaggy



                                                  Takes input as a matrix of integers, with $3$ for Astan and $0$ for Blandia. Returns a matrix with the additional value $1$ for the trench.





                                                  a=>(g=x=>a.map(t=(r,Y)=>r.map((v,X)=>1/x?t|=(x-X)**2+(y-Y)**2<v:v||g(X,y=Y)|t)))()


                                                  Try it online!



                                                  Commented



                                                  a => (                      // a = input matrix
                                                  g = x => // g = function taking x (initially undefined)
                                                  a.map(t = // initialize t to a non-numeric value
                                                  (r, Y) => // for each row r at position Y in a:
                                                  r.map((v, X) => // for each value v at position X in r:
                                                  1 / x ? // if x is defined (this is a recursive call):
                                                  t |= // set the flag t if:
                                                  (x - X) ** 2 + // the squared Euclidean distance
                                                  (y - Y) ** 2 // between (x, y) and (X, Y)
                                                  < v // is less than v (3 = Astan, 0 = Blandia)
                                                  : // else (this is the initial call to g):
                                                  v || // yield v unchanged if it's equal to 3 (Astan)
                                                  g(X, y = Y) // otherwise, do a recursive call with (X, Y) = (x, y)
                                                  | t // and yield the flag t (0 = no change, 1 = trench)
                                                  ) // end of inner map()
                                                  ) // end of outer map()
                                                  )() // initial call to g





                                                  share|improve this answer











                                                  $endgroup$



                                                  JavaScript (ES7),  84  82 bytes



                                                  Saved 2 bytes thanks to @Shaggy



                                                  Takes input as a matrix of integers, with $3$ for Astan and $0$ for Blandia. Returns a matrix with the additional value $1$ for the trench.





                                                  a=>(g=x=>a.map(t=(r,Y)=>r.map((v,X)=>1/x?t|=(x-X)**2+(y-Y)**2<v:v||g(X,y=Y)|t)))()


                                                  Try it online!



                                                  Commented



                                                  a => (                      // a = input matrix
                                                  g = x => // g = function taking x (initially undefined)
                                                  a.map(t = // initialize t to a non-numeric value
                                                  (r, Y) => // for each row r at position Y in a:
                                                  r.map((v, X) => // for each value v at position X in r:
                                                  1 / x ? // if x is defined (this is a recursive call):
                                                  t |= // set the flag t if:
                                                  (x - X) ** 2 + // the squared Euclidean distance
                                                  (y - Y) ** 2 // between (x, y) and (X, Y)
                                                  < v // is less than v (3 = Astan, 0 = Blandia)
                                                  : // else (this is the initial call to g):
                                                  v || // yield v unchanged if it's equal to 3 (Astan)
                                                  g(X, y = Y) // otherwise, do a recursive call with (X, Y) = (x, y)
                                                  | t // and yield the flag t (0 = no change, 1 = trench)
                                                  ) // end of inner map()
                                                  ) // end of outer map()
                                                  )() // initial call to g






                                                  share|improve this answer














                                                  share|improve this answer



                                                  share|improve this answer








                                                  edited Jan 17 at 10:11

























                                                  answered Jan 16 at 17:24









                                                  ArnauldArnauld

                                                  74k690310




                                                  74k690310












                                                  • $begingroup$
                                                    82 bytes?
                                                    $endgroup$
                                                    – Shaggy
                                                    Jan 16 at 17:28






                                                  • 4




                                                    $begingroup$
                                                    @Shaggy Not enough questions lately. I don't know how to golf anymore. :D Thanks!
                                                    $endgroup$
                                                    – Arnauld
                                                    Jan 16 at 17:32










                                                  • $begingroup$
                                                    I was only thinking the same thing earlier!
                                                    $endgroup$
                                                    – Shaggy
                                                    Jan 16 at 17:32


















                                                  • $begingroup$
                                                    82 bytes?
                                                    $endgroup$
                                                    – Shaggy
                                                    Jan 16 at 17:28






                                                  • 4




                                                    $begingroup$
                                                    @Shaggy Not enough questions lately. I don't know how to golf anymore. :D Thanks!
                                                    $endgroup$
                                                    – Arnauld
                                                    Jan 16 at 17:32










                                                  • $begingroup$
                                                    I was only thinking the same thing earlier!
                                                    $endgroup$
                                                    – Shaggy
                                                    Jan 16 at 17:32
















                                                  $begingroup$
                                                  82 bytes?
                                                  $endgroup$
                                                  – Shaggy
                                                  Jan 16 at 17:28




                                                  $begingroup$
                                                  82 bytes?
                                                  $endgroup$
                                                  – Shaggy
                                                  Jan 16 at 17:28




                                                  4




                                                  4




                                                  $begingroup$
                                                  @Shaggy Not enough questions lately. I don't know how to golf anymore. :D Thanks!
                                                  $endgroup$
                                                  – Arnauld
                                                  Jan 16 at 17:32




                                                  $begingroup$
                                                  @Shaggy Not enough questions lately. I don't know how to golf anymore. :D Thanks!
                                                  $endgroup$
                                                  – Arnauld
                                                  Jan 16 at 17:32












                                                  $begingroup$
                                                  I was only thinking the same thing earlier!
                                                  $endgroup$
                                                  – Shaggy
                                                  Jan 16 at 17:32




                                                  $begingroup$
                                                  I was only thinking the same thing earlier!
                                                  $endgroup$
                                                  – Shaggy
                                                  Jan 16 at 17:32











                                                  5












                                                  $begingroup$


                                                  PowerShell, 220 bytes



                                                  It's not as small as the other submissions, but I thought I'd add it for reference.
                                                  [FORE!]





                                                  function m($i,$m){($i-1)..($i+1)|?{$_-in0..$m}}
                                                  $h=$a.count-1;$w=$a[0].length-1
                                                  foreach($i in 0..$h){-join$(foreach($j in 0..$w){if ($a[$i][$j]-eq'B'-and($a[(m $i $h)]|?{$_[(m $j $w)]-match'A'})){'+'}else{$a[$i][$j]}})}


                                                  Try it online!






                                                  share|improve this answer








                                                  New contributor




                                                  Keith S Garner is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                  Check out our Code of Conduct.






                                                  $endgroup$









                                                  • 1




                                                    $begingroup$
                                                    Welcome to PPCG. Nice first answer, with TIO link even! Don't worry about code length; each language competes against itself. Btw, you can save one byte by removing the first line break with no ill effects.
                                                    $endgroup$
                                                    – Adám
                                                    Jan 20 at 20:35










                                                  • $begingroup$
                                                    Can the final line become 0..$h|%{-join$(foreach($j in 0..$w){if ($a[$_][$j]-eq'B'-and($a[(m $_ $h)]|?{$_[(m $j $w)]-match'A'})){'+'}else{$a[$_][$j]}})} for 207 bytes?
                                                    $endgroup$
                                                    – Gabriel Mills
                                                    Jan 20 at 22:09
















                                                  5












                                                  $begingroup$


                                                  PowerShell, 220 bytes



                                                  It's not as small as the other submissions, but I thought I'd add it for reference.
                                                  [FORE!]





                                                  function m($i,$m){($i-1)..($i+1)|?{$_-in0..$m}}
                                                  $h=$a.count-1;$w=$a[0].length-1
                                                  foreach($i in 0..$h){-join$(foreach($j in 0..$w){if ($a[$i][$j]-eq'B'-and($a[(m $i $h)]|?{$_[(m $j $w)]-match'A'})){'+'}else{$a[$i][$j]}})}


                                                  Try it online!






                                                  share|improve this answer








                                                  New contributor




                                                  Keith S Garner is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                  Check out our Code of Conduct.






                                                  $endgroup$









                                                  • 1




                                                    $begingroup$
                                                    Welcome to PPCG. Nice first answer, with TIO link even! Don't worry about code length; each language competes against itself. Btw, you can save one byte by removing the first line break with no ill effects.
                                                    $endgroup$
                                                    – Adám
                                                    Jan 20 at 20:35










                                                  • $begingroup$
                                                    Can the final line become 0..$h|%{-join$(foreach($j in 0..$w){if ($a[$_][$j]-eq'B'-and($a[(m $_ $h)]|?{$_[(m $j $w)]-match'A'})){'+'}else{$a[$_][$j]}})} for 207 bytes?
                                                    $endgroup$
                                                    – Gabriel Mills
                                                    Jan 20 at 22:09














                                                  5












                                                  5








                                                  5





                                                  $begingroup$


                                                  PowerShell, 220 bytes



                                                  It's not as small as the other submissions, but I thought I'd add it for reference.
                                                  [FORE!]





                                                  function m($i,$m){($i-1)..($i+1)|?{$_-in0..$m}}
                                                  $h=$a.count-1;$w=$a[0].length-1
                                                  foreach($i in 0..$h){-join$(foreach($j in 0..$w){if ($a[$i][$j]-eq'B'-and($a[(m $i $h)]|?{$_[(m $j $w)]-match'A'})){'+'}else{$a[$i][$j]}})}


                                                  Try it online!






                                                  share|improve this answer








                                                  New contributor




                                                  Keith S Garner is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                  Check out our Code of Conduct.






                                                  $endgroup$




                                                  PowerShell, 220 bytes



                                                  It's not as small as the other submissions, but I thought I'd add it for reference.
                                                  [FORE!]





                                                  function m($i,$m){($i-1)..($i+1)|?{$_-in0..$m}}
                                                  $h=$a.count-1;$w=$a[0].length-1
                                                  foreach($i in 0..$h){-join$(foreach($j in 0..$w){if ($a[$i][$j]-eq'B'-and($a[(m $i $h)]|?{$_[(m $j $w)]-match'A'})){'+'}else{$a[$i][$j]}})}


                                                  Try it online!







                                                  share|improve this answer








                                                  New contributor




                                                  Keith S Garner is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                  Check out our Code of Conduct.









                                                  share|improve this answer



                                                  share|improve this answer






                                                  New contributor




                                                  Keith S Garner is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                  Check out our Code of Conduct.









                                                  answered Jan 20 at 19:47









                                                  Keith S GarnerKeith S Garner

                                                  511




                                                  511




                                                  New contributor




                                                  Keith S Garner is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                  Check out our Code of Conduct.





                                                  New contributor





                                                  Keith S Garner is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                  Check out our Code of Conduct.






                                                  Keith S Garner is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                  Check out our Code of Conduct.








                                                  • 1




                                                    $begingroup$
                                                    Welcome to PPCG. Nice first answer, with TIO link even! Don't worry about code length; each language competes against itself. Btw, you can save one byte by removing the first line break with no ill effects.
                                                    $endgroup$
                                                    – Adám
                                                    Jan 20 at 20:35










                                                  • $begingroup$
                                                    Can the final line become 0..$h|%{-join$(foreach($j in 0..$w){if ($a[$_][$j]-eq'B'-and($a[(m $_ $h)]|?{$_[(m $j $w)]-match'A'})){'+'}else{$a[$_][$j]}})} for 207 bytes?
                                                    $endgroup$
                                                    – Gabriel Mills
                                                    Jan 20 at 22:09














                                                  • 1




                                                    $begingroup$
                                                    Welcome to PPCG. Nice first answer, with TIO link even! Don't worry about code length; each language competes against itself. Btw, you can save one byte by removing the first line break with no ill effects.
                                                    $endgroup$
                                                    – Adám
                                                    Jan 20 at 20:35










                                                  • $begingroup$
                                                    Can the final line become 0..$h|%{-join$(foreach($j in 0..$w){if ($a[$_][$j]-eq'B'-and($a[(m $_ $h)]|?{$_[(m $j $w)]-match'A'})){'+'}else{$a[$_][$j]}})} for 207 bytes?
                                                    $endgroup$
                                                    – Gabriel Mills
                                                    Jan 20 at 22:09








                                                  1




                                                  1




                                                  $begingroup$
                                                  Welcome to PPCG. Nice first answer, with TIO link even! Don't worry about code length; each language competes against itself. Btw, you can save one byte by removing the first line break with no ill effects.
                                                  $endgroup$
                                                  – Adám
                                                  Jan 20 at 20:35




                                                  $begingroup$
                                                  Welcome to PPCG. Nice first answer, with TIO link even! Don't worry about code length; each language competes against itself. Btw, you can save one byte by removing the first line break with no ill effects.
                                                  $endgroup$
                                                  – Adám
                                                  Jan 20 at 20:35












                                                  $begingroup$
                                                  Can the final line become 0..$h|%{-join$(foreach($j in 0..$w){if ($a[$_][$j]-eq'B'-and($a[(m $_ $h)]|?{$_[(m $j $w)]-match'A'})){'+'}else{$a[$_][$j]}})} for 207 bytes?
                                                  $endgroup$
                                                  – Gabriel Mills
                                                  Jan 20 at 22:09




                                                  $begingroup$
                                                  Can the final line become 0..$h|%{-join$(foreach($j in 0..$w){if ($a[$_][$j]-eq'B'-and($a[(m $_ $h)]|?{$_[(m $j $w)]-match'A'})){'+'}else{$a[$_][$j]}})} for 207 bytes?
                                                  $endgroup$
                                                  – Gabriel Mills
                                                  Jan 20 at 22:09











                                                  4












                                                  $begingroup$


                                                  APL (Dyalog Unicode), 11 bytesSBCS





                                                  ⊢⌈{2∊⍵}⌺3 3


                                                  this is based on @dzaima's 12-byte solution in chat. credit to @Adám himself for thinking of using in the dfn, and to @H.PWiz for reminding us to use the same encoding for input and output



                                                  Try it online!



                                                  represents 'AB+' as 2 0 1 respectively



                                                  { }⌺3 3 apply a function to each overlapping 3×3 region of the input, including regions extending 1 unit outside the matrix, padded with 0s



                                                  2∊⍵ is a 2 present in the argument? return a 0/1 boolean



                                                  ⊢⌈ per-element max of that and the original matrix






                                                  share|improve this answer











                                                  $endgroup$













                                                  • $begingroup$
                                                    Of course, switching to Stencil would save you more than half of your bytes.
                                                    $endgroup$
                                                    – Adám
                                                    Jan 16 at 22:22










                                                  • $begingroup$
                                                    @Adám that would be an answer in a different language, so not comparable or competing against this answer. and i don't find golfing in special-purpose languages particularly interesting, sorry
                                                    $endgroup$
                                                    – ngn
                                                    Jan 16 at 23:24










                                                  • $begingroup$
                                                    @Adám an alias for display which i forgot to remove. removed now
                                                    $endgroup$
                                                    – ngn
                                                    Jan 17 at 2:12


















                                                  4












                                                  $begingroup$


                                                  APL (Dyalog Unicode), 11 bytesSBCS





                                                  ⊢⌈{2∊⍵}⌺3 3


                                                  this is based on @dzaima's 12-byte solution in chat. credit to @Adám himself for thinking of using in the dfn, and to @H.PWiz for reminding us to use the same encoding for input and output



                                                  Try it online!



                                                  represents 'AB+' as 2 0 1 respectively



                                                  { }⌺3 3 apply a function to each overlapping 3×3 region of the input, including regions extending 1 unit outside the matrix, padded with 0s



                                                  2∊⍵ is a 2 present in the argument? return a 0/1 boolean



                                                  ⊢⌈ per-element max of that and the original matrix






                                                  share|improve this answer











                                                  $endgroup$













                                                  • $begingroup$
                                                    Of course, switching to Stencil would save you more than half of your bytes.
                                                    $endgroup$
                                                    – Adám
                                                    Jan 16 at 22:22










                                                  • $begingroup$
                                                    @Adám that would be an answer in a different language, so not comparable or competing against this answer. and i don't find golfing in special-purpose languages particularly interesting, sorry
                                                    $endgroup$
                                                    – ngn
                                                    Jan 16 at 23:24










                                                  • $begingroup$
                                                    @Adám an alias for display which i forgot to remove. removed now
                                                    $endgroup$
                                                    – ngn
                                                    Jan 17 at 2:12
















                                                  4












                                                  4








                                                  4





                                                  $begingroup$


                                                  APL (Dyalog Unicode), 11 bytesSBCS





                                                  ⊢⌈{2∊⍵}⌺3 3


                                                  this is based on @dzaima's 12-byte solution in chat. credit to @Adám himself for thinking of using in the dfn, and to @H.PWiz for reminding us to use the same encoding for input and output



                                                  Try it online!



                                                  represents 'AB+' as 2 0 1 respectively



                                                  { }⌺3 3 apply a function to each overlapping 3×3 region of the input, including regions extending 1 unit outside the matrix, padded with 0s



                                                  2∊⍵ is a 2 present in the argument? return a 0/1 boolean



                                                  ⊢⌈ per-element max of that and the original matrix






                                                  share|improve this answer











                                                  $endgroup$




                                                  APL (Dyalog Unicode), 11 bytesSBCS





                                                  ⊢⌈{2∊⍵}⌺3 3


                                                  this is based on @dzaima's 12-byte solution in chat. credit to @Adám himself for thinking of using in the dfn, and to @H.PWiz for reminding us to use the same encoding for input and output



                                                  Try it online!



                                                  represents 'AB+' as 2 0 1 respectively



                                                  { }⌺3 3 apply a function to each overlapping 3×3 region of the input, including regions extending 1 unit outside the matrix, padded with 0s



                                                  2∊⍵ is a 2 present in the argument? return a 0/1 boolean



                                                  ⊢⌈ per-element max of that and the original matrix







                                                  share|improve this answer














                                                  share|improve this answer



                                                  share|improve this answer








                                                  edited Jan 17 at 2:15

























                                                  answered Jan 16 at 21:38









                                                  ngnngn

                                                  7,09112559




                                                  7,09112559












                                                  • $begingroup$
                                                    Of course, switching to Stencil would save you more than half of your bytes.
                                                    $endgroup$
                                                    – Adám
                                                    Jan 16 at 22:22










                                                  • $begingroup$
                                                    @Adám that would be an answer in a different language, so not comparable or competing against this answer. and i don't find golfing in special-purpose languages particularly interesting, sorry
                                                    $endgroup$
                                                    – ngn
                                                    Jan 16 at 23:24










                                                  • $begingroup$
                                                    @Adám an alias for display which i forgot to remove. removed now
                                                    $endgroup$
                                                    – ngn
                                                    Jan 17 at 2:12




















                                                  • $begingroup$
                                                    Of course, switching to Stencil would save you more than half of your bytes.
                                                    $endgroup$
                                                    – Adám
                                                    Jan 16 at 22:22










                                                  • $begingroup$
                                                    @Adám that would be an answer in a different language, so not comparable or competing against this answer. and i don't find golfing in special-purpose languages particularly interesting, sorry
                                                    $endgroup$
                                                    – ngn
                                                    Jan 16 at 23:24










                                                  • $begingroup$
                                                    @Adám an alias for display which i forgot to remove. removed now
                                                    $endgroup$
                                                    – ngn
                                                    Jan 17 at 2:12


















                                                  $begingroup$
                                                  Of course, switching to Stencil would save you more than half of your bytes.
                                                  $endgroup$
                                                  – Adám
                                                  Jan 16 at 22:22




                                                  $begingroup$
                                                  Of course, switching to Stencil would save you more than half of your bytes.
                                                  $endgroup$
                                                  – Adám
                                                  Jan 16 at 22:22












                                                  $begingroup$
                                                  @Adám that would be an answer in a different language, so not comparable or competing against this answer. and i don't find golfing in special-purpose languages particularly interesting, sorry
                                                  $endgroup$
                                                  – ngn
                                                  Jan 16 at 23:24




                                                  $begingroup$
                                                  @Adám that would be an answer in a different language, so not comparable or competing against this answer. and i don't find golfing in special-purpose languages particularly interesting, sorry
                                                  $endgroup$
                                                  – ngn
                                                  Jan 16 at 23:24












                                                  $begingroup$
                                                  @Adám an alias for display which i forgot to remove. removed now
                                                  $endgroup$
                                                  – ngn
                                                  Jan 17 at 2:12






                                                  $begingroup$
                                                  @Adám an alias for display which i forgot to remove. removed now
                                                  $endgroup$
                                                  – ngn
                                                  Jan 17 at 2:12













                                                  4












                                                  $begingroup$


                                                  Octave, 37 31 26 bytes



                                                  This function performs a morphological erosion on the Astan (1-b) part of the "image" using conv2 imerode, and then uses some arithmetic to make all three areas different symbols. Thanks @LuisMendo for -5 bytes!





                                                  @(b)2*b-imerode(b,ones(3))


                                                  Try it online!






                                                  share|improve this answer











                                                  $endgroup$









                                                  • 2




                                                    $begingroup$
                                                    -1 for no convolution :-P
                                                    $endgroup$
                                                    – Luis Mendo
                                                    Jan 16 at 19:46










                                                  • $begingroup$
                                                    @LuisMendo An earlier version did include a convolution:)
                                                    $endgroup$
                                                    – flawr
                                                    Jan 16 at 20:59










                                                  • $begingroup$
                                                    Thanks, updated!
                                                    $endgroup$
                                                    – flawr
                                                    Jan 17 at 9:15
















                                                  4












                                                  $begingroup$


                                                  Octave, 37 31 26 bytes



                                                  This function performs a morphological erosion on the Astan (1-b) part of the "image" using conv2 imerode, and then uses some arithmetic to make all three areas different symbols. Thanks @LuisMendo for -5 bytes!





                                                  @(b)2*b-imerode(b,ones(3))


                                                  Try it online!






                                                  share|improve this answer











                                                  $endgroup$









                                                  • 2




                                                    $begingroup$
                                                    -1 for no convolution :-P
                                                    $endgroup$
                                                    – Luis Mendo
                                                    Jan 16 at 19:46










                                                  • $begingroup$
                                                    @LuisMendo An earlier version did include a convolution:)
                                                    $endgroup$
                                                    – flawr
                                                    Jan 16 at 20:59










                                                  • $begingroup$
                                                    Thanks, updated!
                                                    $endgroup$
                                                    – flawr
                                                    Jan 17 at 9:15














                                                  4












                                                  4








                                                  4





                                                  $begingroup$


                                                  Octave, 37 31 26 bytes



                                                  This function performs a morphological erosion on the Astan (1-b) part of the "image" using conv2 imerode, and then uses some arithmetic to make all three areas different symbols. Thanks @LuisMendo for -5 bytes!





                                                  @(b)2*b-imerode(b,ones(3))


                                                  Try it online!






                                                  share|improve this answer











                                                  $endgroup$




                                                  Octave, 37 31 26 bytes



                                                  This function performs a morphological erosion on the Astan (1-b) part of the "image" using conv2 imerode, and then uses some arithmetic to make all three areas different symbols. Thanks @LuisMendo for -5 bytes!





                                                  @(b)2*b-imerode(b,ones(3))


                                                  Try it online!







                                                  share|improve this answer














                                                  share|improve this answer



                                                  share|improve this answer








                                                  edited Jan 17 at 9:14

























                                                  answered Jan 16 at 18:42









                                                  flawrflawr

                                                  26.7k665188




                                                  26.7k665188








                                                  • 2




                                                    $begingroup$
                                                    -1 for no convolution :-P
                                                    $endgroup$
                                                    – Luis Mendo
                                                    Jan 16 at 19:46










                                                  • $begingroup$
                                                    @LuisMendo An earlier version did include a convolution:)
                                                    $endgroup$
                                                    – flawr
                                                    Jan 16 at 20:59










                                                  • $begingroup$
                                                    Thanks, updated!
                                                    $endgroup$
                                                    – flawr
                                                    Jan 17 at 9:15














                                                  • 2




                                                    $begingroup$
                                                    -1 for no convolution :-P
                                                    $endgroup$
                                                    – Luis Mendo
                                                    Jan 16 at 19:46










                                                  • $begingroup$
                                                    @LuisMendo An earlier version did include a convolution:)
                                                    $endgroup$
                                                    – flawr
                                                    Jan 16 at 20:59










                                                  • $begingroup$
                                                    Thanks, updated!
                                                    $endgroup$
                                                    – flawr
                                                    Jan 17 at 9:15








                                                  2




                                                  2




                                                  $begingroup$
                                                  -1 for no convolution :-P
                                                  $endgroup$
                                                  – Luis Mendo
                                                  Jan 16 at 19:46




                                                  $begingroup$
                                                  -1 for no convolution :-P
                                                  $endgroup$
                                                  – Luis Mendo
                                                  Jan 16 at 19:46












                                                  $begingroup$
                                                  @LuisMendo An earlier version did include a convolution:)
                                                  $endgroup$
                                                  – flawr
                                                  Jan 16 at 20:59




                                                  $begingroup$
                                                  @LuisMendo An earlier version did include a convolution:)
                                                  $endgroup$
                                                  – flawr
                                                  Jan 16 at 20:59












                                                  $begingroup$
                                                  Thanks, updated!
                                                  $endgroup$
                                                  – flawr
                                                  Jan 17 at 9:15




                                                  $begingroup$
                                                  Thanks, updated!
                                                  $endgroup$
                                                  – flawr
                                                  Jan 17 at 9:15











                                                  2












                                                  $begingroup$


                                                  Charcoal, 20 bytes



                                                  ≔⪫θ⸿θPθFθ⎇∧№KMA⁼Bι+ι


                                                  Try it online! Link is to verbose version of code. Explanation:



                                                  ≔⪫θ⸿θ


                                                  Join the input array with carriage returns rather than the usual newlines. This is needed so that the characters can be printed individually.



                                                  Pθ


                                                  Print the input string without moving the cursor.



                                                  Fθ


                                                  Loop over each character of the input string.



                                                  ⎇∧№KMA⁼Bι


                                                  If the Moore neighbourhood contains an A, and the current character is a B...



                                                  +


                                                  ... then overwrite the B with a +...



                                                  ι


                                                  ... otherwise print the current character (or move to the next line if the current character is a carriage return).






                                                  share|improve this answer









                                                  $endgroup$


















                                                    2












                                                    $begingroup$


                                                    Charcoal, 20 bytes



                                                    ≔⪫θ⸿θPθFθ⎇∧№KMA⁼Bι+ι


                                                    Try it online! Link is to verbose version of code. Explanation:



                                                    ≔⪫θ⸿θ


                                                    Join the input array with carriage returns rather than the usual newlines. This is needed so that the characters can be printed individually.



                                                    Pθ


                                                    Print the input string without moving the cursor.



                                                    Fθ


                                                    Loop over each character of the input string.



                                                    ⎇∧№KMA⁼Bι


                                                    If the Moore neighbourhood contains an A, and the current character is a B...



                                                    +


                                                    ... then overwrite the B with a +...



                                                    ι


                                                    ... otherwise print the current character (or move to the next line if the current character is a carriage return).






                                                    share|improve this answer









                                                    $endgroup$
















                                                      2












                                                      2








                                                      2





                                                      $begingroup$


                                                      Charcoal, 20 bytes



                                                      ≔⪫θ⸿θPθFθ⎇∧№KMA⁼Bι+ι


                                                      Try it online! Link is to verbose version of code. Explanation:



                                                      ≔⪫θ⸿θ


                                                      Join the input array with carriage returns rather than the usual newlines. This is needed so that the characters can be printed individually.



                                                      Pθ


                                                      Print the input string without moving the cursor.



                                                      Fθ


                                                      Loop over each character of the input string.



                                                      ⎇∧№KMA⁼Bι


                                                      If the Moore neighbourhood contains an A, and the current character is a B...



                                                      +


                                                      ... then overwrite the B with a +...



                                                      ι


                                                      ... otherwise print the current character (or move to the next line if the current character is a carriage return).






                                                      share|improve this answer









                                                      $endgroup$




                                                      Charcoal, 20 bytes



                                                      ≔⪫θ⸿θPθFθ⎇∧№KMA⁼Bι+ι


                                                      Try it online! Link is to verbose version of code. Explanation:



                                                      ≔⪫θ⸿θ


                                                      Join the input array with carriage returns rather than the usual newlines. This is needed so that the characters can be printed individually.



                                                      Pθ


                                                      Print the input string without moving the cursor.



                                                      Fθ


                                                      Loop over each character of the input string.



                                                      ⎇∧№KMA⁼Bι


                                                      If the Moore neighbourhood contains an A, and the current character is a B...



                                                      +


                                                      ... then overwrite the B with a +...



                                                      ι


                                                      ... otherwise print the current character (or move to the next line if the current character is a carriage return).







                                                      share|improve this answer












                                                      share|improve this answer



                                                      share|improve this answer










                                                      answered Jan 16 at 22:21









                                                      NeilNeil

                                                      80k744178




                                                      80k744178























                                                          2












                                                          $begingroup$


                                                          J, 28 bytes



                                                          >.3 3(2 e.,);._3(0|:@,|.)^:4


                                                          Try it online!



                                                          'AB+' -> 2 0 1



                                                          Inspired by ngn's APL solution. 12 bytes just to pad the matrix with zeroes...






                                                          share|improve this answer









                                                          $endgroup$


















                                                            2












                                                            $begingroup$


                                                            J, 28 bytes



                                                            >.3 3(2 e.,);._3(0|:@,|.)^:4


                                                            Try it online!



                                                            'AB+' -> 2 0 1



                                                            Inspired by ngn's APL solution. 12 bytes just to pad the matrix with zeroes...






                                                            share|improve this answer









                                                            $endgroup$
















                                                              2












                                                              2








                                                              2





                                                              $begingroup$


                                                              J, 28 bytes



                                                              >.3 3(2 e.,);._3(0|:@,|.)^:4


                                                              Try it online!



                                                              'AB+' -> 2 0 1



                                                              Inspired by ngn's APL solution. 12 bytes just to pad the matrix with zeroes...






                                                              share|improve this answer









                                                              $endgroup$




                                                              J, 28 bytes



                                                              >.3 3(2 e.,);._3(0|:@,|.)^:4


                                                              Try it online!



                                                              'AB+' -> 2 0 1



                                                              Inspired by ngn's APL solution. 12 bytes just to pad the matrix with zeroes...







                                                              share|improve this answer












                                                              share|improve this answer



                                                              share|improve this answer










                                                              answered Jan 17 at 9:27









                                                              Galen IvanovGalen Ivanov

                                                              6,59711032




                                                              6,59711032























                                                                  2












                                                                  $begingroup$

                                                                  Javascript, 126 118 bytes



                                                                  _=>_.map(x=>x.replace(/(?<=A)B|B(?=A)/g,0)).map((x,i,a)=>[...x].map((v,j)=>v>'A'&&(a[i-1][j]<v|(a[i+1]||x)[j]<v)?0:v))


                                                                  Pass in one of the string arrays from the question, and you'll get an array of strings character arrays (thanks @Shaggy!) out using 0 for the trench. Can probably be golfed more (without switching over to numerical arrays), but I can't think of anything at the moment.






                                                                  share|improve this answer











                                                                  $endgroup$













                                                                  • $begingroup$
                                                                    I think this works for 120 bytes.
                                                                    $endgroup$
                                                                    – Shaggy
                                                                    Jan 18 at 12:43










                                                                  • $begingroup$
                                                                    Or 116 bytes returning an array of character arrays.
                                                                    $endgroup$
                                                                    – Shaggy
                                                                    Jan 18 at 12:43






                                                                  • 1




                                                                    $begingroup$
                                                                    @Shaggy Your golf doesn't work, sadly - it doesn't catch places where A's and B's are diagonal to each-other. On the other hand, it does point out some really simple golfs that I missed...
                                                                    $endgroup$
                                                                    – M Dirr
                                                                    Jan 18 at 15:07
















                                                                  2












                                                                  $begingroup$

                                                                  Javascript, 126 118 bytes



                                                                  _=>_.map(x=>x.replace(/(?<=A)B|B(?=A)/g,0)).map((x,i,a)=>[...x].map((v,j)=>v>'A'&&(a[i-1][j]<v|(a[i+1]||x)[j]<v)?0:v))


                                                                  Pass in one of the string arrays from the question, and you'll get an array of strings character arrays (thanks @Shaggy!) out using 0 for the trench. Can probably be golfed more (without switching over to numerical arrays), but I can't think of anything at the moment.






                                                                  share|improve this answer











                                                                  $endgroup$













                                                                  • $begingroup$
                                                                    I think this works for 120 bytes.
                                                                    $endgroup$
                                                                    – Shaggy
                                                                    Jan 18 at 12:43










                                                                  • $begingroup$
                                                                    Or 116 bytes returning an array of character arrays.
                                                                    $endgroup$
                                                                    – Shaggy
                                                                    Jan 18 at 12:43






                                                                  • 1




                                                                    $begingroup$
                                                                    @Shaggy Your golf doesn't work, sadly - it doesn't catch places where A's and B's are diagonal to each-other. On the other hand, it does point out some really simple golfs that I missed...
                                                                    $endgroup$
                                                                    – M Dirr
                                                                    Jan 18 at 15:07














                                                                  2












                                                                  2








                                                                  2





                                                                  $begingroup$

                                                                  Javascript, 126 118 bytes



                                                                  _=>_.map(x=>x.replace(/(?<=A)B|B(?=A)/g,0)).map((x,i,a)=>[...x].map((v,j)=>v>'A'&&(a[i-1][j]<v|(a[i+1]||x)[j]<v)?0:v))


                                                                  Pass in one of the string arrays from the question, and you'll get an array of strings character arrays (thanks @Shaggy!) out using 0 for the trench. Can probably be golfed more (without switching over to numerical arrays), but I can't think of anything at the moment.






                                                                  share|improve this answer











                                                                  $endgroup$



                                                                  Javascript, 126 118 bytes



                                                                  _=>_.map(x=>x.replace(/(?<=A)B|B(?=A)/g,0)).map((x,i,a)=>[...x].map((v,j)=>v>'A'&&(a[i-1][j]<v|(a[i+1]||x)[j]<v)?0:v))


                                                                  Pass in one of the string arrays from the question, and you'll get an array of strings character arrays (thanks @Shaggy!) out using 0 for the trench. Can probably be golfed more (without switching over to numerical arrays), but I can't think of anything at the moment.







                                                                  share|improve this answer














                                                                  share|improve this answer



                                                                  share|improve this answer








                                                                  edited Jan 18 at 15:12

























                                                                  answered Jan 17 at 23:42









                                                                  M DirrM Dirr

                                                                  412




                                                                  412












                                                                  • $begingroup$
                                                                    I think this works for 120 bytes.
                                                                    $endgroup$
                                                                    – Shaggy
                                                                    Jan 18 at 12:43










                                                                  • $begingroup$
                                                                    Or 116 bytes returning an array of character arrays.
                                                                    $endgroup$
                                                                    – Shaggy
                                                                    Jan 18 at 12:43






                                                                  • 1




                                                                    $begingroup$
                                                                    @Shaggy Your golf doesn't work, sadly - it doesn't catch places where A's and B's are diagonal to each-other. On the other hand, it does point out some really simple golfs that I missed...
                                                                    $endgroup$
                                                                    – M Dirr
                                                                    Jan 18 at 15:07


















                                                                  • $begingroup$
                                                                    I think this works for 120 bytes.
                                                                    $endgroup$
                                                                    – Shaggy
                                                                    Jan 18 at 12:43










                                                                  • $begingroup$
                                                                    Or 116 bytes returning an array of character arrays.
                                                                    $endgroup$
                                                                    – Shaggy
                                                                    Jan 18 at 12:43






                                                                  • 1




                                                                    $begingroup$
                                                                    @Shaggy Your golf doesn't work, sadly - it doesn't catch places where A's and B's are diagonal to each-other. On the other hand, it does point out some really simple golfs that I missed...
                                                                    $endgroup$
                                                                    – M Dirr
                                                                    Jan 18 at 15:07
















                                                                  $begingroup$
                                                                  I think this works for 120 bytes.
                                                                  $endgroup$
                                                                  – Shaggy
                                                                  Jan 18 at 12:43




                                                                  $begingroup$
                                                                  I think this works for 120 bytes.
                                                                  $endgroup$
                                                                  – Shaggy
                                                                  Jan 18 at 12:43












                                                                  $begingroup$
                                                                  Or 116 bytes returning an array of character arrays.
                                                                  $endgroup$
                                                                  – Shaggy
                                                                  Jan 18 at 12:43




                                                                  $begingroup$
                                                                  Or 116 bytes returning an array of character arrays.
                                                                  $endgroup$
                                                                  – Shaggy
                                                                  Jan 18 at 12:43




                                                                  1




                                                                  1




                                                                  $begingroup$
                                                                  @Shaggy Your golf doesn't work, sadly - it doesn't catch places where A's and B's are diagonal to each-other. On the other hand, it does point out some really simple golfs that I missed...
                                                                  $endgroup$
                                                                  – M Dirr
                                                                  Jan 18 at 15:07




                                                                  $begingroup$
                                                                  @Shaggy Your golf doesn't work, sadly - it doesn't catch places where A's and B's are diagonal to each-other. On the other hand, it does point out some really simple golfs that I missed...
                                                                  $endgroup$
                                                                  – M Dirr
                                                                  Jan 18 at 15:07











                                                                  1












                                                                  $begingroup$


                                                                  Retina 0.8.2, 92 80 bytes



                                                                  (?<=¶(.)*)B(?=.*¶(?<-1>.)*(?(1)_)A|(?<=¶(?(1)_)(?<-1>.)*A.*¶.*))
                                                                  a
                                                                  iT`Ba`+`.?a.?


                                                                  Try it online! Loosely based on my answer to Will I make it out in time? Explanation: Any Bs immediately above or below As are turned into as. This then reduces the problem to checking Bs to the left or right of As or as. The as themselves also need to get turned into +s of course, but fortunately the i flag to T only affects the regex match, not the actual transliteration, so the As remain unaffected.






                                                                  share|improve this answer











                                                                  $endgroup$


















                                                                    1












                                                                    $begingroup$


                                                                    Retina 0.8.2, 92 80 bytes



                                                                    (?<=¶(.)*)B(?=.*¶(?<-1>.)*(?(1)_)A|(?<=¶(?(1)_)(?<-1>.)*A.*¶.*))
                                                                    a
                                                                    iT`Ba`+`.?a.?


                                                                    Try it online! Loosely based on my answer to Will I make it out in time? Explanation: Any Bs immediately above or below As are turned into as. This then reduces the problem to checking Bs to the left or right of As or as. The as themselves also need to get turned into +s of course, but fortunately the i flag to T only affects the regex match, not the actual transliteration, so the As remain unaffected.






                                                                    share|improve this answer











                                                                    $endgroup$
















                                                                      1












                                                                      1








                                                                      1





                                                                      $begingroup$


                                                                      Retina 0.8.2, 92 80 bytes



                                                                      (?<=¶(.)*)B(?=.*¶(?<-1>.)*(?(1)_)A|(?<=¶(?(1)_)(?<-1>.)*A.*¶.*))
                                                                      a
                                                                      iT`Ba`+`.?a.?


                                                                      Try it online! Loosely based on my answer to Will I make it out in time? Explanation: Any Bs immediately above or below As are turned into as. This then reduces the problem to checking Bs to the left or right of As or as. The as themselves also need to get turned into +s of course, but fortunately the i flag to T only affects the regex match, not the actual transliteration, so the As remain unaffected.






                                                                      share|improve this answer











                                                                      $endgroup$




                                                                      Retina 0.8.2, 92 80 bytes



                                                                      (?<=¶(.)*)B(?=.*¶(?<-1>.)*(?(1)_)A|(?<=¶(?(1)_)(?<-1>.)*A.*¶.*))
                                                                      a
                                                                      iT`Ba`+`.?a.?


                                                                      Try it online! Loosely based on my answer to Will I make it out in time? Explanation: Any Bs immediately above or below As are turned into as. This then reduces the problem to checking Bs to the left or right of As or as. The as themselves also need to get turned into +s of course, but fortunately the i flag to T only affects the regex match, not the actual transliteration, so the As remain unaffected.







                                                                      share|improve this answer














                                                                      share|improve this answer



                                                                      share|improve this answer








                                                                      edited Jan 16 at 23:03

























                                                                      answered Jan 16 at 22:45









                                                                      NeilNeil

                                                                      80k744178




                                                                      80k744178























                                                                          1












                                                                          $begingroup$


                                                                          C# (Visual C# Interactive Compiler), 187 bytes





                                                                          a=>a.Select((b,i)=>b.Select((c,j)=>{int k=0;for(int x=Math.Max(0,i-1);x<Math.Min(i+2,a.Count);x++)for(int y=Math.Max(0,j-1);y<Math.Min(j+2,a[0].Count);)k+=a[x][y++];return k>1&c<1?9:c;}))


                                                                          Instead of chaining Take()s, Skip()s, and Select()s, instead this uses double for loops to find neighbors. HUGE byte decrease, from 392 bytes to 187. Linq isn't always the shortest!



                                                                          Try it online!






                                                                          share|improve this answer











                                                                          $endgroup$


















                                                                            1












                                                                            $begingroup$


                                                                            C# (Visual C# Interactive Compiler), 187 bytes





                                                                            a=>a.Select((b,i)=>b.Select((c,j)=>{int k=0;for(int x=Math.Max(0,i-1);x<Math.Min(i+2,a.Count);x++)for(int y=Math.Max(0,j-1);y<Math.Min(j+2,a[0].Count);)k+=a[x][y++];return k>1&c<1?9:c;}))


                                                                            Instead of chaining Take()s, Skip()s, and Select()s, instead this uses double for loops to find neighbors. HUGE byte decrease, from 392 bytes to 187. Linq isn't always the shortest!



                                                                            Try it online!






                                                                            share|improve this answer











                                                                            $endgroup$
















                                                                              1












                                                                              1








                                                                              1





                                                                              $begingroup$


                                                                              C# (Visual C# Interactive Compiler), 187 bytes





                                                                              a=>a.Select((b,i)=>b.Select((c,j)=>{int k=0;for(int x=Math.Max(0,i-1);x<Math.Min(i+2,a.Count);x++)for(int y=Math.Max(0,j-1);y<Math.Min(j+2,a[0].Count);)k+=a[x][y++];return k>1&c<1?9:c;}))


                                                                              Instead of chaining Take()s, Skip()s, and Select()s, instead this uses double for loops to find neighbors. HUGE byte decrease, from 392 bytes to 187. Linq isn't always the shortest!



                                                                              Try it online!






                                                                              share|improve this answer











                                                                              $endgroup$




                                                                              C# (Visual C# Interactive Compiler), 187 bytes





                                                                              a=>a.Select((b,i)=>b.Select((c,j)=>{int k=0;for(int x=Math.Max(0,i-1);x<Math.Min(i+2,a.Count);x++)for(int y=Math.Max(0,j-1);y<Math.Min(j+2,a[0].Count);)k+=a[x][y++];return k>1&c<1?9:c;}))


                                                                              Instead of chaining Take()s, Skip()s, and Select()s, instead this uses double for loops to find neighbors. HUGE byte decrease, from 392 bytes to 187. Linq isn't always the shortest!



                                                                              Try it online!







                                                                              share|improve this answer














                                                                              share|improve this answer



                                                                              share|improve this answer








                                                                              edited Jan 18 at 3:16

























                                                                              answered Jan 16 at 22:51









                                                                              Embodiment of IgnoranceEmbodiment of Ignorance

                                                                              721115




                                                                              721115























                                                                                  1












                                                                                  $begingroup$

                                                                                  Perl 5, 58 46 bytes



                                                                                  $m=($n=/$/m+"@+")-2;s/A(|.{$m,$n})KB|B(?=(?1)A)/+/s&&redo


                                                                                  TIO



                                                                                  -12 bytes thanks to @Grimy



                                                                                  /.
                                                                                  /;s/A(|.{@{-}}.?.?)KB|B(?=(?1)A)/+/s&&redo


                                                                                  TIO





                                                                                  • -p like -n but print also


                                                                                  • -00 paragraph mode

                                                                                  • to get the width-1 /.n/ matches the last character of first line


                                                                                  • @{-} special array the position of start of match of previous matched groups, coerced as string (first element)


                                                                                  • s/../+/s&&redo replace match by + while matches



                                                                                    • /s flag, so that . matches also newline character




                                                                                  • A(|.{@{-}}.?.?)KB matches



                                                                                    • AB or A followed by (width-1) to (width+1) characters folowed by B


                                                                                    • K to keep the left of B unchanged




                                                                                  • B(?=(?1)A),



                                                                                    • (?1) dirverting recursive, to reference previous expression (|.{$m,$o})


                                                                                    • (?=..) lookahead, to match without consuming input








                                                                                  share|improve this answer











                                                                                  $endgroup$













                                                                                  • $begingroup$
                                                                                    -9 bytes with /. /,@m=@-while s/A(|.{@m}.?.?)KB|B(?=(?1)A)/+/s (literal newline in the first regex). TIO
                                                                                    $endgroup$
                                                                                    – Grimy
                                                                                    Jan 18 at 10:32






                                                                                  • 1




                                                                                    $begingroup$
                                                                                    Down to 46: /. /;s/A(|.{@{-}}.?.?)KB|B(?=(?1)A)/+/s&&redo. TIO
                                                                                    $endgroup$
                                                                                    – Grimy
                                                                                    Jan 18 at 10:50










                                                                                  • $begingroup$
                                                                                    thanks, i also had the idea, but discarded because was thinking to catastrophic backtracking however for code golf performance is not important
                                                                                    $endgroup$
                                                                                    – Nahuel Fouilleul
                                                                                    Jan 18 at 11:14
















                                                                                  1












                                                                                  $begingroup$

                                                                                  Perl 5, 58 46 bytes



                                                                                  $m=($n=/$/m+"@+")-2;s/A(|.{$m,$n})KB|B(?=(?1)A)/+/s&&redo


                                                                                  TIO



                                                                                  -12 bytes thanks to @Grimy



                                                                                  /.
                                                                                  /;s/A(|.{@{-}}.?.?)KB|B(?=(?1)A)/+/s&&redo


                                                                                  TIO





                                                                                  • -p like -n but print also


                                                                                  • -00 paragraph mode

                                                                                  • to get the width-1 /.n/ matches the last character of first line


                                                                                  • @{-} special array the position of start of match of previous matched groups, coerced as string (first element)


                                                                                  • s/../+/s&&redo replace match by + while matches



                                                                                    • /s flag, so that . matches also newline character




                                                                                  • A(|.{@{-}}.?.?)KB matches



                                                                                    • AB or A followed by (width-1) to (width+1) characters folowed by B


                                                                                    • K to keep the left of B unchanged




                                                                                  • B(?=(?1)A),



                                                                                    • (?1) dirverting recursive, to reference previous expression (|.{$m,$o})


                                                                                    • (?=..) lookahead, to match without consuming input








                                                                                  share|improve this answer











                                                                                  $endgroup$













                                                                                  • $begingroup$
                                                                                    -9 bytes with /. /,@m=@-while s/A(|.{@m}.?.?)KB|B(?=(?1)A)/+/s (literal newline in the first regex). TIO
                                                                                    $endgroup$
                                                                                    – Grimy
                                                                                    Jan 18 at 10:32






                                                                                  • 1




                                                                                    $begingroup$
                                                                                    Down to 46: /. /;s/A(|.{@{-}}.?.?)KB|B(?=(?1)A)/+/s&&redo. TIO
                                                                                    $endgroup$
                                                                                    – Grimy
                                                                                    Jan 18 at 10:50










                                                                                  • $begingroup$
                                                                                    thanks, i also had the idea, but discarded because was thinking to catastrophic backtracking however for code golf performance is not important
                                                                                    $endgroup$
                                                                                    – Nahuel Fouilleul
                                                                                    Jan 18 at 11:14














                                                                                  1












                                                                                  1








                                                                                  1





                                                                                  $begingroup$

                                                                                  Perl 5, 58 46 bytes



                                                                                  $m=($n=/$/m+"@+")-2;s/A(|.{$m,$n})KB|B(?=(?1)A)/+/s&&redo


                                                                                  TIO



                                                                                  -12 bytes thanks to @Grimy



                                                                                  /.
                                                                                  /;s/A(|.{@{-}}.?.?)KB|B(?=(?1)A)/+/s&&redo


                                                                                  TIO





                                                                                  • -p like -n but print also


                                                                                  • -00 paragraph mode

                                                                                  • to get the width-1 /.n/ matches the last character of first line


                                                                                  • @{-} special array the position of start of match of previous matched groups, coerced as string (first element)


                                                                                  • s/../+/s&&redo replace match by + while matches



                                                                                    • /s flag, so that . matches also newline character




                                                                                  • A(|.{@{-}}.?.?)KB matches



                                                                                    • AB or A followed by (width-1) to (width+1) characters folowed by B


                                                                                    • K to keep the left of B unchanged




                                                                                  • B(?=(?1)A),



                                                                                    • (?1) dirverting recursive, to reference previous expression (|.{$m,$o})


                                                                                    • (?=..) lookahead, to match without consuming input








                                                                                  share|improve this answer











                                                                                  $endgroup$



                                                                                  Perl 5, 58 46 bytes



                                                                                  $m=($n=/$/m+"@+")-2;s/A(|.{$m,$n})KB|B(?=(?1)A)/+/s&&redo


                                                                                  TIO



                                                                                  -12 bytes thanks to @Grimy



                                                                                  /.
                                                                                  /;s/A(|.{@{-}}.?.?)KB|B(?=(?1)A)/+/s&&redo


                                                                                  TIO





                                                                                  • -p like -n but print also


                                                                                  • -00 paragraph mode

                                                                                  • to get the width-1 /.n/ matches the last character of first line


                                                                                  • @{-} special array the position of start of match of previous matched groups, coerced as string (first element)


                                                                                  • s/../+/s&&redo replace match by + while matches



                                                                                    • /s flag, so that . matches also newline character




                                                                                  • A(|.{@{-}}.?.?)KB matches



                                                                                    • AB or A followed by (width-1) to (width+1) characters folowed by B


                                                                                    • K to keep the left of B unchanged




                                                                                  • B(?=(?1)A),



                                                                                    • (?1) dirverting recursive, to reference previous expression (|.{$m,$o})


                                                                                    • (?=..) lookahead, to match without consuming input









                                                                                  share|improve this answer














                                                                                  share|improve this answer



                                                                                  share|improve this answer








                                                                                  edited Jan 18 at 11:53

























                                                                                  answered Jan 17 at 13:40









                                                                                  Nahuel FouilleulNahuel Fouilleul

                                                                                  1,83028




                                                                                  1,83028












                                                                                  • $begingroup$
                                                                                    -9 bytes with /. /,@m=@-while s/A(|.{@m}.?.?)KB|B(?=(?1)A)/+/s (literal newline in the first regex). TIO
                                                                                    $endgroup$
                                                                                    – Grimy
                                                                                    Jan 18 at 10:32






                                                                                  • 1




                                                                                    $begingroup$
                                                                                    Down to 46: /. /;s/A(|.{@{-}}.?.?)KB|B(?=(?1)A)/+/s&&redo. TIO
                                                                                    $endgroup$
                                                                                    – Grimy
                                                                                    Jan 18 at 10:50










                                                                                  • $begingroup$
                                                                                    thanks, i also had the idea, but discarded because was thinking to catastrophic backtracking however for code golf performance is not important
                                                                                    $endgroup$
                                                                                    – Nahuel Fouilleul
                                                                                    Jan 18 at 11:14


















                                                                                  • $begingroup$
                                                                                    -9 bytes with /. /,@m=@-while s/A(|.{@m}.?.?)KB|B(?=(?1)A)/+/s (literal newline in the first regex). TIO
                                                                                    $endgroup$
                                                                                    – Grimy
                                                                                    Jan 18 at 10:32






                                                                                  • 1




                                                                                    $begingroup$
                                                                                    Down to 46: /. /;s/A(|.{@{-}}.?.?)KB|B(?=(?1)A)/+/s&&redo. TIO
                                                                                    $endgroup$
                                                                                    – Grimy
                                                                                    Jan 18 at 10:50










                                                                                  • $begingroup$
                                                                                    thanks, i also had the idea, but discarded because was thinking to catastrophic backtracking however for code golf performance is not important
                                                                                    $endgroup$
                                                                                    – Nahuel Fouilleul
                                                                                    Jan 18 at 11:14
















                                                                                  $begingroup$
                                                                                  -9 bytes with /. /,@m=@-while s/A(|.{@m}.?.?)KB|B(?=(?1)A)/+/s (literal newline in the first regex). TIO
                                                                                  $endgroup$
                                                                                  – Grimy
                                                                                  Jan 18 at 10:32




                                                                                  $begingroup$
                                                                                  -9 bytes with /. /,@m=@-while s/A(|.{@m}.?.?)KB|B(?=(?1)A)/+/s (literal newline in the first regex). TIO
                                                                                  $endgroup$
                                                                                  – Grimy
                                                                                  Jan 18 at 10:32




                                                                                  1




                                                                                  1




                                                                                  $begingroup$
                                                                                  Down to 46: /. /;s/A(|.{@{-}}.?.?)KB|B(?=(?1)A)/+/s&&redo. TIO
                                                                                  $endgroup$
                                                                                  – Grimy
                                                                                  Jan 18 at 10:50




                                                                                  $begingroup$
                                                                                  Down to 46: /. /;s/A(|.{@{-}}.?.?)KB|B(?=(?1)A)/+/s&&redo. TIO
                                                                                  $endgroup$
                                                                                  – Grimy
                                                                                  Jan 18 at 10:50












                                                                                  $begingroup$
                                                                                  thanks, i also had the idea, but discarded because was thinking to catastrophic backtracking however for code golf performance is not important
                                                                                  $endgroup$
                                                                                  – Nahuel Fouilleul
                                                                                  Jan 18 at 11:14




                                                                                  $begingroup$
                                                                                  thanks, i also had the idea, but discarded because was thinking to catastrophic backtracking however for code golf performance is not important
                                                                                  $endgroup$
                                                                                  – Nahuel Fouilleul
                                                                                  Jan 18 at 11:14











                                                                                  1












                                                                                  $begingroup$

                                                                                  JavaScript, 85 bytes



                                                                                  Threw this together late last night and forgot about it. Probably still room for some improvement somewhere.



                                                                                  Input and output is as an array of digit arrays, using 3 for Astan, 0 for Blandia & 1 for the trench.



                                                                                  a=>a.map((x,i)=>x.map((y,j)=>o.map(v=>o.map(h=>y|=1&(a[i+v]||x)[j+h]))|y),o=[-1,0,1])


                                                                                  Try it online (For convenience, maps from & back to the I/O format used in the challenge)






                                                                                  share|improve this answer











                                                                                  $endgroup$


















                                                                                    1












                                                                                    $begingroup$

                                                                                    JavaScript, 85 bytes



                                                                                    Threw this together late last night and forgot about it. Probably still room for some improvement somewhere.



                                                                                    Input and output is as an array of digit arrays, using 3 for Astan, 0 for Blandia & 1 for the trench.



                                                                                    a=>a.map((x,i)=>x.map((y,j)=>o.map(v=>o.map(h=>y|=1&(a[i+v]||x)[j+h]))|y),o=[-1,0,1])


                                                                                    Try it online (For convenience, maps from & back to the I/O format used in the challenge)






                                                                                    share|improve this answer











                                                                                    $endgroup$
















                                                                                      1












                                                                                      1








                                                                                      1





                                                                                      $begingroup$

                                                                                      JavaScript, 85 bytes



                                                                                      Threw this together late last night and forgot about it. Probably still room for some improvement somewhere.



                                                                                      Input and output is as an array of digit arrays, using 3 for Astan, 0 for Blandia & 1 for the trench.



                                                                                      a=>a.map((x,i)=>x.map((y,j)=>o.map(v=>o.map(h=>y|=1&(a[i+v]||x)[j+h]))|y),o=[-1,0,1])


                                                                                      Try it online (For convenience, maps from & back to the I/O format used in the challenge)






                                                                                      share|improve this answer











                                                                                      $endgroup$



                                                                                      JavaScript, 85 bytes



                                                                                      Threw this together late last night and forgot about it. Probably still room for some improvement somewhere.



                                                                                      Input and output is as an array of digit arrays, using 3 for Astan, 0 for Blandia & 1 for the trench.



                                                                                      a=>a.map((x,i)=>x.map((y,j)=>o.map(v=>o.map(h=>y|=1&(a[i+v]||x)[j+h]))|y),o=[-1,0,1])


                                                                                      Try it online (For convenience, maps from & back to the I/O format used in the challenge)







                                                                                      share|improve this answer














                                                                                      share|improve this answer



                                                                                      share|improve this answer








                                                                                      edited Jan 18 at 12:35

























                                                                                      answered Jan 17 at 11:57









                                                                                      ShaggyShaggy

                                                                                      19.4k21666




                                                                                      19.4k21666























                                                                                          1












                                                                                          $begingroup$

                                                                                          Java 8, 169 145 bytes





                                                                                          m->{for(int i=m.length,j,k;i-->0;)for(j=m[i].length;j-->0;)for(k=9;m[i][j]==1&k-->0;)try{m[i][j]=m[i+k/3-1][j+k%3-1]<1?2:1;}catch(Exception e){}}


                                                                                          -24 bytes thanks to @OlivierGrégoire.



                                                                                          Uses 0 instead of A and 1 instead of B, with the input being a 2D integer-matrix. Modifies the input-matrix instead of returning a new one to save bytes.



                                                                                          The cells are checked the same as in my answer for the All the single eights challenge.



                                                                                          Try it online.



                                                                                          Explanation:



                                                                                          m->{                            // Method with integer-matrix parameter and no return-type
                                                                                          for(int i=m.length,j,k;i-->0;)// Loop over the rows
                                                                                          for(j=m[i].length;j-->0;) // Inner loop over the columns
                                                                                          for(k=9;m[i][j]==1& // If the current cell contains a 1:
                                                                                          k-->0;) // Inner loop `k` in the range (9, 0]:
                                                                                          try{m[i][j]= // Set the current cell to:
                                                                                          m[i+k/3-1] // If `k` is 0, 1, or 2: Look at the previous row
                                                                                          // Else-if `k` is 6, 7, or 8: Look at the next row
                                                                                          // Else (`k` is 3, 4, or 5): Look at the current row
                                                                                          [j+k%3-1] // If `k` is 0, 3, or 6: Look at the previous column
                                                                                          // Else-if `k` is 2, 5, or 8: Look at the next column
                                                                                          // Else (`k` is 1, 4, or 7): Look at the current column
                                                                                          <1? // And if this cell contains a 0:
                                                                                          2 // Change the current cell from a 1 to a 2
                                                                                          : // Else:
                                                                                          1; // Leave it a 1
                                                                                          }catch(Exception e){}} // Catch and ignore ArrayIndexOutOfBoundsExceptions
                                                                                          // (try-catch saves bytes in comparison to if-checks)





                                                                                          share|improve this answer











                                                                                          $endgroup$









                                                                                          • 1




                                                                                            $begingroup$
                                                                                            I haven't checked much, but is there anything wrong with m[i+k/3-1][j+k%3-1]? 145 bytes
                                                                                            $endgroup$
                                                                                            – Olivier Grégoire
                                                                                            Jan 18 at 12:31










                                                                                          • $begingroup$
                                                                                            @OlivierGrégoire Dang, that's so much easier.. Thanks!
                                                                                            $endgroup$
                                                                                            – Kevin Cruijssen
                                                                                            Jan 18 at 12:44










                                                                                          • $begingroup$
                                                                                            I think it's also valid for your answers of previous challenges given that they seem to have the same structure
                                                                                            $endgroup$
                                                                                            – Olivier Grégoire
                                                                                            Jan 18 at 12:51










                                                                                          • $begingroup$
                                                                                            @OlivierGrégoire Yeah, I was about to golf those as well with your suggestion, but then another comment (and a question at work) came in between. Will do so in a moment.
                                                                                            $endgroup$
                                                                                            – Kevin Cruijssen
                                                                                            Jan 18 at 13:00
















                                                                                          1












                                                                                          $begingroup$

                                                                                          Java 8, 169 145 bytes





                                                                                          m->{for(int i=m.length,j,k;i-->0;)for(j=m[i].length;j-->0;)for(k=9;m[i][j]==1&k-->0;)try{m[i][j]=m[i+k/3-1][j+k%3-1]<1?2:1;}catch(Exception e){}}


                                                                                          -24 bytes thanks to @OlivierGrégoire.



                                                                                          Uses 0 instead of A and 1 instead of B, with the input being a 2D integer-matrix. Modifies the input-matrix instead of returning a new one to save bytes.



                                                                                          The cells are checked the same as in my answer for the All the single eights challenge.



                                                                                          Try it online.



                                                                                          Explanation:



                                                                                          m->{                            // Method with integer-matrix parameter and no return-type
                                                                                          for(int i=m.length,j,k;i-->0;)// Loop over the rows
                                                                                          for(j=m[i].length;j-->0;) // Inner loop over the columns
                                                                                          for(k=9;m[i][j]==1& // If the current cell contains a 1:
                                                                                          k-->0;) // Inner loop `k` in the range (9, 0]:
                                                                                          try{m[i][j]= // Set the current cell to:
                                                                                          m[i+k/3-1] // If `k` is 0, 1, or 2: Look at the previous row
                                                                                          // Else-if `k` is 6, 7, or 8: Look at the next row
                                                                                          // Else (`k` is 3, 4, or 5): Look at the current row
                                                                                          [j+k%3-1] // If `k` is 0, 3, or 6: Look at the previous column
                                                                                          // Else-if `k` is 2, 5, or 8: Look at the next column
                                                                                          // Else (`k` is 1, 4, or 7): Look at the current column
                                                                                          <1? // And if this cell contains a 0:
                                                                                          2 // Change the current cell from a 1 to a 2
                                                                                          : // Else:
                                                                                          1; // Leave it a 1
                                                                                          }catch(Exception e){}} // Catch and ignore ArrayIndexOutOfBoundsExceptions
                                                                                          // (try-catch saves bytes in comparison to if-checks)





                                                                                          share|improve this answer











                                                                                          $endgroup$









                                                                                          • 1




                                                                                            $begingroup$
                                                                                            I haven't checked much, but is there anything wrong with m[i+k/3-1][j+k%3-1]? 145 bytes
                                                                                            $endgroup$
                                                                                            – Olivier Grégoire
                                                                                            Jan 18 at 12:31










                                                                                          • $begingroup$
                                                                                            @OlivierGrégoire Dang, that's so much easier.. Thanks!
                                                                                            $endgroup$
                                                                                            – Kevin Cruijssen
                                                                                            Jan 18 at 12:44










                                                                                          • $begingroup$
                                                                                            I think it's also valid for your answers of previous challenges given that they seem to have the same structure
                                                                                            $endgroup$
                                                                                            – Olivier Grégoire
                                                                                            Jan 18 at 12:51










                                                                                          • $begingroup$
                                                                                            @OlivierGrégoire Yeah, I was about to golf those as well with your suggestion, but then another comment (and a question at work) came in between. Will do so in a moment.
                                                                                            $endgroup$
                                                                                            – Kevin Cruijssen
                                                                                            Jan 18 at 13:00














                                                                                          1












                                                                                          1








                                                                                          1





                                                                                          $begingroup$

                                                                                          Java 8, 169 145 bytes





                                                                                          m->{for(int i=m.length,j,k;i-->0;)for(j=m[i].length;j-->0;)for(k=9;m[i][j]==1&k-->0;)try{m[i][j]=m[i+k/3-1][j+k%3-1]<1?2:1;}catch(Exception e){}}


                                                                                          -24 bytes thanks to @OlivierGrégoire.



                                                                                          Uses 0 instead of A and 1 instead of B, with the input being a 2D integer-matrix. Modifies the input-matrix instead of returning a new one to save bytes.



                                                                                          The cells are checked the same as in my answer for the All the single eights challenge.



                                                                                          Try it online.



                                                                                          Explanation:



                                                                                          m->{                            // Method with integer-matrix parameter and no return-type
                                                                                          for(int i=m.length,j,k;i-->0;)// Loop over the rows
                                                                                          for(j=m[i].length;j-->0;) // Inner loop over the columns
                                                                                          for(k=9;m[i][j]==1& // If the current cell contains a 1:
                                                                                          k-->0;) // Inner loop `k` in the range (9, 0]:
                                                                                          try{m[i][j]= // Set the current cell to:
                                                                                          m[i+k/3-1] // If `k` is 0, 1, or 2: Look at the previous row
                                                                                          // Else-if `k` is 6, 7, or 8: Look at the next row
                                                                                          // Else (`k` is 3, 4, or 5): Look at the current row
                                                                                          [j+k%3-1] // If `k` is 0, 3, or 6: Look at the previous column
                                                                                          // Else-if `k` is 2, 5, or 8: Look at the next column
                                                                                          // Else (`k` is 1, 4, or 7): Look at the current column
                                                                                          <1? // And if this cell contains a 0:
                                                                                          2 // Change the current cell from a 1 to a 2
                                                                                          : // Else:
                                                                                          1; // Leave it a 1
                                                                                          }catch(Exception e){}} // Catch and ignore ArrayIndexOutOfBoundsExceptions
                                                                                          // (try-catch saves bytes in comparison to if-checks)





                                                                                          share|improve this answer











                                                                                          $endgroup$



                                                                                          Java 8, 169 145 bytes





                                                                                          m->{for(int i=m.length,j,k;i-->0;)for(j=m[i].length;j-->0;)for(k=9;m[i][j]==1&k-->0;)try{m[i][j]=m[i+k/3-1][j+k%3-1]<1?2:1;}catch(Exception e){}}


                                                                                          -24 bytes thanks to @OlivierGrégoire.



                                                                                          Uses 0 instead of A and 1 instead of B, with the input being a 2D integer-matrix. Modifies the input-matrix instead of returning a new one to save bytes.



                                                                                          The cells are checked the same as in my answer for the All the single eights challenge.



                                                                                          Try it online.



                                                                                          Explanation:



                                                                                          m->{                            // Method with integer-matrix parameter and no return-type
                                                                                          for(int i=m.length,j,k;i-->0;)// Loop over the rows
                                                                                          for(j=m[i].length;j-->0;) // Inner loop over the columns
                                                                                          for(k=9;m[i][j]==1& // If the current cell contains a 1:
                                                                                          k-->0;) // Inner loop `k` in the range (9, 0]:
                                                                                          try{m[i][j]= // Set the current cell to:
                                                                                          m[i+k/3-1] // If `k` is 0, 1, or 2: Look at the previous row
                                                                                          // Else-if `k` is 6, 7, or 8: Look at the next row
                                                                                          // Else (`k` is 3, 4, or 5): Look at the current row
                                                                                          [j+k%3-1] // If `k` is 0, 3, or 6: Look at the previous column
                                                                                          // Else-if `k` is 2, 5, or 8: Look at the next column
                                                                                          // Else (`k` is 1, 4, or 7): Look at the current column
                                                                                          <1? // And if this cell contains a 0:
                                                                                          2 // Change the current cell from a 1 to a 2
                                                                                          : // Else:
                                                                                          1; // Leave it a 1
                                                                                          }catch(Exception e){}} // Catch and ignore ArrayIndexOutOfBoundsExceptions
                                                                                          // (try-catch saves bytes in comparison to if-checks)






                                                                                          share|improve this answer














                                                                                          share|improve this answer



                                                                                          share|improve this answer








                                                                                          edited Jan 18 at 12:44

























                                                                                          answered Jan 17 at 10:13









                                                                                          Kevin CruijssenKevin Cruijssen

                                                                                          36.8k555192




                                                                                          36.8k555192








                                                                                          • 1




                                                                                            $begingroup$
                                                                                            I haven't checked much, but is there anything wrong with m[i+k/3-1][j+k%3-1]? 145 bytes
                                                                                            $endgroup$
                                                                                            – Olivier Grégoire
                                                                                            Jan 18 at 12:31










                                                                                          • $begingroup$
                                                                                            @OlivierGrégoire Dang, that's so much easier.. Thanks!
                                                                                            $endgroup$
                                                                                            – Kevin Cruijssen
                                                                                            Jan 18 at 12:44










                                                                                          • $begingroup$
                                                                                            I think it's also valid for your answers of previous challenges given that they seem to have the same structure
                                                                                            $endgroup$
                                                                                            – Olivier Grégoire
                                                                                            Jan 18 at 12:51










                                                                                          • $begingroup$
                                                                                            @OlivierGrégoire Yeah, I was about to golf those as well with your suggestion, but then another comment (and a question at work) came in between. Will do so in a moment.
                                                                                            $endgroup$
                                                                                            – Kevin Cruijssen
                                                                                            Jan 18 at 13:00














                                                                                          • 1




                                                                                            $begingroup$
                                                                                            I haven't checked much, but is there anything wrong with m[i+k/3-1][j+k%3-1]? 145 bytes
                                                                                            $endgroup$
                                                                                            – Olivier Grégoire
                                                                                            Jan 18 at 12:31










                                                                                          • $begingroup$
                                                                                            @OlivierGrégoire Dang, that's so much easier.. Thanks!
                                                                                            $endgroup$
                                                                                            – Kevin Cruijssen
                                                                                            Jan 18 at 12:44










                                                                                          • $begingroup$
                                                                                            I think it's also valid for your answers of previous challenges given that they seem to have the same structure
                                                                                            $endgroup$
                                                                                            – Olivier Grégoire
                                                                                            Jan 18 at 12:51










                                                                                          • $begingroup$
                                                                                            @OlivierGrégoire Yeah, I was about to golf those as well with your suggestion, but then another comment (and a question at work) came in between. Will do so in a moment.
                                                                                            $endgroup$
                                                                                            – Kevin Cruijssen
                                                                                            Jan 18 at 13:00








                                                                                          1




                                                                                          1




                                                                                          $begingroup$
                                                                                          I haven't checked much, but is there anything wrong with m[i+k/3-1][j+k%3-1]? 145 bytes
                                                                                          $endgroup$
                                                                                          – Olivier Grégoire
                                                                                          Jan 18 at 12:31




                                                                                          $begingroup$
                                                                                          I haven't checked much, but is there anything wrong with m[i+k/3-1][j+k%3-1]? 145 bytes
                                                                                          $endgroup$
                                                                                          – Olivier Grégoire
                                                                                          Jan 18 at 12:31












                                                                                          $begingroup$
                                                                                          @OlivierGrégoire Dang, that's so much easier.. Thanks!
                                                                                          $endgroup$
                                                                                          – Kevin Cruijssen
                                                                                          Jan 18 at 12:44




                                                                                          $begingroup$
                                                                                          @OlivierGrégoire Dang, that's so much easier.. Thanks!
                                                                                          $endgroup$
                                                                                          – Kevin Cruijssen
                                                                                          Jan 18 at 12:44












                                                                                          $begingroup$
                                                                                          I think it's also valid for your answers of previous challenges given that they seem to have the same structure
                                                                                          $endgroup$
                                                                                          – Olivier Grégoire
                                                                                          Jan 18 at 12:51




                                                                                          $begingroup$
                                                                                          I think it's also valid for your answers of previous challenges given that they seem to have the same structure
                                                                                          $endgroup$
                                                                                          – Olivier Grégoire
                                                                                          Jan 18 at 12:51












                                                                                          $begingroup$
                                                                                          @OlivierGrégoire Yeah, I was about to golf those as well with your suggestion, but then another comment (and a question at work) came in between. Will do so in a moment.
                                                                                          $endgroup$
                                                                                          – Kevin Cruijssen
                                                                                          Jan 18 at 13:00




                                                                                          $begingroup$
                                                                                          @OlivierGrégoire Yeah, I was about to golf those as well with your suggestion, but then another comment (and a question at work) came in between. Will do so in a moment.
                                                                                          $endgroup$
                                                                                          – Kevin Cruijssen
                                                                                          Jan 18 at 13:00











                                                                                          1












                                                                                          $begingroup$


                                                                                          PowerShell, 86 80 bytes





                                                                                          $p="(.?.?.{$(($args|% i*f '
                                                                                          ')-1)})?"
                                                                                          $args-replace"(?s)(?<=A$p)B|B(?=$p`A)",'+'


                                                                                          Try it online!



                                                                                          The map is a string with newlines. This script replaces B to + with regexp (?<=A(.?.?.{$MapWidth-1})?)B|B(?=(.?.?.{$MapWidth-1})?A).



                                                                                          Less golfed Test script:



                                                                                          $f = {
                                                                                          $l=($args|% indexOf "`n")-1
                                                                                          $p="(.?.?.{$l})?"
                                                                                          $args-replace"(?s)(?<=A$p)B|B(?=$p`A)",'+'
                                                                                          }

                                                                                          @(
                                                                                          ,(@"
                                                                                          AAAAAAAAAA
                                                                                          ABAAAAAABA
                                                                                          ABBBAABABA
                                                                                          ABBBAABABA
                                                                                          ABBBBABABA
                                                                                          ABBBBABBBB
                                                                                          ABBBBABBBB
                                                                                          ABBBBBBBBB
                                                                                          BBBBBBBBBB
                                                                                          "@,@"
                                                                                          AAAAAAAAAA
                                                                                          A+AAAAAA+A
                                                                                          A+++AA+A+A
                                                                                          A+B+AA+A+A
                                                                                          A+B++A+A+A
                                                                                          A+BB+A++++
                                                                                          A+BB+A+BBB
                                                                                          A+BB+++BBB
                                                                                          ++BBBBBBBB
                                                                                          "@)
                                                                                          ,(@"
                                                                                          AAA
                                                                                          AAA
                                                                                          BBB
                                                                                          "@,@"
                                                                                          AAA
                                                                                          AAA
                                                                                          +++
                                                                                          "@)
                                                                                          ,(@"
                                                                                          AAAAAAAAAA
                                                                                          AAAABBBAAA
                                                                                          AAAABBBAAA
                                                                                          AAAABBBAAA
                                                                                          AAAAAAAAAA
                                                                                          BBBBBBABBB
                                                                                          BBBBBBAABB
                                                                                          BBBAAAAABB
                                                                                          BBBBBBBBBB
                                                                                          "@,@"
                                                                                          AAAAAAAAAA
                                                                                          AAAA+++AAA
                                                                                          AAAA+B+AAA
                                                                                          AAAA+++AAA
                                                                                          AAAAAAAAAA
                                                                                          ++++++A+++
                                                                                          BB++++AA+B
                                                                                          BB+AAAAA+B
                                                                                          BB+++++++B
                                                                                          "@)
                                                                                          ) | % {
                                                                                          $map,$expected = $_
                                                                                          $result = &$f $map
                                                                                          $result-eq$expected
                                                                                          #$result # uncomment this line to display results
                                                                                          }


                                                                                          Output:



                                                                                          True
                                                                                          True
                                                                                          True





                                                                                          share|improve this answer











                                                                                          $endgroup$


















                                                                                            1












                                                                                            $begingroup$


                                                                                            PowerShell, 86 80 bytes





                                                                                            $p="(.?.?.{$(($args|% i*f '
                                                                                            ')-1)})?"
                                                                                            $args-replace"(?s)(?<=A$p)B|B(?=$p`A)",'+'


                                                                                            Try it online!



                                                                                            The map is a string with newlines. This script replaces B to + with regexp (?<=A(.?.?.{$MapWidth-1})?)B|B(?=(.?.?.{$MapWidth-1})?A).



                                                                                            Less golfed Test script:



                                                                                            $f = {
                                                                                            $l=($args|% indexOf "`n")-1
                                                                                            $p="(.?.?.{$l})?"
                                                                                            $args-replace"(?s)(?<=A$p)B|B(?=$p`A)",'+'
                                                                                            }

                                                                                            @(
                                                                                            ,(@"
                                                                                            AAAAAAAAAA
                                                                                            ABAAAAAABA
                                                                                            ABBBAABABA
                                                                                            ABBBAABABA
                                                                                            ABBBBABABA
                                                                                            ABBBBABBBB
                                                                                            ABBBBABBBB
                                                                                            ABBBBBBBBB
                                                                                            BBBBBBBBBB
                                                                                            "@,@"
                                                                                            AAAAAAAAAA
                                                                                            A+AAAAAA+A
                                                                                            A+++AA+A+A
                                                                                            A+B+AA+A+A
                                                                                            A+B++A+A+A
                                                                                            A+BB+A++++
                                                                                            A+BB+A+BBB
                                                                                            A+BB+++BBB
                                                                                            ++BBBBBBBB
                                                                                            "@)
                                                                                            ,(@"
                                                                                            AAA
                                                                                            AAA
                                                                                            BBB
                                                                                            "@,@"
                                                                                            AAA
                                                                                            AAA
                                                                                            +++
                                                                                            "@)
                                                                                            ,(@"
                                                                                            AAAAAAAAAA
                                                                                            AAAABBBAAA
                                                                                            AAAABBBAAA
                                                                                            AAAABBBAAA
                                                                                            AAAAAAAAAA
                                                                                            BBBBBBABBB
                                                                                            BBBBBBAABB
                                                                                            BBBAAAAABB
                                                                                            BBBBBBBBBB
                                                                                            "@,@"
                                                                                            AAAAAAAAAA
                                                                                            AAAA+++AAA
                                                                                            AAAA+B+AAA
                                                                                            AAAA+++AAA
                                                                                            AAAAAAAAAA
                                                                                            ++++++A+++
                                                                                            BB++++AA+B
                                                                                            BB+AAAAA+B
                                                                                            BB+++++++B
                                                                                            "@)
                                                                                            ) | % {
                                                                                            $map,$expected = $_
                                                                                            $result = &$f $map
                                                                                            $result-eq$expected
                                                                                            #$result # uncomment this line to display results
                                                                                            }


                                                                                            Output:



                                                                                            True
                                                                                            True
                                                                                            True





                                                                                            share|improve this answer











                                                                                            $endgroup$
















                                                                                              1












                                                                                              1








                                                                                              1





                                                                                              $begingroup$


                                                                                              PowerShell, 86 80 bytes





                                                                                              $p="(.?.?.{$(($args|% i*f '
                                                                                              ')-1)})?"
                                                                                              $args-replace"(?s)(?<=A$p)B|B(?=$p`A)",'+'


                                                                                              Try it online!



                                                                                              The map is a string with newlines. This script replaces B to + with regexp (?<=A(.?.?.{$MapWidth-1})?)B|B(?=(.?.?.{$MapWidth-1})?A).



                                                                                              Less golfed Test script:



                                                                                              $f = {
                                                                                              $l=($args|% indexOf "`n")-1
                                                                                              $p="(.?.?.{$l})?"
                                                                                              $args-replace"(?s)(?<=A$p)B|B(?=$p`A)",'+'
                                                                                              }

                                                                                              @(
                                                                                              ,(@"
                                                                                              AAAAAAAAAA
                                                                                              ABAAAAAABA
                                                                                              ABBBAABABA
                                                                                              ABBBAABABA
                                                                                              ABBBBABABA
                                                                                              ABBBBABBBB
                                                                                              ABBBBABBBB
                                                                                              ABBBBBBBBB
                                                                                              BBBBBBBBBB
                                                                                              "@,@"
                                                                                              AAAAAAAAAA
                                                                                              A+AAAAAA+A
                                                                                              A+++AA+A+A
                                                                                              A+B+AA+A+A
                                                                                              A+B++A+A+A
                                                                                              A+BB+A++++
                                                                                              A+BB+A+BBB
                                                                                              A+BB+++BBB
                                                                                              ++BBBBBBBB
                                                                                              "@)
                                                                                              ,(@"
                                                                                              AAA
                                                                                              AAA
                                                                                              BBB
                                                                                              "@,@"
                                                                                              AAA
                                                                                              AAA
                                                                                              +++
                                                                                              "@)
                                                                                              ,(@"
                                                                                              AAAAAAAAAA
                                                                                              AAAABBBAAA
                                                                                              AAAABBBAAA
                                                                                              AAAABBBAAA
                                                                                              AAAAAAAAAA
                                                                                              BBBBBBABBB
                                                                                              BBBBBBAABB
                                                                                              BBBAAAAABB
                                                                                              BBBBBBBBBB
                                                                                              "@,@"
                                                                                              AAAAAAAAAA
                                                                                              AAAA+++AAA
                                                                                              AAAA+B+AAA
                                                                                              AAAA+++AAA
                                                                                              AAAAAAAAAA
                                                                                              ++++++A+++
                                                                                              BB++++AA+B
                                                                                              BB+AAAAA+B
                                                                                              BB+++++++B
                                                                                              "@)
                                                                                              ) | % {
                                                                                              $map,$expected = $_
                                                                                              $result = &$f $map
                                                                                              $result-eq$expected
                                                                                              #$result # uncomment this line to display results
                                                                                              }


                                                                                              Output:



                                                                                              True
                                                                                              True
                                                                                              True





                                                                                              share|improve this answer











                                                                                              $endgroup$




                                                                                              PowerShell, 86 80 bytes





                                                                                              $p="(.?.?.{$(($args|% i*f '
                                                                                              ')-1)})?"
                                                                                              $args-replace"(?s)(?<=A$p)B|B(?=$p`A)",'+'


                                                                                              Try it online!



                                                                                              The map is a string with newlines. This script replaces B to + with regexp (?<=A(.?.?.{$MapWidth-1})?)B|B(?=(.?.?.{$MapWidth-1})?A).



                                                                                              Less golfed Test script:



                                                                                              $f = {
                                                                                              $l=($args|% indexOf "`n")-1
                                                                                              $p="(.?.?.{$l})?"
                                                                                              $args-replace"(?s)(?<=A$p)B|B(?=$p`A)",'+'
                                                                                              }

                                                                                              @(
                                                                                              ,(@"
                                                                                              AAAAAAAAAA
                                                                                              ABAAAAAABA
                                                                                              ABBBAABABA
                                                                                              ABBBAABABA
                                                                                              ABBBBABABA
                                                                                              ABBBBABBBB
                                                                                              ABBBBABBBB
                                                                                              ABBBBBBBBB
                                                                                              BBBBBBBBBB
                                                                                              "@,@"
                                                                                              AAAAAAAAAA
                                                                                              A+AAAAAA+A
                                                                                              A+++AA+A+A
                                                                                              A+B+AA+A+A
                                                                                              A+B++A+A+A
                                                                                              A+BB+A++++
                                                                                              A+BB+A+BBB
                                                                                              A+BB+++BBB
                                                                                              ++BBBBBBBB
                                                                                              "@)
                                                                                              ,(@"
                                                                                              AAA
                                                                                              AAA
                                                                                              BBB
                                                                                              "@,@"
                                                                                              AAA
                                                                                              AAA
                                                                                              +++
                                                                                              "@)
                                                                                              ,(@"
                                                                                              AAAAAAAAAA
                                                                                              AAAABBBAAA
                                                                                              AAAABBBAAA
                                                                                              AAAABBBAAA
                                                                                              AAAAAAAAAA
                                                                                              BBBBBBABBB
                                                                                              BBBBBBAABB
                                                                                              BBBAAAAABB
                                                                                              BBBBBBBBBB
                                                                                              "@,@"
                                                                                              AAAAAAAAAA
                                                                                              AAAA+++AAA
                                                                                              AAAA+B+AAA
                                                                                              AAAA+++AAA
                                                                                              AAAAAAAAAA
                                                                                              ++++++A+++
                                                                                              BB++++AA+B
                                                                                              BB+AAAAA+B
                                                                                              BB+++++++B
                                                                                              "@)
                                                                                              ) | % {
                                                                                              $map,$expected = $_
                                                                                              $result = &$f $map
                                                                                              $result-eq$expected
                                                                                              #$result # uncomment this line to display results
                                                                                              }


                                                                                              Output:



                                                                                              True
                                                                                              True
                                                                                              True






                                                                                              share|improve this answer














                                                                                              share|improve this answer



                                                                                              share|improve this answer








                                                                                              edited Jan 20 at 23:10

























                                                                                              answered Jan 20 at 22:26









                                                                                              mazzymazzy

                                                                                              2,3151315




                                                                                              2,3151315























                                                                                                  0












                                                                                                  $begingroup$


                                                                                                  Ruby, 102 bytes





                                                                                                  ->a{a+=?.*s=a.size
                                                                                                  (s*9).times{|i|a[j=i/9]>?A&&a[j-1+i%3+~a.index($/)*(i/3%3-1)]==?A&&a[j]=?+}
                                                                                                  a[0,s]}


                                                                                                  Try it online!



                                                                                                  input/output as a newline separated string






                                                                                                  share|improve this answer









                                                                                                  $endgroup$


















                                                                                                    0












                                                                                                    $begingroup$


                                                                                                    Ruby, 102 bytes





                                                                                                    ->a{a+=?.*s=a.size
                                                                                                    (s*9).times{|i|a[j=i/9]>?A&&a[j-1+i%3+~a.index($/)*(i/3%3-1)]==?A&&a[j]=?+}
                                                                                                    a[0,s]}


                                                                                                    Try it online!



                                                                                                    input/output as a newline separated string






                                                                                                    share|improve this answer









                                                                                                    $endgroup$
















                                                                                                      0












                                                                                                      0








                                                                                                      0





                                                                                                      $begingroup$


                                                                                                      Ruby, 102 bytes





                                                                                                      ->a{a+=?.*s=a.size
                                                                                                      (s*9).times{|i|a[j=i/9]>?A&&a[j-1+i%3+~a.index($/)*(i/3%3-1)]==?A&&a[j]=?+}
                                                                                                      a[0,s]}


                                                                                                      Try it online!



                                                                                                      input/output as a newline separated string






                                                                                                      share|improve this answer









                                                                                                      $endgroup$




                                                                                                      Ruby, 102 bytes





                                                                                                      ->a{a+=?.*s=a.size
                                                                                                      (s*9).times{|i|a[j=i/9]>?A&&a[j-1+i%3+~a.index($/)*(i/3%3-1)]==?A&&a[j]=?+}
                                                                                                      a[0,s]}


                                                                                                      Try it online!



                                                                                                      input/output as a newline separated string







                                                                                                      share|improve this answer












                                                                                                      share|improve this answer



                                                                                                      share|improve this answer










                                                                                                      answered Jan 17 at 0:11









                                                                                                      Level River StLevel River St

                                                                                                      20.3k32579




                                                                                                      20.3k32579























                                                                                                          0












                                                                                                          $begingroup$


                                                                                                          Python 2, 123 119 bytes





                                                                                                          lambda m:[[[c,'+'][c=='B'and'A'in`[x[j-(j>0):j+2]for x in m[i-(i>0):i+2]]`]for j,c in e(l)]for i,l in e(m)];e=enumerate


                                                                                                          Try it online!



                                                                                                          I/O is a list of lists






                                                                                                          share|improve this answer











                                                                                                          $endgroup$


















                                                                                                            0












                                                                                                            $begingroup$


                                                                                                            Python 2, 123 119 bytes





                                                                                                            lambda m:[[[c,'+'][c=='B'and'A'in`[x[j-(j>0):j+2]for x in m[i-(i>0):i+2]]`]for j,c in e(l)]for i,l in e(m)];e=enumerate


                                                                                                            Try it online!



                                                                                                            I/O is a list of lists






                                                                                                            share|improve this answer











                                                                                                            $endgroup$
















                                                                                                              0












                                                                                                              0








                                                                                                              0





                                                                                                              $begingroup$


                                                                                                              Python 2, 123 119 bytes





                                                                                                              lambda m:[[[c,'+'][c=='B'and'A'in`[x[j-(j>0):j+2]for x in m[i-(i>0):i+2]]`]for j,c in e(l)]for i,l in e(m)];e=enumerate


                                                                                                              Try it online!



                                                                                                              I/O is a list of lists






                                                                                                              share|improve this answer











                                                                                                              $endgroup$




                                                                                                              Python 2, 123 119 bytes





                                                                                                              lambda m:[[[c,'+'][c=='B'and'A'in`[x[j-(j>0):j+2]for x in m[i-(i>0):i+2]]`]for j,c in e(l)]for i,l in e(m)];e=enumerate


                                                                                                              Try it online!



                                                                                                              I/O is a list of lists







                                                                                                              share|improve this answer














                                                                                                              share|improve this answer



                                                                                                              share|improve this answer








                                                                                                              edited Jan 17 at 11:49

























                                                                                                              answered Jan 17 at 8:21









                                                                                                              TFeldTFeld

                                                                                                              14.7k21241




                                                                                                              14.7k21241























                                                                                                                  0












                                                                                                                  $begingroup$


                                                                                                                  05AB1E, 29 bytes



                                                                                                                  _2FIн¸.øVgN+FYN._3£})εøO}ø}*Ā+


                                                                                                                  Matrices aren't really 05AB1E's strong suit (nor are they my strong suit).. Can definitely be golfed some more, though.

                                                                                                                  Inspired by @ngn's K (ngn/k) answer, so also uses I/O of a 2D integer matrix with 012 for AB+ respectively.



                                                                                                                  Try it online. (The footer in the TIO is to pretty-print the output. Feel free to remove it to see the matrix output.)



                                                                                                                  Explanation:





                                                                                                                  _                # Inverse the values of the (implicit) input-matrix (0→1 and 1→0)
                                                                                                                  # i.e. [[0,0,0,0],[0,1,0,0],[0,1,1,1],[1,1,1,1]]
                                                                                                                  # → [[1,1,1,1],[1,0,1,1],[1,0,0,0],[0,0,0,0]]
                                                                                                                  2F # Loop `n` 2 times in the range [0, 2):
                                                                                                                  Iн # Take the input-matrix, and only leave the first inner list of 0s
                                                                                                                  # i.e. [[0,0,0,0],[0,1,0,0],[0,1,1,1],[1,1,1,1]] → [0,0,0,0]
                                                                                                                  ¸ # Wrap it into a list
                                                                                                                  # i.e. [0,0,0,0] → [[0,0,0,0]]
                                                                                                                  .ø # Surround the inverted input with the list of 0s
                                                                                                                  # i.e. [[1,1,1,1],[1,0,1,1],[1,0,0,0],[0,0,0,0]] and [0,0,0,0]
                                                                                                                  # → [[0,0,0,0],[1,1,1,1],[1,0,1,1],[1,0,0,0],[0,0,0,0],[0,0,0,0]]
                                                                                                                  V # Pop and store it in variable `Y`
                                                                                                                  g # Take the length of the (implicit) input-matrix
                                                                                                                  # i.e. [[0,0,0,0],[0,1,0,0],[0,1,1,1],[1,1,1,1]] → 4
                                                                                                                  N+ # Add `n` to it
                                                                                                                  # i.e. 4 and n=0 → 4
                                                                                                                  # i.e. 4 and n=1 → 5
                                                                                                                  F # Inner loop `N` in the range [0, length+`n`):
                                                                                                                  Y # Push matrix `Y`
                                                                                                                  N._ # Rotate it `N` times towards the left
                                                                                                                  # i.e. [[0,0,0,0],[1,1,1,1],[1,0,1,1],[1,0,0,0],[0,0,0,0],[0,0,0,0]] and N=2
                                                                                                                  # → [[1,0,1,1],[1,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[1,1,1,1]]
                                                                                                                  3£ # And only leave the first three inner lists
                                                                                                                  # i.e. [[1,0,1,1],[1,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[1,1,1,1]]
                                                                                                                  # → [[1,0,1,1],[1,0,0,0],[0,0,0,0]]
                                                                                                                  } # After the inner loop:
                                                                                                                  ) # Wrap everything on the stack into a list
                                                                                                                  # → [[[0,0,0,0],[1,1,1,1],[1,0,1,1]],[[1,1,1,1],[1,0,1,1],[1,0,0,0]],[[1,0,1,1],[1,0,0,0],[0,0,0,0]],[[1,0,0,0],[0,0,0,0],[0,0,0,0]]]
                                                                                                                  €ø # Zip/transpose (swapping rows/columns) each matrix in the list
                                                                                                                  # → [[[0,1,1],[0,1,0],[0,1,1],[0,1,1]],[[1,1,1],[1,0,0],[1,1,0],[1,1,0]],[[1,1,0],[0,0,0],[1,0,0],[1,0,0]],[[1,0,0],[0,0,0],[0,0,0],[0,0,0]]]
                                                                                                                  O # And take the sum of each inner list
                                                                                                                  # → [[2,1,2,2],[3,1,2,2],[2,0,1,1],[1,0,0,0]]
                                                                                                                  ø # Zip/transpose; swapping rows/columns the entire matrix again
                                                                                                                  # i.e. [[2,1,2,2],[3,1,2,2],[2,0,1,1],[1,0,0,0]]
                                                                                                                  # → [[2,3,2,1],[1,1,0,0],[2,2,1,0],[2,2,1,0]]
                                                                                                                  } # After the outer loop:
                                                                                                                  # i.e. [[3,5,5,4,2],[4,6,5,4,2],[2,3,2,2,1],[1,1,0,0,0]]
                                                                                                                  * # Multiple each value with the input-matrix at the same positions,
                                                                                                                  # which implicitly removes the trailing values
                                                                                                                  # i.e. [[3,5,5,4,2],[4,6,5,4,2],[2,3,2,2,1],[1,1,0,0,0]]
                                                                                                                  # and [[0,0,0,0],[0,1,0,0],[0,1,1,1],[1,1,1,1]]
                                                                                                                  # → [[0,0,0,0],[0,1,0,0],[0,2,1,0],[2,2,1,0]]
                                                                                                                  Ā # Truthify each value (0 remains 0; everything else becomes 1)
                                                                                                                  # i.e. [[0,0,0,0],[0,1,0,0],[0,2,1,0],[2,2,1,0]]
                                                                                                                  # → [[0,0,0,0],[0,1,0,0],[0,1,1,1],[1,1,0,0]]
                                                                                                                  + # Then add each value with the input-matrix at the same positions
                                                                                                                  # i.e. [[0,0,0,0],[0,1,0,0],[0,1,1,1],[1,1,0,0]]
                                                                                                                  # and [[0,0,0,0],[0,1,0,0],[0,1,1,1],[1,1,1,1]]
                                                                                                                  # → [[0,0,0,0],[0,2,0,0],[0,2,2,2],[2,2,1,1]]
                                                                                                                  # (and output the result implicitly)





                                                                                                                  share|improve this answer









                                                                                                                  $endgroup$


















                                                                                                                    0












                                                                                                                    $begingroup$


                                                                                                                    05AB1E, 29 bytes



                                                                                                                    _2FIн¸.øVgN+FYN._3£})εøO}ø}*Ā+


                                                                                                                    Matrices aren't really 05AB1E's strong suit (nor are they my strong suit).. Can definitely be golfed some more, though.

                                                                                                                    Inspired by @ngn's K (ngn/k) answer, so also uses I/O of a 2D integer matrix with 012 for AB+ respectively.



                                                                                                                    Try it online. (The footer in the TIO is to pretty-print the output. Feel free to remove it to see the matrix output.)



                                                                                                                    Explanation:





                                                                                                                    _                # Inverse the values of the (implicit) input-matrix (0→1 and 1→0)
                                                                                                                    # i.e. [[0,0,0,0],[0,1,0,0],[0,1,1,1],[1,1,1,1]]
                                                                                                                    # → [[1,1,1,1],[1,0,1,1],[1,0,0,0],[0,0,0,0]]
                                                                                                                    2F # Loop `n` 2 times in the range [0, 2):
                                                                                                                    Iн # Take the input-matrix, and only leave the first inner list of 0s
                                                                                                                    # i.e. [[0,0,0,0],[0,1,0,0],[0,1,1,1],[1,1,1,1]] → [0,0,0,0]
                                                                                                                    ¸ # Wrap it into a list
                                                                                                                    # i.e. [0,0,0,0] → [[0,0,0,0]]
                                                                                                                    .ø # Surround the inverted input with the list of 0s
                                                                                                                    # i.e. [[1,1,1,1],[1,0,1,1],[1,0,0,0],[0,0,0,0]] and [0,0,0,0]
                                                                                                                    # → [[0,0,0,0],[1,1,1,1],[1,0,1,1],[1,0,0,0],[0,0,0,0],[0,0,0,0]]
                                                                                                                    V # Pop and store it in variable `Y`
                                                                                                                    g # Take the length of the (implicit) input-matrix
                                                                                                                    # i.e. [[0,0,0,0],[0,1,0,0],[0,1,1,1],[1,1,1,1]] → 4
                                                                                                                    N+ # Add `n` to it
                                                                                                                    # i.e. 4 and n=0 → 4
                                                                                                                    # i.e. 4 and n=1 → 5
                                                                                                                    F # Inner loop `N` in the range [0, length+`n`):
                                                                                                                    Y # Push matrix `Y`
                                                                                                                    N._ # Rotate it `N` times towards the left
                                                                                                                    # i.e. [[0,0,0,0],[1,1,1,1],[1,0,1,1],[1,0,0,0],[0,0,0,0],[0,0,0,0]] and N=2
                                                                                                                    # → [[1,0,1,1],[1,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[1,1,1,1]]
                                                                                                                    3£ # And only leave the first three inner lists
                                                                                                                    # i.e. [[1,0,1,1],[1,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[1,1,1,1]]
                                                                                                                    # → [[1,0,1,1],[1,0,0,0],[0,0,0,0]]
                                                                                                                    } # After the inner loop:
                                                                                                                    ) # Wrap everything on the stack into a list
                                                                                                                    # → [[[0,0,0,0],[1,1,1,1],[1,0,1,1]],[[1,1,1,1],[1,0,1,1],[1,0,0,0]],[[1,0,1,1],[1,0,0,0],[0,0,0,0]],[[1,0,0,0],[0,0,0,0],[0,0,0,0]]]
                                                                                                                    €ø # Zip/transpose (swapping rows/columns) each matrix in the list
                                                                                                                    # → [[[0,1,1],[0,1,0],[0,1,1],[0,1,1]],[[1,1,1],[1,0,0],[1,1,0],[1,1,0]],[[1,1,0],[0,0,0],[1,0,0],[1,0,0]],[[1,0,0],[0,0,0],[0,0,0],[0,0,0]]]
                                                                                                                    O # And take the sum of each inner list
                                                                                                                    # → [[2,1,2,2],[3,1,2,2],[2,0,1,1],[1,0,0,0]]
                                                                                                                    ø # Zip/transpose; swapping rows/columns the entire matrix again
                                                                                                                    # i.e. [[2,1,2,2],[3,1,2,2],[2,0,1,1],[1,0,0,0]]
                                                                                                                    # → [[2,3,2,1],[1,1,0,0],[2,2,1,0],[2,2,1,0]]
                                                                                                                    } # After the outer loop:
                                                                                                                    # i.e. [[3,5,5,4,2],[4,6,5,4,2],[2,3,2,2,1],[1,1,0,0,0]]
                                                                                                                    * # Multiple each value with the input-matrix at the same positions,
                                                                                                                    # which implicitly removes the trailing values
                                                                                                                    # i.e. [[3,5,5,4,2],[4,6,5,4,2],[2,3,2,2,1],[1,1,0,0,0]]
                                                                                                                    # and [[0,0,0,0],[0,1,0,0],[0,1,1,1],[1,1,1,1]]
                                                                                                                    # → [[0,0,0,0],[0,1,0,0],[0,2,1,0],[2,2,1,0]]
                                                                                                                    Ā # Truthify each value (0 remains 0; everything else becomes 1)
                                                                                                                    # i.e. [[0,0,0,0],[0,1,0,0],[0,2,1,0],[2,2,1,0]]
                                                                                                                    # → [[0,0,0,0],[0,1,0,0],[0,1,1,1],[1,1,0,0]]
                                                                                                                    + # Then add each value with the input-matrix at the same positions
                                                                                                                    # i.e. [[0,0,0,0],[0,1,0,0],[0,1,1,1],[1,1,0,0]]
                                                                                                                    # and [[0,0,0,0],[0,1,0,0],[0,1,1,1],[1,1,1,1]]
                                                                                                                    # → [[0,0,0,0],[0,2,0,0],[0,2,2,2],[2,2,1,1]]
                                                                                                                    # (and output the result implicitly)





                                                                                                                    share|improve this answer









                                                                                                                    $endgroup$
















                                                                                                                      0












                                                                                                                      0








                                                                                                                      0





                                                                                                                      $begingroup$


                                                                                                                      05AB1E, 29 bytes



                                                                                                                      _2FIн¸.øVgN+FYN._3£})εøO}ø}*Ā+


                                                                                                                      Matrices aren't really 05AB1E's strong suit (nor are they my strong suit).. Can definitely be golfed some more, though.

                                                                                                                      Inspired by @ngn's K (ngn/k) answer, so also uses I/O of a 2D integer matrix with 012 for AB+ respectively.



                                                                                                                      Try it online. (The footer in the TIO is to pretty-print the output. Feel free to remove it to see the matrix output.)



                                                                                                                      Explanation:





                                                                                                                      _                # Inverse the values of the (implicit) input-matrix (0→1 and 1→0)
                                                                                                                      # i.e. [[0,0,0,0],[0,1,0,0],[0,1,1,1],[1,1,1,1]]
                                                                                                                      # → [[1,1,1,1],[1,0,1,1],[1,0,0,0],[0,0,0,0]]
                                                                                                                      2F # Loop `n` 2 times in the range [0, 2):
                                                                                                                      Iн # Take the input-matrix, and only leave the first inner list of 0s
                                                                                                                      # i.e. [[0,0,0,0],[0,1,0,0],[0,1,1,1],[1,1,1,1]] → [0,0,0,0]
                                                                                                                      ¸ # Wrap it into a list
                                                                                                                      # i.e. [0,0,0,0] → [[0,0,0,0]]
                                                                                                                      .ø # Surround the inverted input with the list of 0s
                                                                                                                      # i.e. [[1,1,1,1],[1,0,1,1],[1,0,0,0],[0,0,0,0]] and [0,0,0,0]
                                                                                                                      # → [[0,0,0,0],[1,1,1,1],[1,0,1,1],[1,0,0,0],[0,0,0,0],[0,0,0,0]]
                                                                                                                      V # Pop and store it in variable `Y`
                                                                                                                      g # Take the length of the (implicit) input-matrix
                                                                                                                      # i.e. [[0,0,0,0],[0,1,0,0],[0,1,1,1],[1,1,1,1]] → 4
                                                                                                                      N+ # Add `n` to it
                                                                                                                      # i.e. 4 and n=0 → 4
                                                                                                                      # i.e. 4 and n=1 → 5
                                                                                                                      F # Inner loop `N` in the range [0, length+`n`):
                                                                                                                      Y # Push matrix `Y`
                                                                                                                      N._ # Rotate it `N` times towards the left
                                                                                                                      # i.e. [[0,0,0,0],[1,1,1,1],[1,0,1,1],[1,0,0,0],[0,0,0,0],[0,0,0,0]] and N=2
                                                                                                                      # → [[1,0,1,1],[1,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[1,1,1,1]]
                                                                                                                      3£ # And only leave the first three inner lists
                                                                                                                      # i.e. [[1,0,1,1],[1,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[1,1,1,1]]
                                                                                                                      # → [[1,0,1,1],[1,0,0,0],[0,0,0,0]]
                                                                                                                      } # After the inner loop:
                                                                                                                      ) # Wrap everything on the stack into a list
                                                                                                                      # → [[[0,0,0,0],[1,1,1,1],[1,0,1,1]],[[1,1,1,1],[1,0,1,1],[1,0,0,0]],[[1,0,1,1],[1,0,0,0],[0,0,0,0]],[[1,0,0,0],[0,0,0,0],[0,0,0,0]]]
                                                                                                                      €ø # Zip/transpose (swapping rows/columns) each matrix in the list
                                                                                                                      # → [[[0,1,1],[0,1,0],[0,1,1],[0,1,1]],[[1,1,1],[1,0,0],[1,1,0],[1,1,0]],[[1,1,0],[0,0,0],[1,0,0],[1,0,0]],[[1,0,0],[0,0,0],[0,0,0],[0,0,0]]]
                                                                                                                      O # And take the sum of each inner list
                                                                                                                      # → [[2,1,2,2],[3,1,2,2],[2,0,1,1],[1,0,0,0]]
                                                                                                                      ø # Zip/transpose; swapping rows/columns the entire matrix again
                                                                                                                      # i.e. [[2,1,2,2],[3,1,2,2],[2,0,1,1],[1,0,0,0]]
                                                                                                                      # → [[2,3,2,1],[1,1,0,0],[2,2,1,0],[2,2,1,0]]
                                                                                                                      } # After the outer loop:
                                                                                                                      # i.e. [[3,5,5,4,2],[4,6,5,4,2],[2,3,2,2,1],[1,1,0,0,0]]
                                                                                                                      * # Multiple each value with the input-matrix at the same positions,
                                                                                                                      # which implicitly removes the trailing values
                                                                                                                      # i.e. [[3,5,5,4,2],[4,6,5,4,2],[2,3,2,2,1],[1,1,0,0,0]]
                                                                                                                      # and [[0,0,0,0],[0,1,0,0],[0,1,1,1],[1,1,1,1]]
                                                                                                                      # → [[0,0,0,0],[0,1,0,0],[0,2,1,0],[2,2,1,0]]
                                                                                                                      Ā # Truthify each value (0 remains 0; everything else becomes 1)
                                                                                                                      # i.e. [[0,0,0,0],[0,1,0,0],[0,2,1,0],[2,2,1,0]]
                                                                                                                      # → [[0,0,0,0],[0,1,0,0],[0,1,1,1],[1,1,0,0]]
                                                                                                                      + # Then add each value with the input-matrix at the same positions
                                                                                                                      # i.e. [[0,0,0,0],[0,1,0,0],[0,1,1,1],[1,1,0,0]]
                                                                                                                      # and [[0,0,0,0],[0,1,0,0],[0,1,1,1],[1,1,1,1]]
                                                                                                                      # → [[0,0,0,0],[0,2,0,0],[0,2,2,2],[2,2,1,1]]
                                                                                                                      # (and output the result implicitly)





                                                                                                                      share|improve this answer









                                                                                                                      $endgroup$




                                                                                                                      05AB1E, 29 bytes



                                                                                                                      _2FIн¸.øVgN+FYN._3£})εøO}ø}*Ā+


                                                                                                                      Matrices aren't really 05AB1E's strong suit (nor are they my strong suit).. Can definitely be golfed some more, though.

                                                                                                                      Inspired by @ngn's K (ngn/k) answer, so also uses I/O of a 2D integer matrix with 012 for AB+ respectively.



                                                                                                                      Try it online. (The footer in the TIO is to pretty-print the output. Feel free to remove it to see the matrix output.)



                                                                                                                      Explanation:





                                                                                                                      _                # Inverse the values of the (implicit) input-matrix (0→1 and 1→0)
                                                                                                                      # i.e. [[0,0,0,0],[0,1,0,0],[0,1,1,1],[1,1,1,1]]
                                                                                                                      # → [[1,1,1,1],[1,0,1,1],[1,0,0,0],[0,0,0,0]]
                                                                                                                      2F # Loop `n` 2 times in the range [0, 2):
                                                                                                                      Iн # Take the input-matrix, and only leave the first inner list of 0s
                                                                                                                      # i.e. [[0,0,0,0],[0,1,0,0],[0,1,1,1],[1,1,1,1]] → [0,0,0,0]
                                                                                                                      ¸ # Wrap it into a list
                                                                                                                      # i.e. [0,0,0,0] → [[0,0,0,0]]
                                                                                                                      .ø # Surround the inverted input with the list of 0s
                                                                                                                      # i.e. [[1,1,1,1],[1,0,1,1],[1,0,0,0],[0,0,0,0]] and [0,0,0,0]
                                                                                                                      # → [[0,0,0,0],[1,1,1,1],[1,0,1,1],[1,0,0,0],[0,0,0,0],[0,0,0,0]]
                                                                                                                      V # Pop and store it in variable `Y`
                                                                                                                      g # Take the length of the (implicit) input-matrix
                                                                                                                      # i.e. [[0,0,0,0],[0,1,0,0],[0,1,1,1],[1,1,1,1]] → 4
                                                                                                                      N+ # Add `n` to it
                                                                                                                      # i.e. 4 and n=0 → 4
                                                                                                                      # i.e. 4 and n=1 → 5
                                                                                                                      F # Inner loop `N` in the range [0, length+`n`):
                                                                                                                      Y # Push matrix `Y`
                                                                                                                      N._ # Rotate it `N` times towards the left
                                                                                                                      # i.e. [[0,0,0,0],[1,1,1,1],[1,0,1,1],[1,0,0,0],[0,0,0,0],[0,0,0,0]] and N=2
                                                                                                                      # → [[1,0,1,1],[1,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[1,1,1,1]]
                                                                                                                      3£ # And only leave the first three inner lists
                                                                                                                      # i.e. [[1,0,1,1],[1,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[1,1,1,1]]
                                                                                                                      # → [[1,0,1,1],[1,0,0,0],[0,0,0,0]]
                                                                                                                      } # After the inner loop:
                                                                                                                      ) # Wrap everything on the stack into a list
                                                                                                                      # → [[[0,0,0,0],[1,1,1,1],[1,0,1,1]],[[1,1,1,1],[1,0,1,1],[1,0,0,0]],[[1,0,1,1],[1,0,0,0],[0,0,0,0]],[[1,0,0,0],[0,0,0,0],[0,0,0,0]]]
                                                                                                                      €ø # Zip/transpose (swapping rows/columns) each matrix in the list
                                                                                                                      # → [[[0,1,1],[0,1,0],[0,1,1],[0,1,1]],[[1,1,1],[1,0,0],[1,1,0],[1,1,0]],[[1,1,0],[0,0,0],[1,0,0],[1,0,0]],[[1,0,0],[0,0,0],[0,0,0],[0,0,0]]]
                                                                                                                      O # And take the sum of each inner list
                                                                                                                      # → [[2,1,2,2],[3,1,2,2],[2,0,1,1],[1,0,0,0]]
                                                                                                                      ø # Zip/transpose; swapping rows/columns the entire matrix again
                                                                                                                      # i.e. [[2,1,2,2],[3,1,2,2],[2,0,1,1],[1,0,0,0]]
                                                                                                                      # → [[2,3,2,1],[1,1,0,0],[2,2,1,0],[2,2,1,0]]
                                                                                                                      } # After the outer loop:
                                                                                                                      # i.e. [[3,5,5,4,2],[4,6,5,4,2],[2,3,2,2,1],[1,1,0,0,0]]
                                                                                                                      * # Multiple each value with the input-matrix at the same positions,
                                                                                                                      # which implicitly removes the trailing values
                                                                                                                      # i.e. [[3,5,5,4,2],[4,6,5,4,2],[2,3,2,2,1],[1,1,0,0,0]]
                                                                                                                      # and [[0,0,0,0],[0,1,0,0],[0,1,1,1],[1,1,1,1]]
                                                                                                                      # → [[0,0,0,0],[0,1,0,0],[0,2,1,0],[2,2,1,0]]
                                                                                                                      Ā # Truthify each value (0 remains 0; everything else becomes 1)
                                                                                                                      # i.e. [[0,0,0,0],[0,1,0,0],[0,2,1,0],[2,2,1,0]]
                                                                                                                      # → [[0,0,0,0],[0,1,0,0],[0,1,1,1],[1,1,0,0]]
                                                                                                                      + # Then add each value with the input-matrix at the same positions
                                                                                                                      # i.e. [[0,0,0,0],[0,1,0,0],[0,1,1,1],[1,1,0,0]]
                                                                                                                      # and [[0,0,0,0],[0,1,0,0],[0,1,1,1],[1,1,1,1]]
                                                                                                                      # → [[0,0,0,0],[0,2,0,0],[0,2,2,2],[2,2,1,1]]
                                                                                                                      # (and output the result implicitly)






                                                                                                                      share|improve this answer












                                                                                                                      share|improve this answer



                                                                                                                      share|improve this answer










                                                                                                                      answered Jan 17 at 14:39









                                                                                                                      Kevin CruijssenKevin Cruijssen

                                                                                                                      36.8k555192




                                                                                                                      36.8k555192























                                                                                                                          0












                                                                                                                          $begingroup$

                                                                                                                          TSQL, 252 bytes



                                                                                                                          Splitting the string is very costly, if the string was split and already in a table the byte count would be 127 characters. Script included in the bottom and completely different. Sorry for taking up this much space.



                                                                                                                          Golfed:



                                                                                                                          WITH C as(SELECT x,x/z r,x%z c,substring(@,x+1,1)v
                                                                                                                          FROM spt_values CROSS APPLY(SELECT number x,charindex(char(10),@)z)z
                                                                                                                          WHERE'P'=type)SELECT @=stuff(@,x+1,1,'+')FROM c WHERE
                                                                                                                          exists(SELECT*FROM c d WHERE abs(r-c.r)<2and
                                                                                                                          abs(c-c.c)<2and'AB'=v+c.v)PRINT @


                                                                                                                          Ungolfed:



                                                                                                                          DECLARE @ varchar(max)=
                                                                                                                          'AAAAAAAAAA
                                                                                                                          ABAAAAAABA
                                                                                                                          ABBBAABABA
                                                                                                                          ABBBAABABA
                                                                                                                          ABBBBABABA
                                                                                                                          ABBBBABBBB
                                                                                                                          ABBBBABBBB
                                                                                                                          ABBBBBBBBB
                                                                                                                          BBBBBBBBBB';

                                                                                                                          WITH C as
                                                                                                                          (
                                                                                                                          SELECT x,x/z r,x%z c,substring(@,x+1,1)v
                                                                                                                          FROM spt_values
                                                                                                                          CROSS APPLY(SELECT number x,charindex(char(10),@)z)z
                                                                                                                          WHERE'P'=type
                                                                                                                          )
                                                                                                                          SELECT
                                                                                                                          @=stuff(@,x+1,1,'+')
                                                                                                                          FROM c
                                                                                                                          WHERE exists(SELECT*FROM c d
                                                                                                                          WHERE abs(r-c.r)<2
                                                                                                                          and abs(c-c.c)<2 and'AB'=v+c.v)
                                                                                                                          PRINT @


                                                                                                                          Try it out



                                                                                                                          TSQL, 127 bytes(Using table variable as input)



                                                                                                                          Execute this script in management studio - use "query"-"result to text" to make it readable



                                                                                                                          --populate table variable
                                                                                                                          USE master
                                                                                                                          DECLARE @v varchar(max)=
                                                                                                                          'AAAAAAAAAA
                                                                                                                          ABAAAAAABA
                                                                                                                          ABBBAABABA
                                                                                                                          ABBBAABABA
                                                                                                                          ABBBBABABA
                                                                                                                          ABBBBABBBB
                                                                                                                          ABBBBABBBB
                                                                                                                          ABBBBBBBBB
                                                                                                                          BBBBBBBBBB'

                                                                                                                          DECLARE @ table(x int, r int, c int, v char)

                                                                                                                          INSERT @
                                                                                                                          SELECT x+1,x/z,x%z,substring(@v,x+1,1)
                                                                                                                          FROM spt_values
                                                                                                                          CROSS APPLY(SELECT number x,charindex(char(10),@v)z)z
                                                                                                                          WHERE'P'=type and len(@v)>number

                                                                                                                          -- query(127 characters)

                                                                                                                          SELECT string_agg(v,'')FROM(SELECT
                                                                                                                          iif(exists(SELECT*FROM @
                                                                                                                          WHERE abs(r-c.r)<2and abs(c-c.c)<2and'AB'=v+c.v),'+',v)v
                                                                                                                          FROM @ c)z


                                                                                                                          Try it out - warning output is selected and not readable. Would be readable with print, but that is not possible using this method






                                                                                                                          share|improve this answer











                                                                                                                          $endgroup$













                                                                                                                          • $begingroup$
                                                                                                                            What makes you think that you can't take a table as argument?
                                                                                                                            $endgroup$
                                                                                                                            – Adám
                                                                                                                            Jan 18 at 12:27










                                                                                                                          • $begingroup$
                                                                                                                            @Adám not a bad idea to create the code using a table as argument as well - I will get right on it
                                                                                                                            $endgroup$
                                                                                                                            – t-clausen.dk
                                                                                                                            Jan 18 at 12:52










                                                                                                                          • $begingroup$
                                                                                                                            @Adám I guess I was wrong, in order to make the script work for 120 characters, I would need both the table and the varchar as input, I did rewrite it. It took 151 characters
                                                                                                                            $endgroup$
                                                                                                                            – t-clausen.dk
                                                                                                                            Jan 18 at 13:25
















                                                                                                                          0












                                                                                                                          $begingroup$

                                                                                                                          TSQL, 252 bytes



                                                                                                                          Splitting the string is very costly, if the string was split and already in a table the byte count would be 127 characters. Script included in the bottom and completely different. Sorry for taking up this much space.



                                                                                                                          Golfed:



                                                                                                                          WITH C as(SELECT x,x/z r,x%z c,substring(@,x+1,1)v
                                                                                                                          FROM spt_values CROSS APPLY(SELECT number x,charindex(char(10),@)z)z
                                                                                                                          WHERE'P'=type)SELECT @=stuff(@,x+1,1,'+')FROM c WHERE
                                                                                                                          exists(SELECT*FROM c d WHERE abs(r-c.r)<2and
                                                                                                                          abs(c-c.c)<2and'AB'=v+c.v)PRINT @


                                                                                                                          Ungolfed:



                                                                                                                          DECLARE @ varchar(max)=
                                                                                                                          'AAAAAAAAAA
                                                                                                                          ABAAAAAABA
                                                                                                                          ABBBAABABA
                                                                                                                          ABBBAABABA
                                                                                                                          ABBBBABABA
                                                                                                                          ABBBBABBBB
                                                                                                                          ABBBBABBBB
                                                                                                                          ABBBBBBBBB
                                                                                                                          BBBBBBBBBB';

                                                                                                                          WITH C as
                                                                                                                          (
                                                                                                                          SELECT x,x/z r,x%z c,substring(@,x+1,1)v
                                                                                                                          FROM spt_values
                                                                                                                          CROSS APPLY(SELECT number x,charindex(char(10),@)z)z
                                                                                                                          WHERE'P'=type
                                                                                                                          )
                                                                                                                          SELECT
                                                                                                                          @=stuff(@,x+1,1,'+')
                                                                                                                          FROM c
                                                                                                                          WHERE exists(SELECT*FROM c d
                                                                                                                          WHERE abs(r-c.r)<2
                                                                                                                          and abs(c-c.c)<2 and'AB'=v+c.v)
                                                                                                                          PRINT @


                                                                                                                          Try it out



                                                                                                                          TSQL, 127 bytes(Using table variable as input)



                                                                                                                          Execute this script in management studio - use "query"-"result to text" to make it readable



                                                                                                                          --populate table variable
                                                                                                                          USE master
                                                                                                                          DECLARE @v varchar(max)=
                                                                                                                          'AAAAAAAAAA
                                                                                                                          ABAAAAAABA
                                                                                                                          ABBBAABABA
                                                                                                                          ABBBAABABA
                                                                                                                          ABBBBABABA
                                                                                                                          ABBBBABBBB
                                                                                                                          ABBBBABBBB
                                                                                                                          ABBBBBBBBB
                                                                                                                          BBBBBBBBBB'

                                                                                                                          DECLARE @ table(x int, r int, c int, v char)

                                                                                                                          INSERT @
                                                                                                                          SELECT x+1,x/z,x%z,substring(@v,x+1,1)
                                                                                                                          FROM spt_values
                                                                                                                          CROSS APPLY(SELECT number x,charindex(char(10),@v)z)z
                                                                                                                          WHERE'P'=type and len(@v)>number

                                                                                                                          -- query(127 characters)

                                                                                                                          SELECT string_agg(v,'')FROM(SELECT
                                                                                                                          iif(exists(SELECT*FROM @
                                                                                                                          WHERE abs(r-c.r)<2and abs(c-c.c)<2and'AB'=v+c.v),'+',v)v
                                                                                                                          FROM @ c)z


                                                                                                                          Try it out - warning output is selected and not readable. Would be readable with print, but that is not possible using this method






                                                                                                                          share|improve this answer











                                                                                                                          $endgroup$













                                                                                                                          • $begingroup$
                                                                                                                            What makes you think that you can't take a table as argument?
                                                                                                                            $endgroup$
                                                                                                                            – Adám
                                                                                                                            Jan 18 at 12:27










                                                                                                                          • $begingroup$
                                                                                                                            @Adám not a bad idea to create the code using a table as argument as well - I will get right on it
                                                                                                                            $endgroup$
                                                                                                                            – t-clausen.dk
                                                                                                                            Jan 18 at 12:52










                                                                                                                          • $begingroup$
                                                                                                                            @Adám I guess I was wrong, in order to make the script work for 120 characters, I would need both the table and the varchar as input, I did rewrite it. It took 151 characters
                                                                                                                            $endgroup$
                                                                                                                            – t-clausen.dk
                                                                                                                            Jan 18 at 13:25














                                                                                                                          0












                                                                                                                          0








                                                                                                                          0





                                                                                                                          $begingroup$

                                                                                                                          TSQL, 252 bytes



                                                                                                                          Splitting the string is very costly, if the string was split and already in a table the byte count would be 127 characters. Script included in the bottom and completely different. Sorry for taking up this much space.



                                                                                                                          Golfed:



                                                                                                                          WITH C as(SELECT x,x/z r,x%z c,substring(@,x+1,1)v
                                                                                                                          FROM spt_values CROSS APPLY(SELECT number x,charindex(char(10),@)z)z
                                                                                                                          WHERE'P'=type)SELECT @=stuff(@,x+1,1,'+')FROM c WHERE
                                                                                                                          exists(SELECT*FROM c d WHERE abs(r-c.r)<2and
                                                                                                                          abs(c-c.c)<2and'AB'=v+c.v)PRINT @


                                                                                                                          Ungolfed:



                                                                                                                          DECLARE @ varchar(max)=
                                                                                                                          'AAAAAAAAAA
                                                                                                                          ABAAAAAABA
                                                                                                                          ABBBAABABA
                                                                                                                          ABBBAABABA
                                                                                                                          ABBBBABABA
                                                                                                                          ABBBBABBBB
                                                                                                                          ABBBBABBBB
                                                                                                                          ABBBBBBBBB
                                                                                                                          BBBBBBBBBB';

                                                                                                                          WITH C as
                                                                                                                          (
                                                                                                                          SELECT x,x/z r,x%z c,substring(@,x+1,1)v
                                                                                                                          FROM spt_values
                                                                                                                          CROSS APPLY(SELECT number x,charindex(char(10),@)z)z
                                                                                                                          WHERE'P'=type
                                                                                                                          )
                                                                                                                          SELECT
                                                                                                                          @=stuff(@,x+1,1,'+')
                                                                                                                          FROM c
                                                                                                                          WHERE exists(SELECT*FROM c d
                                                                                                                          WHERE abs(r-c.r)<2
                                                                                                                          and abs(c-c.c)<2 and'AB'=v+c.v)
                                                                                                                          PRINT @


                                                                                                                          Try it out



                                                                                                                          TSQL, 127 bytes(Using table variable as input)



                                                                                                                          Execute this script in management studio - use "query"-"result to text" to make it readable



                                                                                                                          --populate table variable
                                                                                                                          USE master
                                                                                                                          DECLARE @v varchar(max)=
                                                                                                                          'AAAAAAAAAA
                                                                                                                          ABAAAAAABA
                                                                                                                          ABBBAABABA
                                                                                                                          ABBBAABABA
                                                                                                                          ABBBBABABA
                                                                                                                          ABBBBABBBB
                                                                                                                          ABBBBABBBB
                                                                                                                          ABBBBBBBBB
                                                                                                                          BBBBBBBBBB'

                                                                                                                          DECLARE @ table(x int, r int, c int, v char)

                                                                                                                          INSERT @
                                                                                                                          SELECT x+1,x/z,x%z,substring(@v,x+1,1)
                                                                                                                          FROM spt_values
                                                                                                                          CROSS APPLY(SELECT number x,charindex(char(10),@v)z)z
                                                                                                                          WHERE'P'=type and len(@v)>number

                                                                                                                          -- query(127 characters)

                                                                                                                          SELECT string_agg(v,'')FROM(SELECT
                                                                                                                          iif(exists(SELECT*FROM @
                                                                                                                          WHERE abs(r-c.r)<2and abs(c-c.c)<2and'AB'=v+c.v),'+',v)v
                                                                                                                          FROM @ c)z


                                                                                                                          Try it out - warning output is selected and not readable. Would be readable with print, but that is not possible using this method






                                                                                                                          share|improve this answer











                                                                                                                          $endgroup$



                                                                                                                          TSQL, 252 bytes



                                                                                                                          Splitting the string is very costly, if the string was split and already in a table the byte count would be 127 characters. Script included in the bottom and completely different. Sorry for taking up this much space.



                                                                                                                          Golfed:



                                                                                                                          WITH C as(SELECT x,x/z r,x%z c,substring(@,x+1,1)v
                                                                                                                          FROM spt_values CROSS APPLY(SELECT number x,charindex(char(10),@)z)z
                                                                                                                          WHERE'P'=type)SELECT @=stuff(@,x+1,1,'+')FROM c WHERE
                                                                                                                          exists(SELECT*FROM c d WHERE abs(r-c.r)<2and
                                                                                                                          abs(c-c.c)<2and'AB'=v+c.v)PRINT @


                                                                                                                          Ungolfed:



                                                                                                                          DECLARE @ varchar(max)=
                                                                                                                          'AAAAAAAAAA
                                                                                                                          ABAAAAAABA
                                                                                                                          ABBBAABABA
                                                                                                                          ABBBAABABA
                                                                                                                          ABBBBABABA
                                                                                                                          ABBBBABBBB
                                                                                                                          ABBBBABBBB
                                                                                                                          ABBBBBBBBB
                                                                                                                          BBBBBBBBBB';

                                                                                                                          WITH C as
                                                                                                                          (
                                                                                                                          SELECT x,x/z r,x%z c,substring(@,x+1,1)v
                                                                                                                          FROM spt_values
                                                                                                                          CROSS APPLY(SELECT number x,charindex(char(10),@)z)z
                                                                                                                          WHERE'P'=type
                                                                                                                          )
                                                                                                                          SELECT
                                                                                                                          @=stuff(@,x+1,1,'+')
                                                                                                                          FROM c
                                                                                                                          WHERE exists(SELECT*FROM c d
                                                                                                                          WHERE abs(r-c.r)<2
                                                                                                                          and abs(c-c.c)<2 and'AB'=v+c.v)
                                                                                                                          PRINT @


                                                                                                                          Try it out



                                                                                                                          TSQL, 127 bytes(Using table variable as input)



                                                                                                                          Execute this script in management studio - use "query"-"result to text" to make it readable



                                                                                                                          --populate table variable
                                                                                                                          USE master
                                                                                                                          DECLARE @v varchar(max)=
                                                                                                                          'AAAAAAAAAA
                                                                                                                          ABAAAAAABA
                                                                                                                          ABBBAABABA
                                                                                                                          ABBBAABABA
                                                                                                                          ABBBBABABA
                                                                                                                          ABBBBABBBB
                                                                                                                          ABBBBABBBB
                                                                                                                          ABBBBBBBBB
                                                                                                                          BBBBBBBBBB'

                                                                                                                          DECLARE @ table(x int, r int, c int, v char)

                                                                                                                          INSERT @
                                                                                                                          SELECT x+1,x/z,x%z,substring(@v,x+1,1)
                                                                                                                          FROM spt_values
                                                                                                                          CROSS APPLY(SELECT number x,charindex(char(10),@v)z)z
                                                                                                                          WHERE'P'=type and len(@v)>number

                                                                                                                          -- query(127 characters)

                                                                                                                          SELECT string_agg(v,'')FROM(SELECT
                                                                                                                          iif(exists(SELECT*FROM @
                                                                                                                          WHERE abs(r-c.r)<2and abs(c-c.c)<2and'AB'=v+c.v),'+',v)v
                                                                                                                          FROM @ c)z


                                                                                                                          Try it out - warning output is selected and not readable. Would be readable with print, but that is not possible using this method







                                                                                                                          share|improve this answer














                                                                                                                          share|improve this answer



                                                                                                                          share|improve this answer








                                                                                                                          edited Jan 18 at 16:07

























                                                                                                                          answered Jan 18 at 11:23









                                                                                                                          t-clausen.dkt-clausen.dk

                                                                                                                          1,834314




                                                                                                                          1,834314












                                                                                                                          • $begingroup$
                                                                                                                            What makes you think that you can't take a table as argument?
                                                                                                                            $endgroup$
                                                                                                                            – Adám
                                                                                                                            Jan 18 at 12:27










                                                                                                                          • $begingroup$
                                                                                                                            @Adám not a bad idea to create the code using a table as argument as well - I will get right on it
                                                                                                                            $endgroup$
                                                                                                                            – t-clausen.dk
                                                                                                                            Jan 18 at 12:52










                                                                                                                          • $begingroup$
                                                                                                                            @Adám I guess I was wrong, in order to make the script work for 120 characters, I would need both the table and the varchar as input, I did rewrite it. It took 151 characters
                                                                                                                            $endgroup$
                                                                                                                            – t-clausen.dk
                                                                                                                            Jan 18 at 13:25


















                                                                                                                          • $begingroup$
                                                                                                                            What makes you think that you can't take a table as argument?
                                                                                                                            $endgroup$
                                                                                                                            – Adám
                                                                                                                            Jan 18 at 12:27










                                                                                                                          • $begingroup$
                                                                                                                            @Adám not a bad idea to create the code using a table as argument as well - I will get right on it
                                                                                                                            $endgroup$
                                                                                                                            – t-clausen.dk
                                                                                                                            Jan 18 at 12:52










                                                                                                                          • $begingroup$
                                                                                                                            @Adám I guess I was wrong, in order to make the script work for 120 characters, I would need both the table and the varchar as input, I did rewrite it. It took 151 characters
                                                                                                                            $endgroup$
                                                                                                                            – t-clausen.dk
                                                                                                                            Jan 18 at 13:25
















                                                                                                                          $begingroup$
                                                                                                                          What makes you think that you can't take a table as argument?
                                                                                                                          $endgroup$
                                                                                                                          – Adám
                                                                                                                          Jan 18 at 12:27




                                                                                                                          $begingroup$
                                                                                                                          What makes you think that you can't take a table as argument?
                                                                                                                          $endgroup$
                                                                                                                          – Adám
                                                                                                                          Jan 18 at 12:27












                                                                                                                          $begingroup$
                                                                                                                          @Adám not a bad idea to create the code using a table as argument as well - I will get right on it
                                                                                                                          $endgroup$
                                                                                                                          – t-clausen.dk
                                                                                                                          Jan 18 at 12:52




                                                                                                                          $begingroup$
                                                                                                                          @Adám not a bad idea to create the code using a table as argument as well - I will get right on it
                                                                                                                          $endgroup$
                                                                                                                          – t-clausen.dk
                                                                                                                          Jan 18 at 12:52












                                                                                                                          $begingroup$
                                                                                                                          @Adám I guess I was wrong, in order to make the script work for 120 characters, I would need both the table and the varchar as input, I did rewrite it. It took 151 characters
                                                                                                                          $endgroup$
                                                                                                                          – t-clausen.dk
                                                                                                                          Jan 18 at 13:25




                                                                                                                          $begingroup$
                                                                                                                          @Adám I guess I was wrong, in order to make the script work for 120 characters, I would need both the table and the varchar as input, I did rewrite it. It took 151 characters
                                                                                                                          $endgroup$
                                                                                                                          – t-clausen.dk
                                                                                                                          Jan 18 at 13:25


















                                                                                                                          draft saved

                                                                                                                          draft discarded




















































                                                                                                                          If this is an answer to a challenge…




                                                                                                                          • …Be sure to follow the challenge specification. However, please refrain from exploiting obvious loopholes. Answers abusing any of the standard loopholes are considered invalid. If you think a specification is unclear or underspecified, comment on the question instead.


                                                                                                                          • …Try to optimize your score. For instance, answers to code-golf challenges should attempt to be as short as possible. You can always include a readable version of the code in addition to the competitive one.
                                                                                                                            Explanations of your answer make it more interesting to read and are very much encouraged.


                                                                                                                          • …Include a short header which indicates the language(s) of your code and its score, as defined by the challenge.



                                                                                                                          More generally…




                                                                                                                          • …Please make sure to answer the question and provide sufficient detail.


                                                                                                                          • …Avoid asking for help, clarification or responding to other answers (use comments instead).





                                                                                                                          draft saved


                                                                                                                          draft discarded














                                                                                                                          StackExchange.ready(
                                                                                                                          function () {
                                                                                                                          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f178809%2fdig-a-border-trench%23new-answer', 'question_page');
                                                                                                                          }
                                                                                                                          );

                                                                                                                          Post as a guest















                                                                                                                          Required, but never shown





















































                                                                                                                          Required, but never shown














                                                                                                                          Required, but never shown












                                                                                                                          Required, but never shown







                                                                                                                          Required, but never shown

































                                                                                                                          Required, but never shown














                                                                                                                          Required, but never shown












                                                                                                                          Required, but never shown







                                                                                                                          Required, but never shown







                                                                                                                          Popular posts from this blog

                                                                                                                          Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

                                                                                                                          ComboBox Display Member on multiple fields

                                                                                                                          Is it possible to collect Nectar points via Trainline?