Dig a border trench












28












$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$








  • 3




    $begingroup$
    Political satire in the form of code golf, I love it :o)
    $endgroup$
    – Sok
    12 hours ago






  • 2




    $begingroup$
    -1 for that <sup><sub><sup><sub><sup><sub><sup><sub> :-P
    $endgroup$
    – Luis Mendo
    9 hours ago










  • $begingroup$
    Does the input/output have to be a list of strings or are other formats acceptable? my preference is single string with newline separators. Others may prefer 2D array of characters, etc.
    $endgroup$
    – Level River St
    8 hours ago






  • 1




    $begingroup$
    python, 4 bytes: pass The plans to build a border trench lead to a government shutdown and nothing happens.
    $endgroup$
    – TheEspinosa
    7 hours ago








  • 1




    $begingroup$
    @TheEspinosa No no, the shutdown is until the trench is arranged.
    $endgroup$
    – Adám
    7 hours ago
















28












$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$








  • 3




    $begingroup$
    Political satire in the form of code golf, I love it :o)
    $endgroup$
    – Sok
    12 hours ago






  • 2




    $begingroup$
    -1 for that <sup><sub><sup><sub><sup><sub><sup><sub> :-P
    $endgroup$
    – Luis Mendo
    9 hours ago










  • $begingroup$
    Does the input/output have to be a list of strings or are other formats acceptable? my preference is single string with newline separators. Others may prefer 2D array of characters, etc.
    $endgroup$
    – Level River St
    8 hours ago






  • 1




    $begingroup$
    python, 4 bytes: pass The plans to build a border trench lead to a government shutdown and nothing happens.
    $endgroup$
    – TheEspinosa
    7 hours ago








  • 1




    $begingroup$
    @TheEspinosa No no, the shutdown is until the trench is arranged.
    $endgroup$
    – Adám
    7 hours ago














28












28








28


4



$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 11 hours ago







Adám

















asked 12 hours ago









AdámAdám

29.3k270194




29.3k270194








  • 3




    $begingroup$
    Political satire in the form of code golf, I love it :o)
    $endgroup$
    – Sok
    12 hours ago






  • 2




    $begingroup$
    -1 for that <sup><sub><sup><sub><sup><sub><sup><sub> :-P
    $endgroup$
    – Luis Mendo
    9 hours ago










  • $begingroup$
    Does the input/output have to be a list of strings or are other formats acceptable? my preference is single string with newline separators. Others may prefer 2D array of characters, etc.
    $endgroup$
    – Level River St
    8 hours ago






  • 1




    $begingroup$
    python, 4 bytes: pass The plans to build a border trench lead to a government shutdown and nothing happens.
    $endgroup$
    – TheEspinosa
    7 hours ago








  • 1




    $begingroup$
    @TheEspinosa No no, the shutdown is until the trench is arranged.
    $endgroup$
    – Adám
    7 hours ago














  • 3




    $begingroup$
    Political satire in the form of code golf, I love it :o)
    $endgroup$
    – Sok
    12 hours ago






  • 2




    $begingroup$
    -1 for that <sup><sub><sup><sub><sup><sub><sup><sub> :-P
    $endgroup$
    – Luis Mendo
    9 hours ago










  • $begingroup$
    Does the input/output have to be a list of strings or are other formats acceptable? my preference is single string with newline separators. Others may prefer 2D array of characters, etc.
    $endgroup$
    – Level River St
    8 hours ago






  • 1




    $begingroup$
    python, 4 bytes: pass The plans to build a border trench lead to a government shutdown and nothing happens.
    $endgroup$
    – TheEspinosa
    7 hours ago








  • 1




    $begingroup$
    @TheEspinosa No no, the shutdown is until the trench is arranged.
    $endgroup$
    – Adám
    7 hours ago








3




3




$begingroup$
Political satire in the form of code golf, I love it :o)
$endgroup$
– Sok
12 hours ago




$begingroup$
Political satire in the form of code golf, I love it :o)
$endgroup$
– Sok
12 hours ago




2




2




$begingroup$
-1 for that <sup><sub><sup><sub><sup><sub><sup><sub> :-P
$endgroup$
– Luis Mendo
9 hours ago




$begingroup$
-1 for that <sup><sub><sup><sub><sup><sub><sup><sub> :-P
$endgroup$
– Luis Mendo
9 hours ago












$begingroup$
Does the input/output have to be a list of strings or are other formats acceptable? my preference is single string with newline separators. Others may prefer 2D array of characters, etc.
$endgroup$
– Level River St
8 hours ago




$begingroup$
Does the input/output have to be a list of strings or are other formats acceptable? my preference is single string with newline separators. Others may prefer 2D array of characters, etc.
$endgroup$
– Level River St
8 hours ago




1




1




$begingroup$
python, 4 bytes: pass The plans to build a border trench lead to a government shutdown and nothing happens.
$endgroup$
– TheEspinosa
7 hours ago






$begingroup$
python, 4 bytes: pass The plans to build a border trench lead to a government shutdown and nothing happens.
$endgroup$
– TheEspinosa
7 hours ago






1




1




$begingroup$
@TheEspinosa No no, the shutdown is until the trench is arranged.
$endgroup$
– Adám
7 hours ago




$begingroup$
@TheEspinosa No no, the shutdown is until the trench is arranged.
$endgroup$
– Adám
7 hours ago










10 Answers
10






active

oldest

votes


















14












$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$





















    5












    $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$





















      3












      $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!






      share|improve this answer











      $endgroup$













      • $begingroup$
        82 bytes?
        $endgroup$
        – Shaggy
        11 hours ago






      • 2




        $begingroup$
        @Shaggy Not enough questions lately. I don't know how to golf anymore. :D Thanks!
        $endgroup$
        – Arnauld
        11 hours ago










      • $begingroup$
        I was only thinking the same thing earlier!
        $endgroup$
        – Shaggy
        11 hours ago



















      3












      $begingroup$


      Octave, 37 31 bytes



      This function performs a morphological dilation on the Astan (1-b) part of the "image" using conv2 imdilate, and then uses some arithmetic to make all three areas different symbols.





      @(b)imdilate(1-b,ones(3))-1+2*b


      Try it online!






      share|improve this answer











      $endgroup$









      • 1




        $begingroup$
        -1 for no convolution :-P
        $endgroup$
        – Luis Mendo
        9 hours ago










      • $begingroup$
        @LuisMendo An earlier version did include a convolution:)
        $endgroup$
        – flawr
        8 hours ago










      • $begingroup$
        You can replace 1- by ~
        $endgroup$
        – Luis Mendo
        5 hours ago










      • $begingroup$
        Better yet (inspired by listoriast's answer): @(b)-imerode(b,ones(3))+2*b
        $endgroup$
        – Luis Mendo
        5 hours ago










      • $begingroup$
        Make that @(b)2*b-imerode(b,ones(3))
        $endgroup$
        – Luis Mendo
        5 hours ago



















      3












      $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
        7 hours ago










      • $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
        5 hours ago










      • $begingroup$
        What is di in your test t?
        $endgroup$
        – Adám
        4 hours ago










      • $begingroup$
        @Adám an alias for display which i forgot to remove. removed now
        $endgroup$
        – ngn
        3 hours ago





















      2












      $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$





















        1












        $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$





















          0












          $begingroup$


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



          I'm kinda ashamed to post this.





          a=>{a=a.Select(b=>b.Prepend(0).Append(0)).Prepend(Enumerable.Repeat(0,a.Count+2)).Append(Enumerable.Repeat(0,a.Count+2)).Select(x=>x.ToList()).ToList();return a.Select((b,c)=>c<1|c==b.Count()-1?b:b.Select((d,e)=>d>1|e<1|e>b.Count-2?d:a[c-1].Skip(e-1).Take(3).Concat(a[c+1].Skip(e-1).Take(3)).Contains(2)|b[e-1]>1|b[e+1]>1?6:d)).Skip(1).Take(a.Count-2).Select(c=>c.Skip(1).Take(c.Count()-2));}


          What it does is pad the whole matrix with 0s to avoid index out of bounds exception. Then it checks for each Blandia cell if an Astanian one is close by. Uses 2 for Astan, 1 for Blandia, 6 for trench.



          Try it online!






          share|improve this answer









          $endgroup$





















            0












            $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$





















              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$













                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

























                10 Answers
                10






                active

                oldest

                votes








                10 Answers
                10






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                14












                $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$


















                  14












                  $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$
















                    14












                    14








                    14





                    $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 7 hours ago

























                    answered 11 hours ago









                    lirtosiastlirtosiast

                    15.9k436108




                    15.9k436108























                        5












                        $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$


















                          5












                          $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$
















                            5












                            5








                            5





                            $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 10 hours ago

























                            answered 11 hours ago









                            ngnngn

                            7,04112559




                            7,04112559























                                3












                                $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!






                                share|improve this answer











                                $endgroup$













                                • $begingroup$
                                  82 bytes?
                                  $endgroup$
                                  – Shaggy
                                  11 hours ago






                                • 2




                                  $begingroup$
                                  @Shaggy Not enough questions lately. I don't know how to golf anymore. :D Thanks!
                                  $endgroup$
                                  – Arnauld
                                  11 hours ago










                                • $begingroup$
                                  I was only thinking the same thing earlier!
                                  $endgroup$
                                  – Shaggy
                                  11 hours ago
















                                3












                                $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!






                                share|improve this answer











                                $endgroup$













                                • $begingroup$
                                  82 bytes?
                                  $endgroup$
                                  – Shaggy
                                  11 hours ago






                                • 2




                                  $begingroup$
                                  @Shaggy Not enough questions lately. I don't know how to golf anymore. :D Thanks!
                                  $endgroup$
                                  – Arnauld
                                  11 hours ago










                                • $begingroup$
                                  I was only thinking the same thing earlier!
                                  $endgroup$
                                  – Shaggy
                                  11 hours ago














                                3












                                3








                                3





                                $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!






                                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!







                                share|improve this answer














                                share|improve this answer



                                share|improve this answer








                                edited 11 hours ago

























                                answered 11 hours ago









                                ArnauldArnauld

                                73.3k689307




                                73.3k689307












                                • $begingroup$
                                  82 bytes?
                                  $endgroup$
                                  – Shaggy
                                  11 hours ago






                                • 2




                                  $begingroup$
                                  @Shaggy Not enough questions lately. I don't know how to golf anymore. :D Thanks!
                                  $endgroup$
                                  – Arnauld
                                  11 hours ago










                                • $begingroup$
                                  I was only thinking the same thing earlier!
                                  $endgroup$
                                  – Shaggy
                                  11 hours ago


















                                • $begingroup$
                                  82 bytes?
                                  $endgroup$
                                  – Shaggy
                                  11 hours ago






                                • 2




                                  $begingroup$
                                  @Shaggy Not enough questions lately. I don't know how to golf anymore. :D Thanks!
                                  $endgroup$
                                  – Arnauld
                                  11 hours ago










                                • $begingroup$
                                  I was only thinking the same thing earlier!
                                  $endgroup$
                                  – Shaggy
                                  11 hours ago
















                                $begingroup$
                                82 bytes?
                                $endgroup$
                                – Shaggy
                                11 hours ago




                                $begingroup$
                                82 bytes?
                                $endgroup$
                                – Shaggy
                                11 hours ago




                                2




                                2




                                $begingroup$
                                @Shaggy Not enough questions lately. I don't know how to golf anymore. :D Thanks!
                                $endgroup$
                                – Arnauld
                                11 hours ago




                                $begingroup$
                                @Shaggy Not enough questions lately. I don't know how to golf anymore. :D Thanks!
                                $endgroup$
                                – Arnauld
                                11 hours ago












                                $begingroup$
                                I was only thinking the same thing earlier!
                                $endgroup$
                                – Shaggy
                                11 hours ago




                                $begingroup$
                                I was only thinking the same thing earlier!
                                $endgroup$
                                – Shaggy
                                11 hours ago











                                3












                                $begingroup$


                                Octave, 37 31 bytes



                                This function performs a morphological dilation on the Astan (1-b) part of the "image" using conv2 imdilate, and then uses some arithmetic to make all three areas different symbols.





                                @(b)imdilate(1-b,ones(3))-1+2*b


                                Try it online!






                                share|improve this answer











                                $endgroup$









                                • 1




                                  $begingroup$
                                  -1 for no convolution :-P
                                  $endgroup$
                                  – Luis Mendo
                                  9 hours ago










                                • $begingroup$
                                  @LuisMendo An earlier version did include a convolution:)
                                  $endgroup$
                                  – flawr
                                  8 hours ago










                                • $begingroup$
                                  You can replace 1- by ~
                                  $endgroup$
                                  – Luis Mendo
                                  5 hours ago










                                • $begingroup$
                                  Better yet (inspired by listoriast's answer): @(b)-imerode(b,ones(3))+2*b
                                  $endgroup$
                                  – Luis Mendo
                                  5 hours ago










                                • $begingroup$
                                  Make that @(b)2*b-imerode(b,ones(3))
                                  $endgroup$
                                  – Luis Mendo
                                  5 hours ago
















                                3












                                $begingroup$


                                Octave, 37 31 bytes



                                This function performs a morphological dilation on the Astan (1-b) part of the "image" using conv2 imdilate, and then uses some arithmetic to make all three areas different symbols.





                                @(b)imdilate(1-b,ones(3))-1+2*b


                                Try it online!






                                share|improve this answer











                                $endgroup$









                                • 1




                                  $begingroup$
                                  -1 for no convolution :-P
                                  $endgroup$
                                  – Luis Mendo
                                  9 hours ago










                                • $begingroup$
                                  @LuisMendo An earlier version did include a convolution:)
                                  $endgroup$
                                  – flawr
                                  8 hours ago










                                • $begingroup$
                                  You can replace 1- by ~
                                  $endgroup$
                                  – Luis Mendo
                                  5 hours ago










                                • $begingroup$
                                  Better yet (inspired by listoriast's answer): @(b)-imerode(b,ones(3))+2*b
                                  $endgroup$
                                  – Luis Mendo
                                  5 hours ago










                                • $begingroup$
                                  Make that @(b)2*b-imerode(b,ones(3))
                                  $endgroup$
                                  – Luis Mendo
                                  5 hours ago














                                3












                                3








                                3





                                $begingroup$


                                Octave, 37 31 bytes



                                This function performs a morphological dilation on the Astan (1-b) part of the "image" using conv2 imdilate, and then uses some arithmetic to make all three areas different symbols.





                                @(b)imdilate(1-b,ones(3))-1+2*b


                                Try it online!






                                share|improve this answer











                                $endgroup$




                                Octave, 37 31 bytes



                                This function performs a morphological dilation on the Astan (1-b) part of the "image" using conv2 imdilate, and then uses some arithmetic to make all three areas different symbols.





                                @(b)imdilate(1-b,ones(3))-1+2*b


                                Try it online!







                                share|improve this answer














                                share|improve this answer



                                share|improve this answer








                                edited 10 hours ago

























                                answered 10 hours ago









                                flawrflawr

                                26.7k665188




                                26.7k665188








                                • 1




                                  $begingroup$
                                  -1 for no convolution :-P
                                  $endgroup$
                                  – Luis Mendo
                                  9 hours ago










                                • $begingroup$
                                  @LuisMendo An earlier version did include a convolution:)
                                  $endgroup$
                                  – flawr
                                  8 hours ago










                                • $begingroup$
                                  You can replace 1- by ~
                                  $endgroup$
                                  – Luis Mendo
                                  5 hours ago










                                • $begingroup$
                                  Better yet (inspired by listoriast's answer): @(b)-imerode(b,ones(3))+2*b
                                  $endgroup$
                                  – Luis Mendo
                                  5 hours ago










                                • $begingroup$
                                  Make that @(b)2*b-imerode(b,ones(3))
                                  $endgroup$
                                  – Luis Mendo
                                  5 hours ago














                                • 1




                                  $begingroup$
                                  -1 for no convolution :-P
                                  $endgroup$
                                  – Luis Mendo
                                  9 hours ago










                                • $begingroup$
                                  @LuisMendo An earlier version did include a convolution:)
                                  $endgroup$
                                  – flawr
                                  8 hours ago










                                • $begingroup$
                                  You can replace 1- by ~
                                  $endgroup$
                                  – Luis Mendo
                                  5 hours ago










                                • $begingroup$
                                  Better yet (inspired by listoriast's answer): @(b)-imerode(b,ones(3))+2*b
                                  $endgroup$
                                  – Luis Mendo
                                  5 hours ago










                                • $begingroup$
                                  Make that @(b)2*b-imerode(b,ones(3))
                                  $endgroup$
                                  – Luis Mendo
                                  5 hours ago








                                1




                                1




                                $begingroup$
                                -1 for no convolution :-P
                                $endgroup$
                                – Luis Mendo
                                9 hours ago




                                $begingroup$
                                -1 for no convolution :-P
                                $endgroup$
                                – Luis Mendo
                                9 hours ago












                                $begingroup$
                                @LuisMendo An earlier version did include a convolution:)
                                $endgroup$
                                – flawr
                                8 hours ago




                                $begingroup$
                                @LuisMendo An earlier version did include a convolution:)
                                $endgroup$
                                – flawr
                                8 hours ago












                                $begingroup$
                                You can replace 1- by ~
                                $endgroup$
                                – Luis Mendo
                                5 hours ago




                                $begingroup$
                                You can replace 1- by ~
                                $endgroup$
                                – Luis Mendo
                                5 hours ago












                                $begingroup$
                                Better yet (inspired by listoriast's answer): @(b)-imerode(b,ones(3))+2*b
                                $endgroup$
                                – Luis Mendo
                                5 hours ago




                                $begingroup$
                                Better yet (inspired by listoriast's answer): @(b)-imerode(b,ones(3))+2*b
                                $endgroup$
                                – Luis Mendo
                                5 hours ago












                                $begingroup$
                                Make that @(b)2*b-imerode(b,ones(3))
                                $endgroup$
                                – Luis Mendo
                                5 hours ago




                                $begingroup$
                                Make that @(b)2*b-imerode(b,ones(3))
                                $endgroup$
                                – Luis Mendo
                                5 hours ago











                                3












                                $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
                                  7 hours ago










                                • $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
                                  5 hours ago










                                • $begingroup$
                                  What is di in your test t?
                                  $endgroup$
                                  – Adám
                                  4 hours ago










                                • $begingroup$
                                  @Adám an alias for display which i forgot to remove. removed now
                                  $endgroup$
                                  – ngn
                                  3 hours ago


















                                3












                                $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
                                  7 hours ago










                                • $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
                                  5 hours ago










                                • $begingroup$
                                  What is di in your test t?
                                  $endgroup$
                                  – Adám
                                  4 hours ago










                                • $begingroup$
                                  @Adám an alias for display which i forgot to remove. removed now
                                  $endgroup$
                                  – ngn
                                  3 hours ago
















                                3












                                3








                                3





                                $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 3 hours ago

























                                answered 7 hours ago









                                ngnngn

                                7,04112559




                                7,04112559












                                • $begingroup$
                                  Of course, switching to Stencil would save you more than half of your bytes.
                                  $endgroup$
                                  – Adám
                                  7 hours ago










                                • $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
                                  5 hours ago










                                • $begingroup$
                                  What is di in your test t?
                                  $endgroup$
                                  – Adám
                                  4 hours ago










                                • $begingroup$
                                  @Adám an alias for display which i forgot to remove. removed now
                                  $endgroup$
                                  – ngn
                                  3 hours ago




















                                • $begingroup$
                                  Of course, switching to Stencil would save you more than half of your bytes.
                                  $endgroup$
                                  – Adám
                                  7 hours ago










                                • $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
                                  5 hours ago










                                • $begingroup$
                                  What is di in your test t?
                                  $endgroup$
                                  – Adám
                                  4 hours ago










                                • $begingroup$
                                  @Adám an alias for display which i forgot to remove. removed now
                                  $endgroup$
                                  – ngn
                                  3 hours ago


















                                $begingroup$
                                Of course, switching to Stencil would save you more than half of your bytes.
                                $endgroup$
                                – Adám
                                7 hours ago




                                $begingroup$
                                Of course, switching to Stencil would save you more than half of your bytes.
                                $endgroup$
                                – Adám
                                7 hours ago












                                $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
                                5 hours ago




                                $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
                                5 hours ago












                                $begingroup$
                                What is di in your test t?
                                $endgroup$
                                – Adám
                                4 hours ago




                                $begingroup$
                                What is di in your test t?
                                $endgroup$
                                – Adám
                                4 hours ago












                                $begingroup$
                                @Adám an alias for display which i forgot to remove. removed now
                                $endgroup$
                                – ngn
                                3 hours ago






                                $begingroup$
                                @Adám an alias for display which i forgot to remove. removed now
                                $endgroup$
                                – ngn
                                3 hours ago













                                2












                                $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$


















                                  2












                                  $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$
















                                    2












                                    2








                                    2





                                    $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 5 hours ago

























                                    answered 7 hours ago









                                    Luis MendoLuis Mendo

                                    74.1k886291




                                    74.1k886291























                                        1












                                        $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$


















                                          1












                                          $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$
















                                            1












                                            1








                                            1





                                            $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 7 hours ago









                                            NeilNeil

                                            79.8k744177




                                            79.8k744177























                                                0












                                                $begingroup$


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



                                                I'm kinda ashamed to post this.





                                                a=>{a=a.Select(b=>b.Prepend(0).Append(0)).Prepend(Enumerable.Repeat(0,a.Count+2)).Append(Enumerable.Repeat(0,a.Count+2)).Select(x=>x.ToList()).ToList();return a.Select((b,c)=>c<1|c==b.Count()-1?b:b.Select((d,e)=>d>1|e<1|e>b.Count-2?d:a[c-1].Skip(e-1).Take(3).Concat(a[c+1].Skip(e-1).Take(3)).Contains(2)|b[e-1]>1|b[e+1]>1?6:d)).Skip(1).Take(a.Count-2).Select(c=>c.Skip(1).Take(c.Count()-2));}


                                                What it does is pad the whole matrix with 0s to avoid index out of bounds exception. Then it checks for each Blandia cell if an Astanian one is close by. Uses 2 for Astan, 1 for Blandia, 6 for trench.



                                                Try it online!






                                                share|improve this answer









                                                $endgroup$


















                                                  0












                                                  $begingroup$


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



                                                  I'm kinda ashamed to post this.





                                                  a=>{a=a.Select(b=>b.Prepend(0).Append(0)).Prepend(Enumerable.Repeat(0,a.Count+2)).Append(Enumerable.Repeat(0,a.Count+2)).Select(x=>x.ToList()).ToList();return a.Select((b,c)=>c<1|c==b.Count()-1?b:b.Select((d,e)=>d>1|e<1|e>b.Count-2?d:a[c-1].Skip(e-1).Take(3).Concat(a[c+1].Skip(e-1).Take(3)).Contains(2)|b[e-1]>1|b[e+1]>1?6:d)).Skip(1).Take(a.Count-2).Select(c=>c.Skip(1).Take(c.Count()-2));}


                                                  What it does is pad the whole matrix with 0s to avoid index out of bounds exception. Then it checks for each Blandia cell if an Astanian one is close by. Uses 2 for Astan, 1 for Blandia, 6 for trench.



                                                  Try it online!






                                                  share|improve this answer









                                                  $endgroup$
















                                                    0












                                                    0








                                                    0





                                                    $begingroup$


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



                                                    I'm kinda ashamed to post this.





                                                    a=>{a=a.Select(b=>b.Prepend(0).Append(0)).Prepend(Enumerable.Repeat(0,a.Count+2)).Append(Enumerable.Repeat(0,a.Count+2)).Select(x=>x.ToList()).ToList();return a.Select((b,c)=>c<1|c==b.Count()-1?b:b.Select((d,e)=>d>1|e<1|e>b.Count-2?d:a[c-1].Skip(e-1).Take(3).Concat(a[c+1].Skip(e-1).Take(3)).Contains(2)|b[e-1]>1|b[e+1]>1?6:d)).Skip(1).Take(a.Count-2).Select(c=>c.Skip(1).Take(c.Count()-2));}


                                                    What it does is pad the whole matrix with 0s to avoid index out of bounds exception. Then it checks for each Blandia cell if an Astanian one is close by. Uses 2 for Astan, 1 for Blandia, 6 for trench.



                                                    Try it online!






                                                    share|improve this answer









                                                    $endgroup$




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



                                                    I'm kinda ashamed to post this.





                                                    a=>{a=a.Select(b=>b.Prepend(0).Append(0)).Prepend(Enumerable.Repeat(0,a.Count+2)).Append(Enumerable.Repeat(0,a.Count+2)).Select(x=>x.ToList()).ToList();return a.Select((b,c)=>c<1|c==b.Count()-1?b:b.Select((d,e)=>d>1|e<1|e>b.Count-2?d:a[c-1].Skip(e-1).Take(3).Concat(a[c+1].Skip(e-1).Take(3)).Contains(2)|b[e-1]>1|b[e+1]>1?6:d)).Skip(1).Take(a.Count-2).Select(c=>c.Skip(1).Take(c.Count()-2));}


                                                    What it does is pad the whole matrix with 0s to avoid index out of bounds exception. Then it checks for each Blandia cell if an Astanian one is close by. Uses 2 for Astan, 1 for Blandia, 6 for trench.



                                                    Try it online!







                                                    share|improve this answer












                                                    share|improve this answer



                                                    share|improve this answer










                                                    answered 6 hours ago









                                                    Embodiment of IgnoranceEmbodiment of Ignorance

                                                    621115




                                                    621115























                                                        0












                                                        $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$


















                                                          0












                                                          $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$
















                                                            0












                                                            0








                                                            0





                                                            $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 6 hours ago

























                                                            answered 6 hours ago









                                                            NeilNeil

                                                            79.8k744177




                                                            79.8k744177























                                                                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 5 hours ago









                                                                    Level River StLevel River St

                                                                    20.2k32579




                                                                    20.2k32579






























                                                                        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

                                                                        GameSpot

                                                                        日野市

                                                                        Tu-95轟炸機