Roll the carpet
$begingroup$
This question is inspired by Kevin Cruijssen's question.
Now that the carpet is laid out, we want to roll it. Your task is to write a program that takes a string and returns a spiral made from this string (representing a rolled carpet viewed from the side).
The procedure for one step of rolling the carpet is the following. There is an example to illustrate what I mean. Notice that the example starts with a partially rolled carpet for better understanding:
ac
rpet
- separate the "head" from the "tail" of the carpet: the head is what has been rolled so far, the tail is what remains to be rolled.
Head: ac Tail:
rp et
- Rotate the head 90°, clockwise.
Rotated head: ra Tail (unchanged):
pc et
- if the width of the new head (here
2
) is less or equal than the length of the tail (here2
)
- then, put it on top of the tail
- else, the carpet (as it was at the begining of the step) was rolled
New carpet: ra
pc
et
Repeat the procedure as many times as needed.
Two examples showing all steps of the carpet rolling:
carpet
c
arpet
ac
rpet
ra
pc
et
0123456789
0
123456789
10
23456789
21
30
456789
432
501
6789
Some precisions:
- You don't need to show all intermediate steps, only the rolled carpet (e.g. if you find a non-iterative way to compute the result, it's perfect). Also, you don't need to print any leading whitespace, in the examples above, I only show them to align stuff.
- Input is a String, a list/array of char
- Output is printed to stdout or to a file.
- Input is nice: the length is at least 1 char, and at most a constant sufficiently small so that it doesn't cause problems, but you can't use that constant in your program; the content of the string is only nice characters ([a-zA-Z0-9]), encoding at your preference.
- This is code-golf, so shortest answer in bytes wins. Don't let code-golf languages discourage you from posting answers with non-codegolfing languages. Try to come up with an as short as possible answer for 'any' programming language.
Default Loopholes are forbidden.- If possible, please add a link with a test for your code.
- Also, add an explanation for your answer if you think it is needed.
code-golf string
$endgroup$
|
show 2 more comments
$begingroup$
This question is inspired by Kevin Cruijssen's question.
Now that the carpet is laid out, we want to roll it. Your task is to write a program that takes a string and returns a spiral made from this string (representing a rolled carpet viewed from the side).
The procedure for one step of rolling the carpet is the following. There is an example to illustrate what I mean. Notice that the example starts with a partially rolled carpet for better understanding:
ac
rpet
- separate the "head" from the "tail" of the carpet: the head is what has been rolled so far, the tail is what remains to be rolled.
Head: ac Tail:
rp et
- Rotate the head 90°, clockwise.
Rotated head: ra Tail (unchanged):
pc et
- if the width of the new head (here
2
) is less or equal than the length of the tail (here2
)
- then, put it on top of the tail
- else, the carpet (as it was at the begining of the step) was rolled
New carpet: ra
pc
et
Repeat the procedure as many times as needed.
Two examples showing all steps of the carpet rolling:
carpet
c
arpet
ac
rpet
ra
pc
et
0123456789
0
123456789
10
23456789
21
30
456789
432
501
6789
Some precisions:
- You don't need to show all intermediate steps, only the rolled carpet (e.g. if you find a non-iterative way to compute the result, it's perfect). Also, you don't need to print any leading whitespace, in the examples above, I only show them to align stuff.
- Input is a String, a list/array of char
- Output is printed to stdout or to a file.
- Input is nice: the length is at least 1 char, and at most a constant sufficiently small so that it doesn't cause problems, but you can't use that constant in your program; the content of the string is only nice characters ([a-zA-Z0-9]), encoding at your preference.
- This is code-golf, so shortest answer in bytes wins. Don't let code-golf languages discourage you from posting answers with non-codegolfing languages. Try to come up with an as short as possible answer for 'any' programming language.
Default Loopholes are forbidden.- If possible, please add a link with a test for your code.
- Also, add an explanation for your answer if you think it is needed.
code-golf string
$endgroup$
3
$begingroup$
Closely related
$endgroup$
– Giuseppe
15 hours ago
2
$begingroup$
Also this one: codegolf.stackexchange.com/questions/125966/…, but none include the termination check.
$endgroup$
– Bromind
15 hours ago
3
$begingroup$
Suggested test case:ProgrammingPuzzlesAndCodeGolf
- the final tail length greater than 1 tripped me up.
$endgroup$
– Sok
14 hours ago
$begingroup$
Where is the head on the secondcarpet
example, where nothing is rolled up?
$endgroup$
– Embodiment of Ignorance
12 hours ago
1
$begingroup$
I think you've swapped the words "head" and "tail" here: "if the width of the new head [...] is greater or equal than the length of the tail [...]".
$endgroup$
– Erik the Outgolfer
7 hours ago
|
show 2 more comments
$begingroup$
This question is inspired by Kevin Cruijssen's question.
Now that the carpet is laid out, we want to roll it. Your task is to write a program that takes a string and returns a spiral made from this string (representing a rolled carpet viewed from the side).
The procedure for one step of rolling the carpet is the following. There is an example to illustrate what I mean. Notice that the example starts with a partially rolled carpet for better understanding:
ac
rpet
- separate the "head" from the "tail" of the carpet: the head is what has been rolled so far, the tail is what remains to be rolled.
Head: ac Tail:
rp et
- Rotate the head 90°, clockwise.
Rotated head: ra Tail (unchanged):
pc et
- if the width of the new head (here
2
) is less or equal than the length of the tail (here2
)
- then, put it on top of the tail
- else, the carpet (as it was at the begining of the step) was rolled
New carpet: ra
pc
et
Repeat the procedure as many times as needed.
Two examples showing all steps of the carpet rolling:
carpet
c
arpet
ac
rpet
ra
pc
et
0123456789
0
123456789
10
23456789
21
30
456789
432
501
6789
Some precisions:
- You don't need to show all intermediate steps, only the rolled carpet (e.g. if you find a non-iterative way to compute the result, it's perfect). Also, you don't need to print any leading whitespace, in the examples above, I only show them to align stuff.
- Input is a String, a list/array of char
- Output is printed to stdout or to a file.
- Input is nice: the length is at least 1 char, and at most a constant sufficiently small so that it doesn't cause problems, but you can't use that constant in your program; the content of the string is only nice characters ([a-zA-Z0-9]), encoding at your preference.
- This is code-golf, so shortest answer in bytes wins. Don't let code-golf languages discourage you from posting answers with non-codegolfing languages. Try to come up with an as short as possible answer for 'any' programming language.
Default Loopholes are forbidden.- If possible, please add a link with a test for your code.
- Also, add an explanation for your answer if you think it is needed.
code-golf string
$endgroup$
This question is inspired by Kevin Cruijssen's question.
Now that the carpet is laid out, we want to roll it. Your task is to write a program that takes a string and returns a spiral made from this string (representing a rolled carpet viewed from the side).
The procedure for one step of rolling the carpet is the following. There is an example to illustrate what I mean. Notice that the example starts with a partially rolled carpet for better understanding:
ac
rpet
- separate the "head" from the "tail" of the carpet: the head is what has been rolled so far, the tail is what remains to be rolled.
Head: ac Tail:
rp et
- Rotate the head 90°, clockwise.
Rotated head: ra Tail (unchanged):
pc et
- if the width of the new head (here
2
) is less or equal than the length of the tail (here2
)
- then, put it on top of the tail
- else, the carpet (as it was at the begining of the step) was rolled
New carpet: ra
pc
et
Repeat the procedure as many times as needed.
Two examples showing all steps of the carpet rolling:
carpet
c
arpet
ac
rpet
ra
pc
et
0123456789
0
123456789
10
23456789
21
30
456789
432
501
6789
Some precisions:
- You don't need to show all intermediate steps, only the rolled carpet (e.g. if you find a non-iterative way to compute the result, it's perfect). Also, you don't need to print any leading whitespace, in the examples above, I only show them to align stuff.
- Input is a String, a list/array of char
- Output is printed to stdout or to a file.
- Input is nice: the length is at least 1 char, and at most a constant sufficiently small so that it doesn't cause problems, but you can't use that constant in your program; the content of the string is only nice characters ([a-zA-Z0-9]), encoding at your preference.
- This is code-golf, so shortest answer in bytes wins. Don't let code-golf languages discourage you from posting answers with non-codegolfing languages. Try to come up with an as short as possible answer for 'any' programming language.
Default Loopholes are forbidden.- If possible, please add a link with a test for your code.
- Also, add an explanation for your answer if you think it is needed.
code-golf string
code-golf string
edited 6 hours ago
Bromind
asked 15 hours ago
BromindBromind
1866
1866
3
$begingroup$
Closely related
$endgroup$
– Giuseppe
15 hours ago
2
$begingroup$
Also this one: codegolf.stackexchange.com/questions/125966/…, but none include the termination check.
$endgroup$
– Bromind
15 hours ago
3
$begingroup$
Suggested test case:ProgrammingPuzzlesAndCodeGolf
- the final tail length greater than 1 tripped me up.
$endgroup$
– Sok
14 hours ago
$begingroup$
Where is the head on the secondcarpet
example, where nothing is rolled up?
$endgroup$
– Embodiment of Ignorance
12 hours ago
1
$begingroup$
I think you've swapped the words "head" and "tail" here: "if the width of the new head [...] is greater or equal than the length of the tail [...]".
$endgroup$
– Erik the Outgolfer
7 hours ago
|
show 2 more comments
3
$begingroup$
Closely related
$endgroup$
– Giuseppe
15 hours ago
2
$begingroup$
Also this one: codegolf.stackexchange.com/questions/125966/…, but none include the termination check.
$endgroup$
– Bromind
15 hours ago
3
$begingroup$
Suggested test case:ProgrammingPuzzlesAndCodeGolf
- the final tail length greater than 1 tripped me up.
$endgroup$
– Sok
14 hours ago
$begingroup$
Where is the head on the secondcarpet
example, where nothing is rolled up?
$endgroup$
– Embodiment of Ignorance
12 hours ago
1
$begingroup$
I think you've swapped the words "head" and "tail" here: "if the width of the new head [...] is greater or equal than the length of the tail [...]".
$endgroup$
– Erik the Outgolfer
7 hours ago
3
3
$begingroup$
Closely related
$endgroup$
– Giuseppe
15 hours ago
$begingroup$
Closely related
$endgroup$
– Giuseppe
15 hours ago
2
2
$begingroup$
Also this one: codegolf.stackexchange.com/questions/125966/…, but none include the termination check.
$endgroup$
– Bromind
15 hours ago
$begingroup$
Also this one: codegolf.stackexchange.com/questions/125966/…, but none include the termination check.
$endgroup$
– Bromind
15 hours ago
3
3
$begingroup$
Suggested test case:
ProgrammingPuzzlesAndCodeGolf
- the final tail length greater than 1 tripped me up.$endgroup$
– Sok
14 hours ago
$begingroup$
Suggested test case:
ProgrammingPuzzlesAndCodeGolf
- the final tail length greater than 1 tripped me up.$endgroup$
– Sok
14 hours ago
$begingroup$
Where is the head on the second
carpet
example, where nothing is rolled up?$endgroup$
– Embodiment of Ignorance
12 hours ago
$begingroup$
Where is the head on the second
carpet
example, where nothing is rolled up?$endgroup$
– Embodiment of Ignorance
12 hours ago
1
1
$begingroup$
I think you've swapped the words "head" and "tail" here: "if the width of the new head [...] is greater or equal than the length of the tail [...]".
$endgroup$
– Erik the Outgolfer
7 hours ago
$begingroup$
I think you've swapped the words "head" and "tail" here: "if the width of the new head [...] is greater or equal than the length of the tail [...]".
$endgroup$
– Erik the Outgolfer
7 hours ago
|
show 2 more comments
4 Answers
4
active
oldest
votes
$begingroup$
Pyth, 37 bytes
.U+j;bZ.WgleHJlhH,+_MChZ<eZJ>eZJ,]hQt
Try it online here, or verify all the test cases at once here.
.U+j;bZ.WgleHJlhH,+_MChZ<eZJ>eZJ,]hQtQ Implicit: Q=eval(input())
Trailing Q inferred
]hQ First character of Q, wrapped in an array
tQ All but the first character of Q
, 2-element array of the two previous results
This yields array with rolled carpet (as array of strings) followed by the tail
.W While condition function is truthy, execute inner function, with initial value of the above:
gleHJlhH Condition function, input H
JlhH Number of layers in the current rolled carpet, store in J
leH Lenth of the tail
g J Is the above greater than or equal to J?
,+_MChZ<eZJ>eZJ Inner function, input Z
_MChZ Rotate the current rolled carpet (transpose, then reverse each row)
+ <eZJ Append the first J characters of the tail as a new row
, Pair the above with...
>eZJ ... all but the first J characters of the tail - this is the new tail
.U+j;bZ Join the carpet roll on newlines and append the tail, implicit print
$endgroup$
add a comment |
$begingroup$
J, 73 bytes
[:(}:@[,{:@[,])&>/[:((|:@|.@[,#@[{.]);#@[}.])&>/^:(<:&#&>/)^:_,:@,:@{.;}.
Try it online!
I plan to golf this further after work tonight.
$endgroup$
add a comment |
$begingroup$
R, 146 132 bytes
function(s){m=F[F]
while({m=rbind(t(m)[,F:0],s[1:F])
s=s[-1:-F]
length(s)>sum(F<-dim(m))})0
write(m[F:1,],1,F[1],,"")
cat(s,sep="")}
Try it online!
Implements the carpet-rolling procedure. Takes input as a list of characters and prints to stdout.
Saved 14 bytes by finding a way to use a do-while
loop and initializing using F
.
function(s){
m=F[F] # logical(0); create an empty array (this gets automatically promoted to character(0) later
while( # do-while loop
{m=rbind(t(m)[,F:0],s[1:F]) # rotate m counterclockwise and add the first F characters of s to the bottom
s=s[-1:-F] # remove those characters
length(s)>sum(F<-dim(m))})0 # while the number of characters remaining is greater than the sum of m's dimensions
write(m[F:1,],1,F[1],,"") # write the rolled portion write writes down the columns, we reverse each column
cat(s,sep="") # and write the remaining characters
}
$endgroup$
add a comment |
$begingroup$
Jelly, 30 bytes
Seems overly long...
ḢW,ðZU;Ls@¥©ḢWɗ,®Ẏ¤ð/ẈṢƑ$¿ḢY;Ɗ
Try it online!
How?
ḢW,ðZU;Ls@¥©ḢWɗ,®Ẏ¤ð/ẈṢƑ$¿ḢY;Ɗ - Main Link: list of characters
Ḣ - pop and yield head
W - wrap in a list
, - pair with (the remaining list after Ḣ)
¿ - while...
$ - ...condition: last two links as a monad:
Ẉ - length of each
Ƒ - is invariant under:
Ṣ - sort
/ - ...do: reduce by:
ð ð - the enclosed dyadic chain -- i.e. f(head, tail):
Z - transpose
U - reverse each (giving a rotated head)
ɗ - last three links as a dyad:
¥ - last two links as a dyad:
L - length (i.e. number of rows in current roll)
@ - with swapped arguments:
s - split (the tail) into chunks of that length
© - (copy to register for later)
Ḣ - pop and yield head (Note register "copy" is altered too)
W - wrap in a list
; - concatenate (the rotated head with the first chunk of the tail)
¤ - nilad followed by link(s) as a nilad:
® - recall from register (other chunks of tail, or an empty list)
Ẏ - tighten (the chunks to a flat list)
, - pair (the concatenate result with the tightened chunks)
Ɗ - last three links as a monad:
Ḣ - pop and yield head
Y - join with newline characters
; - concatenate (the remaining tail)
- when running as a full program implicitly prints
$endgroup$
add a comment |
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
});
}
});
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f182727%2froll-the-carpet%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
Pyth, 37 bytes
.U+j;bZ.WgleHJlhH,+_MChZ<eZJ>eZJ,]hQt
Try it online here, or verify all the test cases at once here.
.U+j;bZ.WgleHJlhH,+_MChZ<eZJ>eZJ,]hQtQ Implicit: Q=eval(input())
Trailing Q inferred
]hQ First character of Q, wrapped in an array
tQ All but the first character of Q
, 2-element array of the two previous results
This yields array with rolled carpet (as array of strings) followed by the tail
.W While condition function is truthy, execute inner function, with initial value of the above:
gleHJlhH Condition function, input H
JlhH Number of layers in the current rolled carpet, store in J
leH Lenth of the tail
g J Is the above greater than or equal to J?
,+_MChZ<eZJ>eZJ Inner function, input Z
_MChZ Rotate the current rolled carpet (transpose, then reverse each row)
+ <eZJ Append the first J characters of the tail as a new row
, Pair the above with...
>eZJ ... all but the first J characters of the tail - this is the new tail
.U+j;bZ Join the carpet roll on newlines and append the tail, implicit print
$endgroup$
add a comment |
$begingroup$
Pyth, 37 bytes
.U+j;bZ.WgleHJlhH,+_MChZ<eZJ>eZJ,]hQt
Try it online here, or verify all the test cases at once here.
.U+j;bZ.WgleHJlhH,+_MChZ<eZJ>eZJ,]hQtQ Implicit: Q=eval(input())
Trailing Q inferred
]hQ First character of Q, wrapped in an array
tQ All but the first character of Q
, 2-element array of the two previous results
This yields array with rolled carpet (as array of strings) followed by the tail
.W While condition function is truthy, execute inner function, with initial value of the above:
gleHJlhH Condition function, input H
JlhH Number of layers in the current rolled carpet, store in J
leH Lenth of the tail
g J Is the above greater than or equal to J?
,+_MChZ<eZJ>eZJ Inner function, input Z
_MChZ Rotate the current rolled carpet (transpose, then reverse each row)
+ <eZJ Append the first J characters of the tail as a new row
, Pair the above with...
>eZJ ... all but the first J characters of the tail - this is the new tail
.U+j;bZ Join the carpet roll on newlines and append the tail, implicit print
$endgroup$
add a comment |
$begingroup$
Pyth, 37 bytes
.U+j;bZ.WgleHJlhH,+_MChZ<eZJ>eZJ,]hQt
Try it online here, or verify all the test cases at once here.
.U+j;bZ.WgleHJlhH,+_MChZ<eZJ>eZJ,]hQtQ Implicit: Q=eval(input())
Trailing Q inferred
]hQ First character of Q, wrapped in an array
tQ All but the first character of Q
, 2-element array of the two previous results
This yields array with rolled carpet (as array of strings) followed by the tail
.W While condition function is truthy, execute inner function, with initial value of the above:
gleHJlhH Condition function, input H
JlhH Number of layers in the current rolled carpet, store in J
leH Lenth of the tail
g J Is the above greater than or equal to J?
,+_MChZ<eZJ>eZJ Inner function, input Z
_MChZ Rotate the current rolled carpet (transpose, then reverse each row)
+ <eZJ Append the first J characters of the tail as a new row
, Pair the above with...
>eZJ ... all but the first J characters of the tail - this is the new tail
.U+j;bZ Join the carpet roll on newlines and append the tail, implicit print
$endgroup$
Pyth, 37 bytes
.U+j;bZ.WgleHJlhH,+_MChZ<eZJ>eZJ,]hQt
Try it online here, or verify all the test cases at once here.
.U+j;bZ.WgleHJlhH,+_MChZ<eZJ>eZJ,]hQtQ Implicit: Q=eval(input())
Trailing Q inferred
]hQ First character of Q, wrapped in an array
tQ All but the first character of Q
, 2-element array of the two previous results
This yields array with rolled carpet (as array of strings) followed by the tail
.W While condition function is truthy, execute inner function, with initial value of the above:
gleHJlhH Condition function, input H
JlhH Number of layers in the current rolled carpet, store in J
leH Lenth of the tail
g J Is the above greater than or equal to J?
,+_MChZ<eZJ>eZJ Inner function, input Z
_MChZ Rotate the current rolled carpet (transpose, then reverse each row)
+ <eZJ Append the first J characters of the tail as a new row
, Pair the above with...
>eZJ ... all but the first J characters of the tail - this is the new tail
.U+j;bZ Join the carpet roll on newlines and append the tail, implicit print
edited 13 hours ago
answered 14 hours ago
SokSok
4,177925
4,177925
add a comment |
add a comment |
$begingroup$
J, 73 bytes
[:(}:@[,{:@[,])&>/[:((|:@|.@[,#@[{.]);#@[}.])&>/^:(<:&#&>/)^:_,:@,:@{.;}.
Try it online!
I plan to golf this further after work tonight.
$endgroup$
add a comment |
$begingroup$
J, 73 bytes
[:(}:@[,{:@[,])&>/[:((|:@|.@[,#@[{.]);#@[}.])&>/^:(<:&#&>/)^:_,:@,:@{.;}.
Try it online!
I plan to golf this further after work tonight.
$endgroup$
add a comment |
$begingroup$
J, 73 bytes
[:(}:@[,{:@[,])&>/[:((|:@|.@[,#@[{.]);#@[}.])&>/^:(<:&#&>/)^:_,:@,:@{.;}.
Try it online!
I plan to golf this further after work tonight.
$endgroup$
J, 73 bytes
[:(}:@[,{:@[,])&>/[:((|:@|.@[,#@[{.]);#@[}.])&>/^:(<:&#&>/)^:_,:@,:@{.;}.
Try it online!
I plan to golf this further after work tonight.
answered 10 hours ago
JonahJonah
2,5611017
2,5611017
add a comment |
add a comment |
$begingroup$
R, 146 132 bytes
function(s){m=F[F]
while({m=rbind(t(m)[,F:0],s[1:F])
s=s[-1:-F]
length(s)>sum(F<-dim(m))})0
write(m[F:1,],1,F[1],,"")
cat(s,sep="")}
Try it online!
Implements the carpet-rolling procedure. Takes input as a list of characters and prints to stdout.
Saved 14 bytes by finding a way to use a do-while
loop and initializing using F
.
function(s){
m=F[F] # logical(0); create an empty array (this gets automatically promoted to character(0) later
while( # do-while loop
{m=rbind(t(m)[,F:0],s[1:F]) # rotate m counterclockwise and add the first F characters of s to the bottom
s=s[-1:-F] # remove those characters
length(s)>sum(F<-dim(m))})0 # while the number of characters remaining is greater than the sum of m's dimensions
write(m[F:1,],1,F[1],,"") # write the rolled portion write writes down the columns, we reverse each column
cat(s,sep="") # and write the remaining characters
}
$endgroup$
add a comment |
$begingroup$
R, 146 132 bytes
function(s){m=F[F]
while({m=rbind(t(m)[,F:0],s[1:F])
s=s[-1:-F]
length(s)>sum(F<-dim(m))})0
write(m[F:1,],1,F[1],,"")
cat(s,sep="")}
Try it online!
Implements the carpet-rolling procedure. Takes input as a list of characters and prints to stdout.
Saved 14 bytes by finding a way to use a do-while
loop and initializing using F
.
function(s){
m=F[F] # logical(0); create an empty array (this gets automatically promoted to character(0) later
while( # do-while loop
{m=rbind(t(m)[,F:0],s[1:F]) # rotate m counterclockwise and add the first F characters of s to the bottom
s=s[-1:-F] # remove those characters
length(s)>sum(F<-dim(m))})0 # while the number of characters remaining is greater than the sum of m's dimensions
write(m[F:1,],1,F[1],,"") # write the rolled portion write writes down the columns, we reverse each column
cat(s,sep="") # and write the remaining characters
}
$endgroup$
add a comment |
$begingroup$
R, 146 132 bytes
function(s){m=F[F]
while({m=rbind(t(m)[,F:0],s[1:F])
s=s[-1:-F]
length(s)>sum(F<-dim(m))})0
write(m[F:1,],1,F[1],,"")
cat(s,sep="")}
Try it online!
Implements the carpet-rolling procedure. Takes input as a list of characters and prints to stdout.
Saved 14 bytes by finding a way to use a do-while
loop and initializing using F
.
function(s){
m=F[F] # logical(0); create an empty array (this gets automatically promoted to character(0) later
while( # do-while loop
{m=rbind(t(m)[,F:0],s[1:F]) # rotate m counterclockwise and add the first F characters of s to the bottom
s=s[-1:-F] # remove those characters
length(s)>sum(F<-dim(m))})0 # while the number of characters remaining is greater than the sum of m's dimensions
write(m[F:1,],1,F[1],,"") # write the rolled portion write writes down the columns, we reverse each column
cat(s,sep="") # and write the remaining characters
}
$endgroup$
R, 146 132 bytes
function(s){m=F[F]
while({m=rbind(t(m)[,F:0],s[1:F])
s=s[-1:-F]
length(s)>sum(F<-dim(m))})0
write(m[F:1,],1,F[1],,"")
cat(s,sep="")}
Try it online!
Implements the carpet-rolling procedure. Takes input as a list of characters and prints to stdout.
Saved 14 bytes by finding a way to use a do-while
loop and initializing using F
.
function(s){
m=F[F] # logical(0); create an empty array (this gets automatically promoted to character(0) later
while( # do-while loop
{m=rbind(t(m)[,F:0],s[1:F]) # rotate m counterclockwise and add the first F characters of s to the bottom
s=s[-1:-F] # remove those characters
length(s)>sum(F<-dim(m))})0 # while the number of characters remaining is greater than the sum of m's dimensions
write(m[F:1,],1,F[1],,"") # write the rolled portion write writes down the columns, we reverse each column
cat(s,sep="") # and write the remaining characters
}
edited 13 hours ago
answered 14 hours ago
GiuseppeGiuseppe
17.6k31152
17.6k31152
add a comment |
add a comment |
$begingroup$
Jelly, 30 bytes
Seems overly long...
ḢW,ðZU;Ls@¥©ḢWɗ,®Ẏ¤ð/ẈṢƑ$¿ḢY;Ɗ
Try it online!
How?
ḢW,ðZU;Ls@¥©ḢWɗ,®Ẏ¤ð/ẈṢƑ$¿ḢY;Ɗ - Main Link: list of characters
Ḣ - pop and yield head
W - wrap in a list
, - pair with (the remaining list after Ḣ)
¿ - while...
$ - ...condition: last two links as a monad:
Ẉ - length of each
Ƒ - is invariant under:
Ṣ - sort
/ - ...do: reduce by:
ð ð - the enclosed dyadic chain -- i.e. f(head, tail):
Z - transpose
U - reverse each (giving a rotated head)
ɗ - last three links as a dyad:
¥ - last two links as a dyad:
L - length (i.e. number of rows in current roll)
@ - with swapped arguments:
s - split (the tail) into chunks of that length
© - (copy to register for later)
Ḣ - pop and yield head (Note register "copy" is altered too)
W - wrap in a list
; - concatenate (the rotated head with the first chunk of the tail)
¤ - nilad followed by link(s) as a nilad:
® - recall from register (other chunks of tail, or an empty list)
Ẏ - tighten (the chunks to a flat list)
, - pair (the concatenate result with the tightened chunks)
Ɗ - last three links as a monad:
Ḣ - pop and yield head
Y - join with newline characters
; - concatenate (the remaining tail)
- when running as a full program implicitly prints
$endgroup$
add a comment |
$begingroup$
Jelly, 30 bytes
Seems overly long...
ḢW,ðZU;Ls@¥©ḢWɗ,®Ẏ¤ð/ẈṢƑ$¿ḢY;Ɗ
Try it online!
How?
ḢW,ðZU;Ls@¥©ḢWɗ,®Ẏ¤ð/ẈṢƑ$¿ḢY;Ɗ - Main Link: list of characters
Ḣ - pop and yield head
W - wrap in a list
, - pair with (the remaining list after Ḣ)
¿ - while...
$ - ...condition: last two links as a monad:
Ẉ - length of each
Ƒ - is invariant under:
Ṣ - sort
/ - ...do: reduce by:
ð ð - the enclosed dyadic chain -- i.e. f(head, tail):
Z - transpose
U - reverse each (giving a rotated head)
ɗ - last three links as a dyad:
¥ - last two links as a dyad:
L - length (i.e. number of rows in current roll)
@ - with swapped arguments:
s - split (the tail) into chunks of that length
© - (copy to register for later)
Ḣ - pop and yield head (Note register "copy" is altered too)
W - wrap in a list
; - concatenate (the rotated head with the first chunk of the tail)
¤ - nilad followed by link(s) as a nilad:
® - recall from register (other chunks of tail, or an empty list)
Ẏ - tighten (the chunks to a flat list)
, - pair (the concatenate result with the tightened chunks)
Ɗ - last three links as a monad:
Ḣ - pop and yield head
Y - join with newline characters
; - concatenate (the remaining tail)
- when running as a full program implicitly prints
$endgroup$
add a comment |
$begingroup$
Jelly, 30 bytes
Seems overly long...
ḢW,ðZU;Ls@¥©ḢWɗ,®Ẏ¤ð/ẈṢƑ$¿ḢY;Ɗ
Try it online!
How?
ḢW,ðZU;Ls@¥©ḢWɗ,®Ẏ¤ð/ẈṢƑ$¿ḢY;Ɗ - Main Link: list of characters
Ḣ - pop and yield head
W - wrap in a list
, - pair with (the remaining list after Ḣ)
¿ - while...
$ - ...condition: last two links as a monad:
Ẉ - length of each
Ƒ - is invariant under:
Ṣ - sort
/ - ...do: reduce by:
ð ð - the enclosed dyadic chain -- i.e. f(head, tail):
Z - transpose
U - reverse each (giving a rotated head)
ɗ - last three links as a dyad:
¥ - last two links as a dyad:
L - length (i.e. number of rows in current roll)
@ - with swapped arguments:
s - split (the tail) into chunks of that length
© - (copy to register for later)
Ḣ - pop and yield head (Note register "copy" is altered too)
W - wrap in a list
; - concatenate (the rotated head with the first chunk of the tail)
¤ - nilad followed by link(s) as a nilad:
® - recall from register (other chunks of tail, or an empty list)
Ẏ - tighten (the chunks to a flat list)
, - pair (the concatenate result with the tightened chunks)
Ɗ - last three links as a monad:
Ḣ - pop and yield head
Y - join with newline characters
; - concatenate (the remaining tail)
- when running as a full program implicitly prints
$endgroup$
Jelly, 30 bytes
Seems overly long...
ḢW,ðZU;Ls@¥©ḢWɗ,®Ẏ¤ð/ẈṢƑ$¿ḢY;Ɗ
Try it online!
How?
ḢW,ðZU;Ls@¥©ḢWɗ,®Ẏ¤ð/ẈṢƑ$¿ḢY;Ɗ - Main Link: list of characters
Ḣ - pop and yield head
W - wrap in a list
, - pair with (the remaining list after Ḣ)
¿ - while...
$ - ...condition: last two links as a monad:
Ẉ - length of each
Ƒ - is invariant under:
Ṣ - sort
/ - ...do: reduce by:
ð ð - the enclosed dyadic chain -- i.e. f(head, tail):
Z - transpose
U - reverse each (giving a rotated head)
ɗ - last three links as a dyad:
¥ - last two links as a dyad:
L - length (i.e. number of rows in current roll)
@ - with swapped arguments:
s - split (the tail) into chunks of that length
© - (copy to register for later)
Ḣ - pop and yield head (Note register "copy" is altered too)
W - wrap in a list
; - concatenate (the rotated head with the first chunk of the tail)
¤ - nilad followed by link(s) as a nilad:
® - recall from register (other chunks of tail, or an empty list)
Ẏ - tighten (the chunks to a flat list)
, - pair (the concatenate result with the tightened chunks)
Ɗ - last three links as a monad:
Ḣ - pop and yield head
Y - join with newline characters
; - concatenate (the remaining tail)
- when running as a full program implicitly prints
answered 9 hours ago
Jonathan AllanJonathan Allan
53.7k535173
53.7k535173
add a comment |
add a comment |
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).
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f182727%2froll-the-carpet%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
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
Required, but never shown
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
Required, but never shown
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
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
3
$begingroup$
Closely related
$endgroup$
– Giuseppe
15 hours ago
2
$begingroup$
Also this one: codegolf.stackexchange.com/questions/125966/…, but none include the termination check.
$endgroup$
– Bromind
15 hours ago
3
$begingroup$
Suggested test case:
ProgrammingPuzzlesAndCodeGolf
- the final tail length greater than 1 tripped me up.$endgroup$
– Sok
14 hours ago
$begingroup$
Where is the head on the second
carpet
example, where nothing is rolled up?$endgroup$
– Embodiment of Ignorance
12 hours ago
1
$begingroup$
I think you've swapped the words "head" and "tail" here: "if the width of the new head [...] is greater or equal than the length of the tail [...]".
$endgroup$
– Erik the Outgolfer
7 hours ago