Chop up my ascii art
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
So, 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 5 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: 16
- Width: 5
- Height: 7
Result:
,=.
-|=|-
| |
) )
/ /
( (
DB icon
- X: 2
- Y: 12
- Width: 6
- Height: 4
Output:
,-""-.
i-..-i
| |
`-..-'
Alien Elder
- X: 9
- Y: 1
- Width: 7
- Height: 10
(Formatting issues prevent me from displaying the result here. It's the joystick itself.)
Signature
- X: 16
- Y: 19
- Width: 3
Height: 1
hjw
Rules
- Output can be given by any convenient method.
- Either a full program or a function are acceptable.
Standard loopholes are forbidden.- This is code-golf so all usual golfing rules apply, and the shortest code (in bytes) wins.
code-golf ascii-art
add a comment |Â
up vote
1
down vote
favorite
So, 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 5 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: 16
- Width: 5
- Height: 7
Result:
,=.
-|=|-
| |
) )
/ /
( (
DB icon
- X: 2
- Y: 12
- Width: 6
- Height: 4
Output:
,-""-.
i-..-i
| |
`-..-'
Alien Elder
- X: 9
- Y: 1
- Width: 7
- Height: 10
(Formatting issues prevent me from displaying the result here. It's the joystick itself.)
Signature
- X: 16
- Y: 19
- Width: 3
Height: 1
hjw
Rules
- Output can be given by any convenient method.
- Either a full program or a function are acceptable.
Standard loopholes are forbidden.- This is code-golf so all usual golfing rules apply, and the shortest code (in bytes) wins.
code-golf ascii-art
3
Can we take X and Y as 0-indexed?
– TFeld
1 hour ago
3
Also, in testcase 1,Y
should bew15
, and in TC 2,X
should be3
– TFeld
57 mins ago
Can we assume the input is padded to a rectangle with spaces?
– DLosc
50 mins ago
Can you add a test case where the original art doesn't use both single and double quotes, please? Also, I see no good reason to enforce 1-indexing here.
– Shaggy
22 mins ago
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
So, 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 5 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: 16
- Width: 5
- Height: 7
Result:
,=.
-|=|-
| |
) )
/ /
( (
DB icon
- X: 2
- Y: 12
- Width: 6
- Height: 4
Output:
,-""-.
i-..-i
| |
`-..-'
Alien Elder
- X: 9
- Y: 1
- Width: 7
- Height: 10
(Formatting issues prevent me from displaying the result here. It's the joystick itself.)
Signature
- X: 16
- Y: 19
- Width: 3
Height: 1
hjw
Rules
- Output can be given by any convenient method.
- Either a full program or a function are acceptable.
Standard loopholes are forbidden.- This is code-golf so all usual golfing rules apply, and the shortest code (in bytes) wins.
code-golf ascii-art
So, 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 5 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: 16
- Width: 5
- Height: 7
Result:
,=.
-|=|-
| |
) )
/ /
( (
DB icon
- X: 2
- Y: 12
- Width: 6
- Height: 4
Output:
,-""-.
i-..-i
| |
`-..-'
Alien Elder
- X: 9
- Y: 1
- Width: 7
- Height: 10
(Formatting issues prevent me from displaying the result here. It's the joystick itself.)
Signature
- X: 16
- Y: 19
- Width: 3
Height: 1
hjw
Rules
- Output can be given by any convenient method.
- Either a full program or a function are acceptable.
Standard loopholes are forbidden.- This is code-golf so all usual golfing rules apply, and the shortest code (in bytes) wins.
code-golf ascii-art
code-golf ascii-art
asked 1 hour ago
AJFaraday
3,06842751
3,06842751
3
Can we take X and Y as 0-indexed?
– TFeld
1 hour ago
3
Also, in testcase 1,Y
should bew15
, and in TC 2,X
should be3
– TFeld
57 mins ago
Can we assume the input is padded to a rectangle with spaces?
– DLosc
50 mins ago
Can you add a test case where the original art doesn't use both single and double quotes, please? Also, I see no good reason to enforce 1-indexing here.
– Shaggy
22 mins ago
add a comment |Â
3
Can we take X and Y as 0-indexed?
– TFeld
1 hour ago
3
Also, in testcase 1,Y
should bew15
, and in TC 2,X
should be3
– TFeld
57 mins ago
Can we assume the input is padded to a rectangle with spaces?
– DLosc
50 mins ago
Can you add a test case where the original art doesn't use both single and double quotes, please? Also, I see no good reason to enforce 1-indexing here.
– Shaggy
22 mins ago
3
3
Can we take X and Y as 0-indexed?
– TFeld
1 hour ago
Can we take X and Y as 0-indexed?
– TFeld
1 hour ago
3
3
Also, in testcase 1,
Y
should bew 15
, and in TC 2, X
should be 3
– TFeld
57 mins ago
Also, in testcase 1,
Y
should bew 15
, and in TC 2, X
should be 3
– TFeld
57 mins ago
Can we assume the input is padded to a rectangle with spaces?
– DLosc
50 mins ago
Can we assume the input is padded to a rectangle with spaces?
– DLosc
50 mins ago
Can you add a test case where the original art doesn't use both single and double quotes, please? Also, I see no good reason to enforce 1-indexing here.
– Shaggy
22 mins ago
Can you add a test case where the original art doesn't use both single and double quotes, please? Also, I see no good reason to enforce 1-indexing here.
– Shaggy
22 mins ago
add a comment |Â
5 Answers
5
active
oldest
votes
up vote
2
down vote
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!
add a comment |Â
up vote
1
down vote
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
add a comment |Â
up vote
1
down vote
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!
add a comment |Â
up vote
0
down vote
Japt -R
, 13 bytes
Can save 1 byte if we can use 0-indexing.
·sYÉV+Y ®tXÉW
Try it
Explanation
:Implicit input of string U and integers V=Height, W=Width, X & Y
· :Split U on newlines
s :Slice
YÉ : From index Y-1
V+Y : To index V+Y
® :Map
t : Substring
XÉ : From index X-1
W : To length W
add a comment |Â
up vote
0
down vote
JavaScript (Node.js), 70 bytes
(a,x,y,w,h)=>a.split`
`.map(q=>q.substr(x-1,w)).slice(--y,y+h).join`
`
Try it online!
The ASCII art given is annoying tbh
add a comment |Â
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
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!
add a comment |Â
up vote
2
down vote
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!
add a comment |Â
up vote
2
down vote
up vote
2
down vote
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!
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!
answered 56 mins ago


TFeld
12.7k2836
12.7k2836
add a comment |Â
add a comment |Â
up vote
1
down vote
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
add a comment |Â
up vote
1
down vote
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
add a comment |Â
up vote
1
down vote
up vote
1
down vote
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
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
answered 29 mins ago


DLosc
18.6k33588
18.6k33588
add a comment |Â
add a comment |Â
up vote
1
down vote
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!
add a comment |Â
up vote
1
down vote
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!
add a comment |Â
up vote
1
down vote
up vote
1
down vote
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!
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!
edited 22 mins ago
answered 47 mins ago


Emigna
44k431133
44k431133
add a comment |Â
add a comment |Â
up vote
0
down vote
Japt -R
, 13 bytes
Can save 1 byte if we can use 0-indexing.
·sYÉV+Y ®tXÉW
Try it
Explanation
:Implicit input of string U and integers V=Height, W=Width, X & Y
· :Split U on newlines
s :Slice
YÉ : From index Y-1
V+Y : To index V+Y
® :Map
t : Substring
XÉ : From index X-1
W : To length W
add a comment |Â
up vote
0
down vote
Japt -R
, 13 bytes
Can save 1 byte if we can use 0-indexing.
·sYÉV+Y ®tXÉW
Try it
Explanation
:Implicit input of string U and integers V=Height, W=Width, X & Y
· :Split U on newlines
s :Slice
YÉ : From index Y-1
V+Y : To index V+Y
® :Map
t : Substring
XÉ : From index X-1
W : To length W
add a comment |Â
up vote
0
down vote
up vote
0
down vote
Japt -R
, 13 bytes
Can save 1 byte if we can use 0-indexing.
·sYÉV+Y ®tXÉW
Try it
Explanation
:Implicit input of string U and integers V=Height, W=Width, X & Y
· :Split U on newlines
s :Slice
YÉ : From index Y-1
V+Y : To index V+Y
® :Map
t : Substring
XÉ : From index X-1
W : To length W
Japt -R
, 13 bytes
Can save 1 byte if we can use 0-indexing.
·sYÉV+Y ®tXÉW
Try it
Explanation
:Implicit input of string U and integers V=Height, W=Width, X & Y
· :Split U on newlines
s :Slice
YÉ : From index Y-1
V+Y : To index V+Y
® :Map
t : Substring
XÉ : From index X-1
W : To length W
answered 6 mins ago


Shaggy
17.4k21663
17.4k21663
add a comment |Â
add a comment |Â
up vote
0
down vote
JavaScript (Node.js), 70 bytes
(a,x,y,w,h)=>a.split`
`.map(q=>q.substr(x-1,w)).slice(--y,y+h).join`
`
Try it online!
The ASCII art given is annoying tbh
add a comment |Â
up vote
0
down vote
JavaScript (Node.js), 70 bytes
(a,x,y,w,h)=>a.split`
`.map(q=>q.substr(x-1,w)).slice(--y,y+h).join`
`
Try it online!
The ASCII art given is annoying tbh
add a comment |Â
up vote
0
down vote
up vote
0
down vote
JavaScript (Node.js), 70 bytes
(a,x,y,w,h)=>a.split`
`.map(q=>q.substr(x-1,w)).slice(--y,y+h).join`
`
Try it online!
The ASCII art given is annoying tbh
JavaScript (Node.js), 70 bytes
(a,x,y,w,h)=>a.split`
`.map(q=>q.substr(x-1,w)).slice(--y,y+h).join`
`
Try it online!
The ASCII art given is annoying tbh
answered 10 secs ago
Shieru Asakoto
2,060313
2,060313
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f174748%2fchop-up-my-ascii-art%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
3
Can we take X and Y as 0-indexed?
– TFeld
1 hour ago
3
Also, in testcase 1,
Y
should bew15
, and in TC 2,X
should be3
– TFeld
57 mins ago
Can we assume the input is padded to a rectangle with spaces?
– DLosc
50 mins ago
Can you add a test case where the original art doesn't use both single and double quotes, please? Also, I see no good reason to enforce 1-indexing here.
– Shaggy
22 mins ago