# Chop up my ASCII art

Let's say I've got some ASCII art:

          ___
,"---".
:     ;
-.-'
| |
| |
| |
_.-\_/-._
_ / |     | \ _
/ /   ---'   \ \
/  -----------'  \
/,-""-.       ,-""-.\
( i-..-i       i-..-i )
||    |-------|    |'|
\ -..-'  ,=.  -..-'/
--------|=|-------'
| |
\ \
) ) hjw
/ /
( (


(Source)

But I want to focus on the cable of this joystick, because I actually want a picture of a garden path, leading up to a door.

 ,=.
-|=|-
| |
\ \
) )
/ /
( (


I could copy out line after line, I could use a text editor with block selection mode, or... I could write some code!

So, my code needs five arguments:

• A piece of ASCII art, a newline separated string.
• The X axis of the top-left corner (1-indexed, from left hand column, positive integer)
• The Y axis of the top-left corner (1-indexed, from top row, positive integer)
• Width of the resultant image (positive integer)
• Height of the resultant image (positive integer)

## Test Cases

ASCII image:

          ___
,"---".
:     ;
-.-'
| |
| |
| |
_.-\_/-._
_ / |     | \ _
/ /   ---'   \ \
/  -----------'  \
/,-""-.       ,-""-.\
( i-..-i       i-..-i )
||    |-------|    |'|
\ -..-'  ,=.  -..-'/
--------|=|-------'
| |
\ \
) ) hjw
/ /
( (


### Garden path

• X: 10
• Y: 15
• Width: 5
• Height: 7

Result:

 ,=.
-|=|-
| |
\ \
) )
/ /
( (


### DB icon

• X: 3
• Y: 12
• Width: 6
• Height: 4

Output:

,-""-.
i-..-i
|    |
-..-'


### Alien Elder

• X: 9
• Y: 1
• Width: 7
• Height: 10
  ___
,"---".
:     ;
-.-'
| |
| |
| |
.-\_/-.
|     |
---'

### Signature

• X: 16
• Y: 19
• Width: 3
• Height: 1
hjw

## Rules

• Can we take X and Y as 0-indexed? – TFeld Oct 26 '18 at 9:12
• Also, in testcase 1, Y should bew 15, and in TC 2, X should be 3 – TFeld Oct 26 '18 at 9:15
• Can we assume the input is padded to a rectangle with spaces? – DLosc Oct 26 '18 at 9:23
• That's exactly what I thought of when I saw that ascii art, a garden path. I didn't think of anything else at all... – Jo King Oct 26 '18 at 10:27
• It seems many of the X and Y values are incorrect in the test cases (maybe some are 0 indexed?!) - at least: Y of Garden Path should be 15; X of DB Icon should be 3. – Jonathan Allan Oct 26 '18 at 10:44

# Canvas, 1 byte

＠


Try it here!

It takes height, width, Y, X, and ASCII art as inputs, in that order.

Though almost everything about the ASCII art objects in Canvas is 0-indexed, ＠ is 1-indexed by some reason. ¯\_(ツ)_/¯

• Expected answer from canvas :D (mb charcoal have something similar) – Dead Possum Oct 26 '18 at 11:44
• What sorcery is this! – AJFaraday Oct 29 '18 at 9:18
• @AJFaraday I mean, wouldn't you expect that a language specifically made for ASCII-art manipulation to not have a built-in to get a specific part of an ascii-art? :p – dzaima Oct 29 '18 at 9:36

# Python 2, 64 62 bytes

lambda I,x,y,w,h:[l[x-1:][:w]for l in I.split('\n')[y-1:][:h]]


Try it online!

If X and Y can be 0-indexed:

# Python 2, 56 bytes

lambda I,x,y,w,h:[l[x:x+w]for l in I.split('\n')[y:y+h]]


Try it online!

# Charcoal, 7 bytes

δＪθηＴζε


Try it online! Link is to verbose version of code. Takes 0-indexed coordintaes. Explanation:

δ


Print the ASCII art.

Ｊθη


Ｔζε


Trim the canvas to the desired size.

# Pip-n, 21 bytes

_@>--b@<dM(a^n--c+,e)


Try it online!

Or, if 0-indexing is allowed...

## Pip -n, 17 bytes:

_@>b@<dM(a^nc+,e)


Try it online!

### Explanation

Takes all five arguments as command-line args.

                   a-e are the 5 cmdline args; n is newline
a^n       Split a on newlines
,e   Range(e)
c+     Add c to each element
(       )  Use the resulting range(c,c+e) to slice into the list of lines
M           To each line, map this function:
_                   The line
@>b                Slice, keeping indices b and greater
@<d             Slice, keeping indices less than d
Print, joining on newlines (-n flag)


The above solutions also assume the input is a perfect rectangle--i.e., the lines are all the same length. Otherwise, 2 extra bytes are needed (use -l instead of -n):

(Z(a^nZDsb+,d)c+,e)      0-indexed, 19 bytes
(Z(a^nZDs--b+,d)--c+,e)  1-indexed, 23 bytes


# 05AB1E, 21 19 bytes

UV¶¡εXG¦}Y£}sG¦}s£»


Try it online!

Or if 0-indexing is allowed:

# 05AB1E, 18 bytes

UV¶¡εX.$Y£}sF¦}s£»  Try it online! • 12 if you can take in [X1, X2] and [Y1, Y2] as v³ŸNåiy²ŸèJ= – Magic Octopus Urn Oct 26 '18 at 13:14 # Japt-R, 13 11 bytes ·tWÉY ®tVÉX  Try it 2 bytes saved thanks to Kamil Drakari 9 bytes if not for the unnecessary requirement that solutions use 1-based indexing. ·tWY ®tVX  ## Explanation  :Implicit input of string U and integers V=X, W=Y, X=Width & Y=Height · :Split U on newlines t :Slice WÉ : From 0-based index W-1 Y : To length Y ® :Map t : Substring VÉ : From 0-based index V-1 X : To length X :Implicitly join with newlines and output  • In my testing, you can save 2 bytes by switching the slice to t, and another 2 bytes by 0 indexing. It might be worth waiting on the second one since OP hasn't clarified whether 0 indexing is acceptable, or fixed the test cases. – Kamil Drakari Oct 26 '18 at 14:40 • Nice one, @KamilDrakari - completely forgot the t method works on arrays, too. – Shaggy Oct 26 '18 at 16:44 # JavaScript (Node.js), 70 69 bytes -1 Thanks @Shaggy for the splice() idea. (a,x,y,w,h)=>a.split .map(q=>q.substr(x-1,w)).splice(--y,h).join   Try it online! The ASCII art given is annoying tbh • 69 bytes – Shaggy Oct 26 '18 at 16:52 # J, 45 bytes f=:4 :0 'a b c d'=.x-#:10 y{~<(a+i.b);c+i.d )  Try it online! Notes: J accepts up to 2 arguments, left and right. The right argument is the ASCII art string, the left one - a list of U H X W values I simulate the newline and the function call has a code to convert the string to an array. # V, 16 bytes ÀGÀ|<C-v>@cl@djyHVGp  Try it online! Input is expected to be padded with spaces to be a perfect rectangle. Also, the 'Y' input comes before the 'X'. Hexdump: 00000000: c047 c07c 1640 636c 4064 6a79 4856 4770 .G.|.@cl@djyHVGp  # C (gcc), 118 109 bytes The input can contain jagged lines: newlines are printed if they exist in the y-range. Thanks to ceilingcat for the suggestion. f(s,x,y,w,h,a,b,c)char*s;{for(b=1,a=0;*s;a=c?!b++:a,s++)((c=*s==10)|++a>=x&a-x<w)&b>=y&b-y<h-c&&putchar(*s);}  Try it online! # Jelly, 11 bytes Ỵṫ€¥ṫḣ€ḣ4ƭ/  Try it online! Takes the ASCII-art as a Python multi-line r-string (so as to avoid problems with backslashes). # Haskell, 61 bytes (x#y)w h=unlines.map(take w.drop(x-1)).take h.drop(y-1).lines  Try it online! Takes arguments in order: x, y, w, h, s. # Bash + coreutils, 37 cut -b$1-$[$1+$3-1]|tail +$2|head -$4  Interestingly, GNU tail 8.28 (on Ubuntu 18.04) allows +NUM format, whereas GNU tail 8.29 on TIO requires 2 extra bytes for this -n+NUM. Try it online! I thought doing this all in sed might be shorter, but at 50 bytes its not: sed -nr "$2,$[$2+$4-1]s/.{$[$1-1]}(.{,$3}).*/\1/p"


# K4, 31 bytes

Solution:

{[a;x;y;w;h]a .-1+(y;x)+!:'h,w}


Examples:

q)ASCII:("          ___";"        ,\"---\".";"        :     ;";"         -.-'";"          | |";"          | |";"          | |";"       _.-\\_/-._";"    _ / |     | \\ _";"   / /   ---'   \\ \\";"  /  -----------'  \\";" /,-\"\"-.       ,-\"\"-.\\";"( i-..-i       i-..-i )";"||    |-------|    |'|";"\\ -..-'  ,=.  -..-'/";" --------|=|-------'";"          | |";"          \\ \\";"           ) ) hjw";"          / /";"         ( (")
q)k){[a;x;y;w;h]a .-1+(y;x)+!:'h,w}[ASCII;10;15;5;7]
" ,=. "
"-|=|-"
" | | "
" \\ \\ "
"  ) )"
" / / "
"( (  "
q)k){[a;x;y;w;h].[a;-1+(y;x)+!:'h,w]}[ASCII;3;12;6;4]
",-\"\"-."
"i-..-i"
"|    |"
"-..-'"
q)k){[a;x;y;w;h].[a;-1+(y;x)+!:'h,w]}[ASCII;9;1;7;10]
"  ___  "
",\"---\"."
":     ;"
" -.-' "
"  | |  "
"  | |  "
"  | |  "
".-\\_/-."
"|     |"
" ---' "
q)k){[a;x;y;w;h].[a;-1+(y;x)+!:'h,w]}[ASCII;16;19;3;1]
"hjw"


Explanation:

Take the 1-indexed input, generate the x/y coordinates and index into ascii art.

{[a;x;y;w;h]a .-1+(y;x)+!:'h,w} / the solution
{[a;x;y;w;h]                  } / lambda taking 5 inputs, a(scii), x, y, w(idth), h(eight)
h,w  / concatenate height and width, e.g. 7 5
!:'     / range til each, e.g. (0 1 2 3 4 5 6;0 1 2 3 4)
(y;x)+        / add list y x to it, e.g. (15 16 17 18 19 20 21;10 11 12 13 14)
-1+              / subtract 1, e.g. (14 15 16 17 18 19 20;9 10 11 12 13)
a .                 / index into a at these coordinates


# R, 62 bytes

Perhaps surprisingly short solution to a text challenge in R, because we don't need to actually read the whole text into a matrix.

function(x,y,w,h)write(substr(readLines()[y+1:h-1],x,x+w-1),1)


Try it online!

# \/\/>, 123 bytes

j:j8+}j+}j}pppp80x
{:}i:0(?v:a=?x@s1+:
~~~~1+0!/000y
yp:qqqq~
q{y?=p:+1q:}}g\$q:pp:
1}x}p}ap:q~q+1{{y!?:-
ys{{
~!u.*2(0:;!?h


Input consists of a single line of space-delimited input variables x, y, w, and h`, followed by the ascii art on the next line onward.

## Explanation

This code uses \/\/>'s ability to self-modify by placing the ascii art in the code itself, which ends up looking like this:

It then grabs the characters in the specified rectangle off of the code and outputs it.