Crop ASCII Art Challenge

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
4
down vote

favorite












ASCII art is fun. Modern text editors are very good at manipulating text. Are modern programming languages up to the task?



One common task in ASCII art manipulation is to crop text to a rectangle between two characters. This is the task you must implement in this challenge.



Details



Your program will take 3 inputs:



  • the first is the 'start' character of the block - marking the top-left corner

  • the second is the 'end' character of the block - marking the bottom-right corner

  • the third is some form of multiline text, either a string, or list of strings, or filename, or whatever

The result will be multiline text cropped to the rectangle between the given inputs. Note that the first two inputs may not be unique.



Edge cases



Boxes must always have volume at least 2. Thus these:



() (
)


are boxes but these:



)( ) (
( )


are not (with start=( and end=)).



The input will only contain one box. Thus the start and end characters must only occur once, unless they are the same character in which case they must occur exactly twice.



Additionally each line in the input must be at least as long as the distance from the start of a line to the right edge of the box in the input.



Rules



Typical code-golf rules apply. Shortest code wins.



Examples



Sunny-day: start: ( end: ) input:



This is some text
(but this text
is in a box )
So only it is important.


Output:



(but this text
is in a box )


Note the stripping of horizontal whitespace as well. ASCII art crops are 2d.



Rainy-day: start: ( end: ) input:



This is some text (
But is that even )
really a box?


Output:



(
)


Same start/end: start: / end: / input:



Oh, I get how this could be useful
/----------------------------
| All this text is in a box! |
----------------------------/


Output:



/----------------------------
| All this text is in a box! |
----------------------------/


Invalid input: `start: ( end: ) input:



Boxes are rectangular ( so this has
0 volume ) which is illegal.


Invalid input 2: `start: ( end: ) input:



(The lines must already be square 
so this line that is too short
relative to this end, is illegal)









share|improve this question























  • What about a valid box with a line outside that is shorter than the box?
    – seadoggie01
    1 hour ago






  • 1




    clarified, also invalid input
    – LambdaBeta
    1 hour ago










  • what should be the outcome in case of an invalid input? or are they mentioned so they won't have to be taken care of?
    – Uriel
    1 hour ago











  • The outcome is much like undefined behaviour in C, don't worry about it, anything goes.
    – LambdaBeta
    1 hour ago










  • This is a nasty little challenge: nice job!
    – seadoggie01
    1 hour ago














up vote
4
down vote

favorite












ASCII art is fun. Modern text editors are very good at manipulating text. Are modern programming languages up to the task?



One common task in ASCII art manipulation is to crop text to a rectangle between two characters. This is the task you must implement in this challenge.



Details



Your program will take 3 inputs:



  • the first is the 'start' character of the block - marking the top-left corner

  • the second is the 'end' character of the block - marking the bottom-right corner

  • the third is some form of multiline text, either a string, or list of strings, or filename, or whatever

The result will be multiline text cropped to the rectangle between the given inputs. Note that the first two inputs may not be unique.



Edge cases



Boxes must always have volume at least 2. Thus these:



() (
)


are boxes but these:



)( ) (
( )


are not (with start=( and end=)).



The input will only contain one box. Thus the start and end characters must only occur once, unless they are the same character in which case they must occur exactly twice.



Additionally each line in the input must be at least as long as the distance from the start of a line to the right edge of the box in the input.



Rules



Typical code-golf rules apply. Shortest code wins.



Examples



Sunny-day: start: ( end: ) input:



This is some text
(but this text
is in a box )
So only it is important.


Output:



(but this text
is in a box )


Note the stripping of horizontal whitespace as well. ASCII art crops are 2d.



Rainy-day: start: ( end: ) input:



This is some text (
But is that even )
really a box?


Output:



(
)


Same start/end: start: / end: / input:



Oh, I get how this could be useful
/----------------------------
| All this text is in a box! |
----------------------------/


Output:



/----------------------------
| All this text is in a box! |
----------------------------/


Invalid input: `start: ( end: ) input:



Boxes are rectangular ( so this has
0 volume ) which is illegal.


Invalid input 2: `start: ( end: ) input:



(The lines must already be square 
so this line that is too short
relative to this end, is illegal)









share|improve this question























  • What about a valid box with a line outside that is shorter than the box?
    – seadoggie01
    1 hour ago






  • 1




    clarified, also invalid input
    – LambdaBeta
    1 hour ago










  • what should be the outcome in case of an invalid input? or are they mentioned so they won't have to be taken care of?
    – Uriel
    1 hour ago











  • The outcome is much like undefined behaviour in C, don't worry about it, anything goes.
    – LambdaBeta
    1 hour ago










  • This is a nasty little challenge: nice job!
    – seadoggie01
    1 hour ago












up vote
4
down vote

favorite









up vote
4
down vote

favorite











ASCII art is fun. Modern text editors are very good at manipulating text. Are modern programming languages up to the task?



One common task in ASCII art manipulation is to crop text to a rectangle between two characters. This is the task you must implement in this challenge.



Details



Your program will take 3 inputs:



  • the first is the 'start' character of the block - marking the top-left corner

  • the second is the 'end' character of the block - marking the bottom-right corner

  • the third is some form of multiline text, either a string, or list of strings, or filename, or whatever

The result will be multiline text cropped to the rectangle between the given inputs. Note that the first two inputs may not be unique.



Edge cases



Boxes must always have volume at least 2. Thus these:



() (
)


are boxes but these:



)( ) (
( )


are not (with start=( and end=)).



The input will only contain one box. Thus the start and end characters must only occur once, unless they are the same character in which case they must occur exactly twice.



Additionally each line in the input must be at least as long as the distance from the start of a line to the right edge of the box in the input.



Rules



Typical code-golf rules apply. Shortest code wins.



Examples



Sunny-day: start: ( end: ) input:



This is some text
(but this text
is in a box )
So only it is important.


Output:



(but this text
is in a box )


Note the stripping of horizontal whitespace as well. ASCII art crops are 2d.



Rainy-day: start: ( end: ) input:



This is some text (
But is that even )
really a box?


Output:



(
)


Same start/end: start: / end: / input:



Oh, I get how this could be useful
/----------------------------
| All this text is in a box! |
----------------------------/


Output:



/----------------------------
| All this text is in a box! |
----------------------------/


Invalid input: `start: ( end: ) input:



Boxes are rectangular ( so this has
0 volume ) which is illegal.


Invalid input 2: `start: ( end: ) input:



(The lines must already be square 
so this line that is too short
relative to this end, is illegal)









share|improve this question















ASCII art is fun. Modern text editors are very good at manipulating text. Are modern programming languages up to the task?



One common task in ASCII art manipulation is to crop text to a rectangle between two characters. This is the task you must implement in this challenge.



Details



Your program will take 3 inputs:



  • the first is the 'start' character of the block - marking the top-left corner

  • the second is the 'end' character of the block - marking the bottom-right corner

  • the third is some form of multiline text, either a string, or list of strings, or filename, or whatever

The result will be multiline text cropped to the rectangle between the given inputs. Note that the first two inputs may not be unique.



Edge cases



Boxes must always have volume at least 2. Thus these:



() (
)


are boxes but these:



)( ) (
( )


are not (with start=( and end=)).



The input will only contain one box. Thus the start and end characters must only occur once, unless they are the same character in which case they must occur exactly twice.



Additionally each line in the input must be at least as long as the distance from the start of a line to the right edge of the box in the input.



Rules



Typical code-golf rules apply. Shortest code wins.



Examples



Sunny-day: start: ( end: ) input:



This is some text
(but this text
is in a box )
So only it is important.


Output:



(but this text
is in a box )


Note the stripping of horizontal whitespace as well. ASCII art crops are 2d.



Rainy-day: start: ( end: ) input:



This is some text (
But is that even )
really a box?


Output:



(
)


Same start/end: start: / end: / input:



Oh, I get how this could be useful
/----------------------------
| All this text is in a box! |
----------------------------/


Output:



/----------------------------
| All this text is in a box! |
----------------------------/


Invalid input: `start: ( end: ) input:



Boxes are rectangular ( so this has
0 volume ) which is illegal.


Invalid input 2: `start: ( end: ) input:



(The lines must already be square 
so this line that is too short
relative to this end, is illegal)






code-golf ascii-art






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 1 hour ago

























asked 1 hour ago









LambdaBeta

1,544412




1,544412











  • What about a valid box with a line outside that is shorter than the box?
    – seadoggie01
    1 hour ago






  • 1




    clarified, also invalid input
    – LambdaBeta
    1 hour ago










  • what should be the outcome in case of an invalid input? or are they mentioned so they won't have to be taken care of?
    – Uriel
    1 hour ago











  • The outcome is much like undefined behaviour in C, don't worry about it, anything goes.
    – LambdaBeta
    1 hour ago










  • This is a nasty little challenge: nice job!
    – seadoggie01
    1 hour ago
















  • What about a valid box with a line outside that is shorter than the box?
    – seadoggie01
    1 hour ago






  • 1




    clarified, also invalid input
    – LambdaBeta
    1 hour ago










  • what should be the outcome in case of an invalid input? or are they mentioned so they won't have to be taken care of?
    – Uriel
    1 hour ago











  • The outcome is much like undefined behaviour in C, don't worry about it, anything goes.
    – LambdaBeta
    1 hour ago










  • This is a nasty little challenge: nice job!
    – seadoggie01
    1 hour ago















What about a valid box with a line outside that is shorter than the box?
– seadoggie01
1 hour ago




What about a valid box with a line outside that is shorter than the box?
– seadoggie01
1 hour ago




1




1




clarified, also invalid input
– LambdaBeta
1 hour ago




clarified, also invalid input
– LambdaBeta
1 hour ago












what should be the outcome in case of an invalid input? or are they mentioned so they won't have to be taken care of?
– Uriel
1 hour ago





what should be the outcome in case of an invalid input? or are they mentioned so they won't have to be taken care of?
– Uriel
1 hour ago













The outcome is much like undefined behaviour in C, don't worry about it, anything goes.
– LambdaBeta
1 hour ago




The outcome is much like undefined behaviour in C, don't worry about it, anything goes.
– LambdaBeta
1 hour ago












This is a nasty little challenge: nice job!
– seadoggie01
1 hour ago




This is a nasty little challenge: nice job!
– seadoggie01
1 hour ago










4 Answers
4






active

oldest

votes

















up vote
4
down vote













Vim, 16, 12 bytes/keystrokes



#<C-v>Nj*yggVGp


Try it online! in the V interpreter




Modern text editors are very good at manipulating text. Are modern programming languages up to the task?




I bet old text editors are even better! :D



Even though it doesn't necessarily have to, this answer does work with both of the given "invalid" inputs, outputting



 rectangular (
) which is ill


and



(The lines must already be square
so this line that is too short
relative to this end, is illegal)





share|improve this answer






















  • Incidentally this is exactly the use-case I was doing to prompt me to write this challenge. I had my q macro as //<cr><c-v>nygv$o0dp or something like that for far too long :)
    – LambdaBeta
    1 hour ago

















up vote
1
down vote














APL (Dyalog), 38 30 bytes



4 bytes saved thanks to @EriktheOutgolfer





(1-⍨w-⍨⊃⍸⎕=s)↑(w←∊⊃⌽⍸⎕=s)↑s←↑⎕


Try it online!






share|improve this answer





























    up vote
    0
    down vote














    Canvas, 37 bytes



    {³⁴⁰;x≡‽┐
    X⁸)J╵⁶;┤ω┤⁵X⁶⁸⁰K├;┐┤└∔┘┘∔;@


    Try it here!



    36 bytes for getting the coordinates of the characters (and converting them to x,y,w,h because that's what ï¼  takes) and 1 byte for getting the subsection.. There must be a better approach






    share|improve this answer



























      up vote
      0
      down vote














      Jelly, 17 bytes



      ỴœẹⱮẎQr/Ṛṭþ/œịỴ{Y


      Try it online!





      share




















        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',
        convertImagesToLinks: false,
        noModals: false,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: null,
        bindNavPrevention: true,
        postfix: "",
        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%2f172132%2fcrop-ascii-art-challenge%23new-answer', 'question_page');

        );

        Post as a guest






























        4 Answers
        4






        active

        oldest

        votes








        4 Answers
        4






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes








        up vote
        4
        down vote













        Vim, 16, 12 bytes/keystrokes



        #<C-v>Nj*yggVGp


        Try it online! in the V interpreter




        Modern text editors are very good at manipulating text. Are modern programming languages up to the task?




        I bet old text editors are even better! :D



        Even though it doesn't necessarily have to, this answer does work with both of the given "invalid" inputs, outputting



         rectangular (
        ) which is ill


        and



        (The lines must already be square
        so this line that is too short
        relative to this end, is illegal)





        share|improve this answer






















        • Incidentally this is exactly the use-case I was doing to prompt me to write this challenge. I had my q macro as //<cr><c-v>nygv$o0dp or something like that for far too long :)
          – LambdaBeta
          1 hour ago














        up vote
        4
        down vote













        Vim, 16, 12 bytes/keystrokes



        #<C-v>Nj*yggVGp


        Try it online! in the V interpreter




        Modern text editors are very good at manipulating text. Are modern programming languages up to the task?




        I bet old text editors are even better! :D



        Even though it doesn't necessarily have to, this answer does work with both of the given "invalid" inputs, outputting



         rectangular (
        ) which is ill


        and



        (The lines must already be square
        so this line that is too short
        relative to this end, is illegal)





        share|improve this answer






















        • Incidentally this is exactly the use-case I was doing to prompt me to write this challenge. I had my q macro as //<cr><c-v>nygv$o0dp or something like that for far too long :)
          – LambdaBeta
          1 hour ago












        up vote
        4
        down vote










        up vote
        4
        down vote









        Vim, 16, 12 bytes/keystrokes



        #<C-v>Nj*yggVGp


        Try it online! in the V interpreter




        Modern text editors are very good at manipulating text. Are modern programming languages up to the task?




        I bet old text editors are even better! :D



        Even though it doesn't necessarily have to, this answer does work with both of the given "invalid" inputs, outputting



         rectangular (
        ) which is ill


        and



        (The lines must already be square
        so this line that is too short
        relative to this end, is illegal)





        share|improve this answer














        Vim, 16, 12 bytes/keystrokes



        #<C-v>Nj*yggVGp


        Try it online! in the V interpreter




        Modern text editors are very good at manipulating text. Are modern programming languages up to the task?




        I bet old text editors are even better! :D



        Even though it doesn't necessarily have to, this answer does work with both of the given "invalid" inputs, outputting



         rectangular (
        ) which is ill


        and



        (The lines must already be square
        so this line that is too short
        relative to this end, is illegal)






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited 57 mins ago

























        answered 1 hour ago









        DJMcMayhem♦

        40k11140304




        40k11140304











        • Incidentally this is exactly the use-case I was doing to prompt me to write this challenge. I had my q macro as //<cr><c-v>nygv$o0dp or something like that for far too long :)
          – LambdaBeta
          1 hour ago
















        • Incidentally this is exactly the use-case I was doing to prompt me to write this challenge. I had my q macro as //<cr><c-v>nygv$o0dp or something like that for far too long :)
          – LambdaBeta
          1 hour ago















        Incidentally this is exactly the use-case I was doing to prompt me to write this challenge. I had my q macro as //<cr><c-v>nygv$o0dp or something like that for far too long :)
        – LambdaBeta
        1 hour ago




        Incidentally this is exactly the use-case I was doing to prompt me to write this challenge. I had my q macro as //<cr><c-v>nygv$o0dp or something like that for far too long :)
        – LambdaBeta
        1 hour ago










        up vote
        1
        down vote














        APL (Dyalog), 38 30 bytes



        4 bytes saved thanks to @EriktheOutgolfer





        (1-⍨w-⍨⊃⍸⎕=s)↑(w←∊⊃⌽⍸⎕=s)↑s←↑⎕


        Try it online!






        share|improve this answer


























          up vote
          1
          down vote














          APL (Dyalog), 38 30 bytes



          4 bytes saved thanks to @EriktheOutgolfer





          (1-⍨w-⍨⊃⍸⎕=s)↑(w←∊⊃⌽⍸⎕=s)↑s←↑⎕


          Try it online!






          share|improve this answer
























            up vote
            1
            down vote










            up vote
            1
            down vote










            APL (Dyalog), 38 30 bytes



            4 bytes saved thanks to @EriktheOutgolfer





            (1-⍨w-⍨⊃⍸⎕=s)↑(w←∊⊃⌽⍸⎕=s)↑s←↑⎕


            Try it online!






            share|improve this answer















            APL (Dyalog), 38 30 bytes



            4 bytes saved thanks to @EriktheOutgolfer





            (1-⍨w-⍨⊃⍸⎕=s)↑(w←∊⊃⌽⍸⎕=s)↑s←↑⎕


            Try it online!







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited 30 mins ago

























            answered 35 mins ago









            Uriel

            11.5k4936




            11.5k4936




















                up vote
                0
                down vote














                Canvas, 37 bytes



                {³⁴⁰;x≡‽┐
                X⁸)J╵⁶;┤ω┤⁵X⁶⁸⁰K├;┐┤└∔┘┘∔;@


                Try it here!



                36 bytes for getting the coordinates of the characters (and converting them to x,y,w,h because that's what ï¼  takes) and 1 byte for getting the subsection.. There must be a better approach






                share|improve this answer
























                  up vote
                  0
                  down vote














                  Canvas, 37 bytes



                  {³⁴⁰;x≡‽┐
                  X⁸)J╵⁶;┤ω┤⁵X⁶⁸⁰K├;┐┤└∔┘┘∔;@


                  Try it here!



                  36 bytes for getting the coordinates of the characters (and converting them to x,y,w,h because that's what ï¼  takes) and 1 byte for getting the subsection.. There must be a better approach






                  share|improve this answer






















                    up vote
                    0
                    down vote










                    up vote
                    0
                    down vote










                    Canvas, 37 bytes



                    {³⁴⁰;x≡‽┐
                    X⁸)J╵⁶;┤ω┤⁵X⁶⁸⁰K├;┐┤└∔┘┘∔;@


                    Try it here!



                    36 bytes for getting the coordinates of the characters (and converting them to x,y,w,h because that's what ï¼  takes) and 1 byte for getting the subsection.. There must be a better approach






                    share|improve this answer













                    Canvas, 37 bytes



                    {³⁴⁰;x≡‽┐
                    X⁸)J╵⁶;┤ω┤⁵X⁶⁸⁰K├;┐┤└∔┘┘∔;@


                    Try it here!



                    36 bytes for getting the coordinates of the characters (and converting them to x,y,w,h because that's what ï¼  takes) and 1 byte for getting the subsection.. There must be a better approach







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered 47 mins ago









                    dzaima

                    13k21652




                    13k21652




















                        up vote
                        0
                        down vote














                        Jelly, 17 bytes



                        ỴœẹⱮẎQr/Ṛṭþ/œịỴ{Y


                        Try it online!





                        share
























                          up vote
                          0
                          down vote














                          Jelly, 17 bytes



                          ỴœẹⱮẎQr/Ṛṭþ/œịỴ{Y


                          Try it online!





                          share






















                            up vote
                            0
                            down vote










                            up vote
                            0
                            down vote










                            Jelly, 17 bytes



                            ỴœẹⱮẎQr/Ṛṭþ/œịỴ{Y


                            Try it online!





                            share













                            Jelly, 17 bytes



                            ỴœẹⱮẎQr/Ṛṭþ/œịỴ{Y


                            Try it online!






                            share











                            share


                            share










                            answered 5 mins ago









                            Erik the Outgolfer

                            29.5k42698




                            29.5k42698



























                                 

                                draft saved


                                draft discarded















































                                 


                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function ()
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f172132%2fcrop-ascii-art-challenge%23new-answer', 'question_page');

                                );

                                Post as a guest













































































                                Comments

                                Popular posts from this blog

                                Long meetings (6-7 hours a day): Being “babysat” by supervisor

                                Is the Concept of Multiple Fantasy Races Scientifically Flawed? [closed]

                                Confectionery