Count rook moves 1DTell me the movesWhat is the Probability that a Knight Stays on Chessboard?Can the king...
What is the maximal acceptable delay between pilot's input and flight control surface actuation?
Are there any writings by blinded and/or exiled Byzantine emperors?
Can an intercepting fighter jet force a small propeller aircraft down without completely destroying it?
Count rook moves 1D
How do I stop making people jump at home and at work?
Why not use futuristic pavise ballistic shields for protection?
What happens if you just start drawing from the Deck of Many Things without declaring any number of cards?
Received email from ISP saying one of my devices has malware
What is the converted mana cost of land cards?
Map a function that takes arguments in different levels of a list
To which country did MiGs in Top Gun belong?
Tiny image scraper for xkcd.com
How to add some symbol (or just add newline) if the numbers in the text are not continuous
Visiting girlfriend in the USA
Initializing a std::array with a constant value
How could reincarnation magic be limited to prevent overuse?
Why are Latin and Sanskrit called dead languages?
My boss says "This will help us better view the utilization of your services." Does this mean my job is ending in this organisation?
Is there anything in the universe that cannot be compressed?
How did Gollum know Sauron was gathering the Haradrim to make war?
Importance of electrolytic capacitor size
What is the significance of 104%?
Lumix G7: Raw photos only in 1920x1440, no higher res available
How to check status of Wi-Fi adapter through command line?
Count rook moves 1D
Tell me the movesWhat is the Probability that a Knight Stays on Chessboard?Can the king catch the pawn?Shifty Eyes Shifting I'sHelp Sam understand chessChess conversionEscape a chessboardWorking on my Knight movesHow many moves?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}
$begingroup$
Given a position with a row of rooks and/or empty spaces, output how many different rook moves are possible. A rook can move left or right to an empty space, but not to one that requires passing over another rook. When a rook moves, the other rooks remain in place.
For example, from this position, 6 moves are possible:
.R..RRR.
- The first (leftmost) rook can move 1 space left, or 1 or 2 spaces right (3 moves)
- The next rook can only move 1 or 2 spaces left (2 moves)
- The third rook cannot move at all because it's squeezed between two other rooks (0 moves)
- The last rook can only move 1 space right (1 move)
Note that a position might have no rooks at all, or no empty spaces at all.
Input: A non-empty list (string, array, etc..) of rooks and empty spaces. You can represent them as True
/False
, 1
/0
, 'R'
/'.'
, or any two consistent distinct single-byte characters or one-digit numbers of your choice. It's up to you which one means rook and which means empty space.
Output: A non-negative integer. Whole-number floats are also fine.
Test cases
The output is the number on the left.
6 .R..RRR.
0 .
0 R
4 R..RR
3 ...R
8 ..R..R..
0 ......
For more test cases, here are all inputs up to length 5.
0 .
0 R
0 ..
1 .R
1 R.
0 RR
0 ...
2 ..R
2 .R.
1 .RR
2 R..
2 R.R
1 RR.
0 RRR
0 ....
3 ...R
3 ..R.
2 ..RR
3 .R..
3 .R.R
2 .RR.
1 .RRR
3 R...
4 R..R
3 R.R.
2 R.RR
2 RR..
2 RR.R
1 RRR.
0 RRRR
0 .....
4 ....R
4 ...R.
3 ...RR
4 ..R..
4 ..R.R
3 ..RR.
2 ..RRR
4 .R...
5 .R..R
4 .R.R.
3 .R.RR
3 .RR..
3 .RR.R
2 .RRR.
1 .RRRR
4 R....
6 R...R
5 R..R.
4 R..RR
4 R.R..
4 R.R.R
3 R.RR.
2 R.RRR
3 RR...
4 RR..R
3 RR.R.
2 RR.RR
2 RRR..
2 RRR.R
1 RRRR.
0 RRRRR
code-golf counting chess
$endgroup$
add a comment |
$begingroup$
Given a position with a row of rooks and/or empty spaces, output how many different rook moves are possible. A rook can move left or right to an empty space, but not to one that requires passing over another rook. When a rook moves, the other rooks remain in place.
For example, from this position, 6 moves are possible:
.R..RRR.
- The first (leftmost) rook can move 1 space left, or 1 or 2 spaces right (3 moves)
- The next rook can only move 1 or 2 spaces left (2 moves)
- The third rook cannot move at all because it's squeezed between two other rooks (0 moves)
- The last rook can only move 1 space right (1 move)
Note that a position might have no rooks at all, or no empty spaces at all.
Input: A non-empty list (string, array, etc..) of rooks and empty spaces. You can represent them as True
/False
, 1
/0
, 'R'
/'.'
, or any two consistent distinct single-byte characters or one-digit numbers of your choice. It's up to you which one means rook and which means empty space.
Output: A non-negative integer. Whole-number floats are also fine.
Test cases
The output is the number on the left.
6 .R..RRR.
0 .
0 R
4 R..RR
3 ...R
8 ..R..R..
0 ......
For more test cases, here are all inputs up to length 5.
0 .
0 R
0 ..
1 .R
1 R.
0 RR
0 ...
2 ..R
2 .R.
1 .RR
2 R..
2 R.R
1 RR.
0 RRR
0 ....
3 ...R
3 ..R.
2 ..RR
3 .R..
3 .R.R
2 .RR.
1 .RRR
3 R...
4 R..R
3 R.R.
2 R.RR
2 RR..
2 RR.R
1 RRR.
0 RRRR
0 .....
4 ....R
4 ...R.
3 ...RR
4 ..R..
4 ..R.R
3 ..RR.
2 ..RRR
4 .R...
5 .R..R
4 .R.R.
3 .R.RR
3 .RR..
3 .RR.R
2 .RRR.
1 .RRRR
4 R....
6 R...R
5 R..R.
4 R..RR
4 R.R..
4 R.R.R
3 R.RR.
2 R.RRR
3 RR...
4 RR..R
3 RR.R.
2 RR.RR
2 RRR..
2 RRR.R
1 RRRR.
0 RRRRR
code-golf counting chess
$endgroup$
add a comment |
$begingroup$
Given a position with a row of rooks and/or empty spaces, output how many different rook moves are possible. A rook can move left or right to an empty space, but not to one that requires passing over another rook. When a rook moves, the other rooks remain in place.
For example, from this position, 6 moves are possible:
.R..RRR.
- The first (leftmost) rook can move 1 space left, or 1 or 2 spaces right (3 moves)
- The next rook can only move 1 or 2 spaces left (2 moves)
- The third rook cannot move at all because it's squeezed between two other rooks (0 moves)
- The last rook can only move 1 space right (1 move)
Note that a position might have no rooks at all, or no empty spaces at all.
Input: A non-empty list (string, array, etc..) of rooks and empty spaces. You can represent them as True
/False
, 1
/0
, 'R'
/'.'
, or any two consistent distinct single-byte characters or one-digit numbers of your choice. It's up to you which one means rook and which means empty space.
Output: A non-negative integer. Whole-number floats are also fine.
Test cases
The output is the number on the left.
6 .R..RRR.
0 .
0 R
4 R..RR
3 ...R
8 ..R..R..
0 ......
For more test cases, here are all inputs up to length 5.
0 .
0 R
0 ..
1 .R
1 R.
0 RR
0 ...
2 ..R
2 .R.
1 .RR
2 R..
2 R.R
1 RR.
0 RRR
0 ....
3 ...R
3 ..R.
2 ..RR
3 .R..
3 .R.R
2 .RR.
1 .RRR
3 R...
4 R..R
3 R.R.
2 R.RR
2 RR..
2 RR.R
1 RRR.
0 RRRR
0 .....
4 ....R
4 ...R.
3 ...RR
4 ..R..
4 ..R.R
3 ..RR.
2 ..RRR
4 .R...
5 .R..R
4 .R.R.
3 .R.RR
3 .RR..
3 .RR.R
2 .RRR.
1 .RRRR
4 R....
6 R...R
5 R..R.
4 R..RR
4 R.R..
4 R.R.R
3 R.RR.
2 R.RRR
3 RR...
4 RR..R
3 RR.R.
2 RR.RR
2 RRR..
2 RRR.R
1 RRRR.
0 RRRRR
code-golf counting chess
$endgroup$
Given a position with a row of rooks and/or empty spaces, output how many different rook moves are possible. A rook can move left or right to an empty space, but not to one that requires passing over another rook. When a rook moves, the other rooks remain in place.
For example, from this position, 6 moves are possible:
.R..RRR.
- The first (leftmost) rook can move 1 space left, or 1 or 2 spaces right (3 moves)
- The next rook can only move 1 or 2 spaces left (2 moves)
- The third rook cannot move at all because it's squeezed between two other rooks (0 moves)
- The last rook can only move 1 space right (1 move)
Note that a position might have no rooks at all, or no empty spaces at all.
Input: A non-empty list (string, array, etc..) of rooks and empty spaces. You can represent them as True
/False
, 1
/0
, 'R'
/'.'
, or any two consistent distinct single-byte characters or one-digit numbers of your choice. It's up to you which one means rook and which means empty space.
Output: A non-negative integer. Whole-number floats are also fine.
Test cases
The output is the number on the left.
6 .R..RRR.
0 .
0 R
4 R..RR
3 ...R
8 ..R..R..
0 ......
For more test cases, here are all inputs up to length 5.
0 .
0 R
0 ..
1 .R
1 R.
0 RR
0 ...
2 ..R
2 .R.
1 .RR
2 R..
2 R.R
1 RR.
0 RRR
0 ....
3 ...R
3 ..R.
2 ..RR
3 .R..
3 .R.R
2 .RR.
1 .RRR
3 R...
4 R..R
3 R.R.
2 R.RR
2 RR..
2 RR.R
1 RRR.
0 RRRR
0 .....
4 ....R
4 ...R.
3 ...RR
4 ..R..
4 ..R.R
3 ..RR.
2 ..RRR
4 .R...
5 .R..R
4 .R.R.
3 .R.RR
3 .RR..
3 .RR.R
2 .RRR.
1 .RRRR
4 R....
6 R...R
5 R..R.
4 R..RR
4 R.R..
4 R.R.R
3 R.RR.
2 R.RRR
3 RR...
4 RR..R
3 RR.R.
2 RR.RR
2 RRR..
2 RRR.R
1 RRRR.
0 RRRRR
code-golf counting chess
code-golf counting chess
asked 8 hours ago
xnorxnor
98.7k19 gold badges202 silver badges463 bronze badges
98.7k19 gold badges202 silver badges463 bronze badges
add a comment |
add a comment |
17 Answers
17
active
oldest
votes
$begingroup$
JavaScript (ES6), 38 33 bytes
Saved 5 bytes thanks to @JoKing
Takes input as a string. Expects a space for an empty square and any other character for a rook.
s=>(s+s).trim().split` `.length-1
Try it online!
Commented
s => // s = input, e.g. " R RRR "
(s + s) // double -> " R RRR R RRR "
.trim() // remove leading and trailing spaces -> "R RRR R RRR"
.split` ` // split on spaces -> [ 'R', '', 'RRR', '', 'R', '', 'RRR' ]
.length - 1 // return the length - 1 -> 6
Python 2, 40 bytes
lambda s:len((s+s).strip().split(' '))-1
Try it online!
$endgroup$
$begingroup$
The Python version should usecount
instead ofsplit
(TIO)
$endgroup$
– Grimy
4 hours ago
add a comment |
$begingroup$
Retina, 14 9 bytes
w`_+R|R_+
Try it online! Link includes test cases. Uses _
for empty space as it's the most pleasant non-regex character. Works by counting the number of substrings that correspond to a valid Rook move. A substring is a valid Rook move if it contains at least one _
plus a single R
at either the beginning or the end.
$endgroup$
$begingroup$
Oh, you basically figured out how to do what I mentioned in my answer. Idk why I didn't think of that.
$endgroup$
– mbomb007
4 hours ago
add a comment |
$begingroup$
Python 3, 30 29 bytes
lambda s:sum((s+s).strip())/9
Try it online!
-1 byte thanks to @JoKing
The function takes a Python byte string as input. Each empty space is encoded as a tab and each rook is encoded as a byte b'x00'
having value 0
.
The computation is equivalent to lambda s:(s+s).strip().count(b't')
while having a lower byte count.
$endgroup$
add a comment |
$begingroup$
Jelly, 6 bytes
t1;ḟẠS
Try it online!
A monadic link taking a list of 0
for rook and 1
for space and returning an integer with the number of moves. The TIO link takes the pasted list of possible boards given in the question, converts to the right format and then outputs the calculated and correct answers.
Explanation
t1 | Trim 1s from end
; | Concatenate to input
ḟẠ | Filter out 1s if all input were 1s, otherwise filter out 0s
S | Sum
$endgroup$
add a comment |
$begingroup$
Python 2, 59 bytes
def f(r):S=map(len,r.split('R'));return sum(S)*2-S[0]-S[-1]
Try it online!
$endgroup$
add a comment |
$begingroup$
Perl 6, 16 bytes
{+m:ex/s+R|Rs+/}
Try it online!
A regex that matches all exhaustive instances of rooks followed by spaces, or spaces followed by a rook and returns the number of matches.
$endgroup$
add a comment |
$begingroup$
Retina, 23 15 bytes
Double the number of spaces between rooks, grep lines with at least one rook, then count the number of spaces.
R +R
$0$0
G`R
Try it online!
Though the program uses spaces instead of periods, I added prefix code so that the test cases provided may be easily pasted in and used.
I was hoping I could use overlapping matches with (?<=R.*) | (?=.*R)
, but overlaps aren't quite that aggressive. It would need to count all possible ways a match could be obtained in order to return the correct result with that method.
$endgroup$
add a comment |
$begingroup$
Jelly, 6 bytes
ṣ0§+ƝS
Try it online!
0
represent a rook, 1
represents an empty space.
$endgroup$
1
$begingroup$
If you use a space character for a Rook and another character for a space you can useẈ
to get the five:ḲẈ+ƝS
$endgroup$
– Jonathan Allan
5 hours ago
add a comment |
$begingroup$
Japt, 6 bytes
²x ¸ÊÉ
Try it online
$endgroup$
add a comment |
$begingroup$
Japt, 6 bytes
Spaces for spaces, any other character for rooks.
²x ¸ÊÉ
Try it
$endgroup$
$begingroup$
Beat ya by 10 minutes :P
$endgroup$
– Oliver
5 hours ago
add a comment |
$begingroup$
Stax, 7 bytes
âE¶ƒÅ╒±
Run and debug it
$endgroup$
add a comment |
$begingroup$
Wolfram Language (Mathematica), 43 38 bytes
Count[Subsequences@#,{0,1..}|{1..,0}]&
Try it online!
Port of Neil's Retina solution. Uses 1 for spaces and 0 for rooks.
$endgroup$
add a comment |
$begingroup$
05AB1E, 5 bytes
«ðÚð¢
Try it online!
« # concatenate the input with itself
ðÚ # remove leading and trailing spaces
ð¢ # count spaces
$endgroup$
add a comment |
$begingroup$
C# (Visual C# Interactive Compiler), 28 bytes
x=>(x+x).Trim().Sum(d=>d)/32
Try it online!
$endgroup$
add a comment |
$begingroup$
Japt, 5 bytes
²x èS
Try it
²x èS Implicit input string of U
² U + U
x Remove trailing and leading whitespace
èS Number of spaces
$endgroup$
add a comment |
$begingroup$
Haskell, 68 58 54 bytes
(n#y)(a:x)|a<1=n+(0#1)x|m<-n+1=y+(m#y)x
(_#_)x=0
f=0#0
Try it online!
$endgroup$
add a comment |
$begingroup$
C, 181 bytes
#include<stdio.h>
#include<string.h>
main(a,c,d,e){c=a=d=0;char*b=calloc(99,1);gets(b);do{if(b[a]=='R'){c+=e*(1+d);e=0;d=1;}if(b[a]=='.')e++;a++;}while(b[a]);c+=e;printf("%i",c);}
New contributor
$endgroup$
add a comment |
Your Answer
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%2f191180%2fcount-rook-moves-1d%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
17 Answers
17
active
oldest
votes
17 Answers
17
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
JavaScript (ES6), 38 33 bytes
Saved 5 bytes thanks to @JoKing
Takes input as a string. Expects a space for an empty square and any other character for a rook.
s=>(s+s).trim().split` `.length-1
Try it online!
Commented
s => // s = input, e.g. " R RRR "
(s + s) // double -> " R RRR R RRR "
.trim() // remove leading and trailing spaces -> "R RRR R RRR"
.split` ` // split on spaces -> [ 'R', '', 'RRR', '', 'R', '', 'RRR' ]
.length - 1 // return the length - 1 -> 6
Python 2, 40 bytes
lambda s:len((s+s).strip().split(' '))-1
Try it online!
$endgroup$
$begingroup$
The Python version should usecount
instead ofsplit
(TIO)
$endgroup$
– Grimy
4 hours ago
add a comment |
$begingroup$
JavaScript (ES6), 38 33 bytes
Saved 5 bytes thanks to @JoKing
Takes input as a string. Expects a space for an empty square and any other character for a rook.
s=>(s+s).trim().split` `.length-1
Try it online!
Commented
s => // s = input, e.g. " R RRR "
(s + s) // double -> " R RRR R RRR "
.trim() // remove leading and trailing spaces -> "R RRR R RRR"
.split` ` // split on spaces -> [ 'R', '', 'RRR', '', 'R', '', 'RRR' ]
.length - 1 // return the length - 1 -> 6
Python 2, 40 bytes
lambda s:len((s+s).strip().split(' '))-1
Try it online!
$endgroup$
$begingroup$
The Python version should usecount
instead ofsplit
(TIO)
$endgroup$
– Grimy
4 hours ago
add a comment |
$begingroup$
JavaScript (ES6), 38 33 bytes
Saved 5 bytes thanks to @JoKing
Takes input as a string. Expects a space for an empty square and any other character for a rook.
s=>(s+s).trim().split` `.length-1
Try it online!
Commented
s => // s = input, e.g. " R RRR "
(s + s) // double -> " R RRR R RRR "
.trim() // remove leading and trailing spaces -> "R RRR R RRR"
.split` ` // split on spaces -> [ 'R', '', 'RRR', '', 'R', '', 'RRR' ]
.length - 1 // return the length - 1 -> 6
Python 2, 40 bytes
lambda s:len((s+s).strip().split(' '))-1
Try it online!
$endgroup$
JavaScript (ES6), 38 33 bytes
Saved 5 bytes thanks to @JoKing
Takes input as a string. Expects a space for an empty square and any other character for a rook.
s=>(s+s).trim().split` `.length-1
Try it online!
Commented
s => // s = input, e.g. " R RRR "
(s + s) // double -> " R RRR R RRR "
.trim() // remove leading and trailing spaces -> "R RRR R RRR"
.split` ` // split on spaces -> [ 'R', '', 'RRR', '', 'R', '', 'RRR' ]
.length - 1 // return the length - 1 -> 6
Python 2, 40 bytes
lambda s:len((s+s).strip().split(' '))-1
Try it online!
edited 5 hours ago
answered 7 hours ago
ArnauldArnauld
91.6k7 gold badges107 silver badges373 bronze badges
91.6k7 gold badges107 silver badges373 bronze badges
$begingroup$
The Python version should usecount
instead ofsplit
(TIO)
$endgroup$
– Grimy
4 hours ago
add a comment |
$begingroup$
The Python version should usecount
instead ofsplit
(TIO)
$endgroup$
– Grimy
4 hours ago
$begingroup$
The Python version should use
count
instead of split
(TIO)$endgroup$
– Grimy
4 hours ago
$begingroup$
The Python version should use
count
instead of split
(TIO)$endgroup$
– Grimy
4 hours ago
add a comment |
$begingroup$
Retina, 14 9 bytes
w`_+R|R_+
Try it online! Link includes test cases. Uses _
for empty space as it's the most pleasant non-regex character. Works by counting the number of substrings that correspond to a valid Rook move. A substring is a valid Rook move if it contains at least one _
plus a single R
at either the beginning or the end.
$endgroup$
$begingroup$
Oh, you basically figured out how to do what I mentioned in my answer. Idk why I didn't think of that.
$endgroup$
– mbomb007
4 hours ago
add a comment |
$begingroup$
Retina, 14 9 bytes
w`_+R|R_+
Try it online! Link includes test cases. Uses _
for empty space as it's the most pleasant non-regex character. Works by counting the number of substrings that correspond to a valid Rook move. A substring is a valid Rook move if it contains at least one _
plus a single R
at either the beginning or the end.
$endgroup$
$begingroup$
Oh, you basically figured out how to do what I mentioned in my answer. Idk why I didn't think of that.
$endgroup$
– mbomb007
4 hours ago
add a comment |
$begingroup$
Retina, 14 9 bytes
w`_+R|R_+
Try it online! Link includes test cases. Uses _
for empty space as it's the most pleasant non-regex character. Works by counting the number of substrings that correspond to a valid Rook move. A substring is a valid Rook move if it contains at least one _
plus a single R
at either the beginning or the end.
$endgroup$
Retina, 14 9 bytes
w`_+R|R_+
Try it online! Link includes test cases. Uses _
for empty space as it's the most pleasant non-regex character. Works by counting the number of substrings that correspond to a valid Rook move. A substring is a valid Rook move if it contains at least one _
plus a single R
at either the beginning or the end.
edited 5 hours ago
answered 5 hours ago
NeilNeil
88.2k8 gold badges46 silver badges186 bronze badges
88.2k8 gold badges46 silver badges186 bronze badges
$begingroup$
Oh, you basically figured out how to do what I mentioned in my answer. Idk why I didn't think of that.
$endgroup$
– mbomb007
4 hours ago
add a comment |
$begingroup$
Oh, you basically figured out how to do what I mentioned in my answer. Idk why I didn't think of that.
$endgroup$
– mbomb007
4 hours ago
$begingroup$
Oh, you basically figured out how to do what I mentioned in my answer. Idk why I didn't think of that.
$endgroup$
– mbomb007
4 hours ago
$begingroup$
Oh, you basically figured out how to do what I mentioned in my answer. Idk why I didn't think of that.
$endgroup$
– mbomb007
4 hours ago
add a comment |
$begingroup$
Python 3, 30 29 bytes
lambda s:sum((s+s).strip())/9
Try it online!
-1 byte thanks to @JoKing
The function takes a Python byte string as input. Each empty space is encoded as a tab and each rook is encoded as a byte b'x00'
having value 0
.
The computation is equivalent to lambda s:(s+s).strip().count(b't')
while having a lower byte count.
$endgroup$
add a comment |
$begingroup$
Python 3, 30 29 bytes
lambda s:sum((s+s).strip())/9
Try it online!
-1 byte thanks to @JoKing
The function takes a Python byte string as input. Each empty space is encoded as a tab and each rook is encoded as a byte b'x00'
having value 0
.
The computation is equivalent to lambda s:(s+s).strip().count(b't')
while having a lower byte count.
$endgroup$
add a comment |
$begingroup$
Python 3, 30 29 bytes
lambda s:sum((s+s).strip())/9
Try it online!
-1 byte thanks to @JoKing
The function takes a Python byte string as input. Each empty space is encoded as a tab and each rook is encoded as a byte b'x00'
having value 0
.
The computation is equivalent to lambda s:(s+s).strip().count(b't')
while having a lower byte count.
$endgroup$
Python 3, 30 29 bytes
lambda s:sum((s+s).strip())/9
Try it online!
-1 byte thanks to @JoKing
The function takes a Python byte string as input. Each empty space is encoded as a tab and each rook is encoded as a byte b'x00'
having value 0
.
The computation is equivalent to lambda s:(s+s).strip().count(b't')
while having a lower byte count.
edited 3 hours ago
answered 4 hours ago
JoelJoel
8618 bronze badges
8618 bronze badges
add a comment |
add a comment |
$begingroup$
Jelly, 6 bytes
t1;ḟẠS
Try it online!
A monadic link taking a list of 0
for rook and 1
for space and returning an integer with the number of moves. The TIO link takes the pasted list of possible boards given in the question, converts to the right format and then outputs the calculated and correct answers.
Explanation
t1 | Trim 1s from end
; | Concatenate to input
ḟẠ | Filter out 1s if all input were 1s, otherwise filter out 0s
S | Sum
$endgroup$
add a comment |
$begingroup$
Jelly, 6 bytes
t1;ḟẠS
Try it online!
A monadic link taking a list of 0
for rook and 1
for space and returning an integer with the number of moves. The TIO link takes the pasted list of possible boards given in the question, converts to the right format and then outputs the calculated and correct answers.
Explanation
t1 | Trim 1s from end
; | Concatenate to input
ḟẠ | Filter out 1s if all input were 1s, otherwise filter out 0s
S | Sum
$endgroup$
add a comment |
$begingroup$
Jelly, 6 bytes
t1;ḟẠS
Try it online!
A monadic link taking a list of 0
for rook and 1
for space and returning an integer with the number of moves. The TIO link takes the pasted list of possible boards given in the question, converts to the right format and then outputs the calculated and correct answers.
Explanation
t1 | Trim 1s from end
; | Concatenate to input
ḟẠ | Filter out 1s if all input were 1s, otherwise filter out 0s
S | Sum
$endgroup$
Jelly, 6 bytes
t1;ḟẠS
Try it online!
A monadic link taking a list of 0
for rook and 1
for space and returning an integer with the number of moves. The TIO link takes the pasted list of possible boards given in the question, converts to the right format and then outputs the calculated and correct answers.
Explanation
t1 | Trim 1s from end
; | Concatenate to input
ḟẠ | Filter out 1s if all input were 1s, otherwise filter out 0s
S | Sum
answered 6 hours ago
Nick KennedyNick Kennedy
6,3971 gold badge9 silver badges15 bronze badges
6,3971 gold badge9 silver badges15 bronze badges
add a comment |
add a comment |
$begingroup$
Python 2, 59 bytes
def f(r):S=map(len,r.split('R'));return sum(S)*2-S[0]-S[-1]
Try it online!
$endgroup$
add a comment |
$begingroup$
Python 2, 59 bytes
def f(r):S=map(len,r.split('R'));return sum(S)*2-S[0]-S[-1]
Try it online!
$endgroup$
add a comment |
$begingroup$
Python 2, 59 bytes
def f(r):S=map(len,r.split('R'));return sum(S)*2-S[0]-S[-1]
Try it online!
$endgroup$
Python 2, 59 bytes
def f(r):S=map(len,r.split('R'));return sum(S)*2-S[0]-S[-1]
Try it online!
answered 6 hours ago
Chas BrownChas Brown
6,5491 gold badge6 silver badges27 bronze badges
6,5491 gold badge6 silver badges27 bronze badges
add a comment |
add a comment |
$begingroup$
Perl 6, 16 bytes
{+m:ex/s+R|Rs+/}
Try it online!
A regex that matches all exhaustive instances of rooks followed by spaces, or spaces followed by a rook and returns the number of matches.
$endgroup$
add a comment |
$begingroup$
Perl 6, 16 bytes
{+m:ex/s+R|Rs+/}
Try it online!
A regex that matches all exhaustive instances of rooks followed by spaces, or spaces followed by a rook and returns the number of matches.
$endgroup$
add a comment |
$begingroup$
Perl 6, 16 bytes
{+m:ex/s+R|Rs+/}
Try it online!
A regex that matches all exhaustive instances of rooks followed by spaces, or spaces followed by a rook and returns the number of matches.
$endgroup$
Perl 6, 16 bytes
{+m:ex/s+R|Rs+/}
Try it online!
A regex that matches all exhaustive instances of rooks followed by spaces, or spaces followed by a rook and returns the number of matches.
edited 5 hours ago
answered 5 hours ago
Jo KingJo King
31k4 gold badges72 silver badges139 bronze badges
31k4 gold badges72 silver badges139 bronze badges
add a comment |
add a comment |
$begingroup$
Retina, 23 15 bytes
Double the number of spaces between rooks, grep lines with at least one rook, then count the number of spaces.
R +R
$0$0
G`R
Try it online!
Though the program uses spaces instead of periods, I added prefix code so that the test cases provided may be easily pasted in and used.
I was hoping I could use overlapping matches with (?<=R.*) | (?=.*R)
, but overlaps aren't quite that aggressive. It would need to count all possible ways a match could be obtained in order to return the correct result with that method.
$endgroup$
add a comment |
$begingroup$
Retina, 23 15 bytes
Double the number of spaces between rooks, grep lines with at least one rook, then count the number of spaces.
R +R
$0$0
G`R
Try it online!
Though the program uses spaces instead of periods, I added prefix code so that the test cases provided may be easily pasted in and used.
I was hoping I could use overlapping matches with (?<=R.*) | (?=.*R)
, but overlaps aren't quite that aggressive. It would need to count all possible ways a match could be obtained in order to return the correct result with that method.
$endgroup$
add a comment |
$begingroup$
Retina, 23 15 bytes
Double the number of spaces between rooks, grep lines with at least one rook, then count the number of spaces.
R +R
$0$0
G`R
Try it online!
Though the program uses spaces instead of periods, I added prefix code so that the test cases provided may be easily pasted in and used.
I was hoping I could use overlapping matches with (?<=R.*) | (?=.*R)
, but overlaps aren't quite that aggressive. It would need to count all possible ways a match could be obtained in order to return the correct result with that method.
$endgroup$
Retina, 23 15 bytes
Double the number of spaces between rooks, grep lines with at least one rook, then count the number of spaces.
R +R
$0$0
G`R
Try it online!
Though the program uses spaces instead of periods, I added prefix code so that the test cases provided may be easily pasted in and used.
I was hoping I could use overlapping matches with (?<=R.*) | (?=.*R)
, but overlaps aren't quite that aggressive. It would need to count all possible ways a match could be obtained in order to return the correct result with that method.
edited 4 hours ago
answered 6 hours ago
mbomb007mbomb007
18.6k5 gold badges47 silver badges120 bronze badges
18.6k5 gold badges47 silver badges120 bronze badges
add a comment |
add a comment |
$begingroup$
Jelly, 6 bytes
ṣ0§+ƝS
Try it online!
0
represent a rook, 1
represents an empty space.
$endgroup$
1
$begingroup$
If you use a space character for a Rook and another character for a space you can useẈ
to get the five:ḲẈ+ƝS
$endgroup$
– Jonathan Allan
5 hours ago
add a comment |
$begingroup$
Jelly, 6 bytes
ṣ0§+ƝS
Try it online!
0
represent a rook, 1
represents an empty space.
$endgroup$
1
$begingroup$
If you use a space character for a Rook and another character for a space you can useẈ
to get the five:ḲẈ+ƝS
$endgroup$
– Jonathan Allan
5 hours ago
add a comment |
$begingroup$
Jelly, 6 bytes
ṣ0§+ƝS
Try it online!
0
represent a rook, 1
represents an empty space.
$endgroup$
Jelly, 6 bytes
ṣ0§+ƝS
Try it online!
0
represent a rook, 1
represents an empty space.
edited 7 hours ago
answered 7 hours ago
Erik the OutgolferErik the Outgolfer
36k4 gold badges30 silver badges113 bronze badges
36k4 gold badges30 silver badges113 bronze badges
1
$begingroup$
If you use a space character for a Rook and another character for a space you can useẈ
to get the five:ḲẈ+ƝS
$endgroup$
– Jonathan Allan
5 hours ago
add a comment |
1
$begingroup$
If you use a space character for a Rook and another character for a space you can useẈ
to get the five:ḲẈ+ƝS
$endgroup$
– Jonathan Allan
5 hours ago
1
1
$begingroup$
If you use a space character for a Rook and another character for a space you can use
Ẉ
to get the five: ḲẈ+ƝS
$endgroup$
– Jonathan Allan
5 hours ago
$begingroup$
If you use a space character for a Rook and another character for a space you can use
Ẉ
to get the five: ḲẈ+ƝS
$endgroup$
– Jonathan Allan
5 hours ago
add a comment |
$begingroup$
Japt, 6 bytes
²x ¸ÊÉ
Try it online
$endgroup$
add a comment |
$begingroup$
Japt, 6 bytes
²x ¸ÊÉ
Try it online
$endgroup$
add a comment |
$begingroup$
Japt, 6 bytes
²x ¸ÊÉ
Try it online
$endgroup$
Japt, 6 bytes
²x ¸ÊÉ
Try it online
answered 5 hours ago
OliverOliver
6,6002 gold badges9 silver badges36 bronze badges
6,6002 gold badges9 silver badges36 bronze badges
add a comment |
add a comment |
$begingroup$
Japt, 6 bytes
Spaces for spaces, any other character for rooks.
²x ¸ÊÉ
Try it
$endgroup$
$begingroup$
Beat ya by 10 minutes :P
$endgroup$
– Oliver
5 hours ago
add a comment |
$begingroup$
Japt, 6 bytes
Spaces for spaces, any other character for rooks.
²x ¸ÊÉ
Try it
$endgroup$
$begingroup$
Beat ya by 10 minutes :P
$endgroup$
– Oliver
5 hours ago
add a comment |
$begingroup$
Japt, 6 bytes
Spaces for spaces, any other character for rooks.
²x ¸ÊÉ
Try it
$endgroup$
Japt, 6 bytes
Spaces for spaces, any other character for rooks.
²x ¸ÊÉ
Try it
answered 5 hours ago
ShaggyShaggy
21.3k3 gold badges21 silver badges72 bronze badges
21.3k3 gold badges21 silver badges72 bronze badges
$begingroup$
Beat ya by 10 minutes :P
$endgroup$
– Oliver
5 hours ago
add a comment |
$begingroup$
Beat ya by 10 minutes :P
$endgroup$
– Oliver
5 hours ago
$begingroup$
Beat ya by 10 minutes :P
$endgroup$
– Oliver
5 hours ago
$begingroup$
Beat ya by 10 minutes :P
$endgroup$
– Oliver
5 hours ago
add a comment |
$begingroup$
Stax, 7 bytes
âE¶ƒÅ╒±
Run and debug it
$endgroup$
add a comment |
$begingroup$
Stax, 7 bytes
âE¶ƒÅ╒±
Run and debug it
$endgroup$
add a comment |
$begingroup$
Stax, 7 bytes
âE¶ƒÅ╒±
Run and debug it
$endgroup$
Stax, 7 bytes
âE¶ƒÅ╒±
Run and debug it
answered 5 hours ago
OliverOliver
6,6002 gold badges9 silver badges36 bronze badges
6,6002 gold badges9 silver badges36 bronze badges
add a comment |
add a comment |
$begingroup$
Wolfram Language (Mathematica), 43 38 bytes
Count[Subsequences@#,{0,1..}|{1..,0}]&
Try it online!
Port of Neil's Retina solution. Uses 1 for spaces and 0 for rooks.
$endgroup$
add a comment |
$begingroup$
Wolfram Language (Mathematica), 43 38 bytes
Count[Subsequences@#,{0,1..}|{1..,0}]&
Try it online!
Port of Neil's Retina solution. Uses 1 for spaces and 0 for rooks.
$endgroup$
add a comment |
$begingroup$
Wolfram Language (Mathematica), 43 38 bytes
Count[Subsequences@#,{0,1..}|{1..,0}]&
Try it online!
Port of Neil's Retina solution. Uses 1 for spaces and 0 for rooks.
$endgroup$
Wolfram Language (Mathematica), 43 38 bytes
Count[Subsequences@#,{0,1..}|{1..,0}]&
Try it online!
Port of Neil's Retina solution. Uses 1 for spaces and 0 for rooks.
edited 5 hours ago
answered 7 hours ago
attinatattinat
2,2272 silver badges10 bronze badges
2,2272 silver badges10 bronze badges
add a comment |
add a comment |
$begingroup$
05AB1E, 5 bytes
«ðÚð¢
Try it online!
« # concatenate the input with itself
ðÚ # remove leading and trailing spaces
ð¢ # count spaces
$endgroup$
add a comment |
$begingroup$
05AB1E, 5 bytes
«ðÚð¢
Try it online!
« # concatenate the input with itself
ðÚ # remove leading and trailing spaces
ð¢ # count spaces
$endgroup$
add a comment |
$begingroup$
05AB1E, 5 bytes
«ðÚð¢
Try it online!
« # concatenate the input with itself
ðÚ # remove leading and trailing spaces
ð¢ # count spaces
$endgroup$
05AB1E, 5 bytes
«ðÚð¢
Try it online!
« # concatenate the input with itself
ðÚ # remove leading and trailing spaces
ð¢ # count spaces
answered 4 hours ago
GrimyGrimy
6,02915 silver badges31 bronze badges
6,02915 silver badges31 bronze badges
add a comment |
add a comment |
$begingroup$
C# (Visual C# Interactive Compiler), 28 bytes
x=>(x+x).Trim().Sum(d=>d)/32
Try it online!
$endgroup$
add a comment |
$begingroup$
C# (Visual C# Interactive Compiler), 28 bytes
x=>(x+x).Trim().Sum(d=>d)/32
Try it online!
$endgroup$
add a comment |
$begingroup$
C# (Visual C# Interactive Compiler), 28 bytes
x=>(x+x).Trim().Sum(d=>d)/32
Try it online!
$endgroup$
C# (Visual C# Interactive Compiler), 28 bytes
x=>(x+x).Trim().Sum(d=>d)/32
Try it online!
edited 4 hours ago
answered 4 hours ago
Embodiment of IgnoranceEmbodiment of Ignorance
5,1561 silver badge30 bronze badges
5,1561 silver badge30 bronze badges
add a comment |
add a comment |
$begingroup$
Japt, 5 bytes
²x èS
Try it
²x èS Implicit input string of U
² U + U
x Remove trailing and leading whitespace
èS Number of spaces
$endgroup$
add a comment |
$begingroup$
Japt, 5 bytes
²x èS
Try it
²x èS Implicit input string of U
² U + U
x Remove trailing and leading whitespace
èS Number of spaces
$endgroup$
add a comment |
$begingroup$
Japt, 5 bytes
²x èS
Try it
²x èS Implicit input string of U
² U + U
x Remove trailing and leading whitespace
èS Number of spaces
$endgroup$
Japt, 5 bytes
²x èS
Try it
²x èS Implicit input string of U
² U + U
x Remove trailing and leading whitespace
èS Number of spaces
answered 4 hours ago
Embodiment of IgnoranceEmbodiment of Ignorance
5,1561 silver badge30 bronze badges
5,1561 silver badge30 bronze badges
add a comment |
add a comment |
$begingroup$
Haskell, 68 58 54 bytes
(n#y)(a:x)|a<1=n+(0#1)x|m<-n+1=y+(m#y)x
(_#_)x=0
f=0#0
Try it online!
$endgroup$
add a comment |
$begingroup$
Haskell, 68 58 54 bytes
(n#y)(a:x)|a<1=n+(0#1)x|m<-n+1=y+(m#y)x
(_#_)x=0
f=0#0
Try it online!
$endgroup$
add a comment |
$begingroup$
Haskell, 68 58 54 bytes
(n#y)(a:x)|a<1=n+(0#1)x|m<-n+1=y+(m#y)x
(_#_)x=0
f=0#0
Try it online!
$endgroup$
Haskell, 68 58 54 bytes
(n#y)(a:x)|a<1=n+(0#1)x|m<-n+1=y+(m#y)x
(_#_)x=0
f=0#0
Try it online!
edited 3 hours ago
answered 3 hours ago
Sriotchilism O'ZaicSriotchilism O'Zaic
37.5k10 gold badges168 silver badges382 bronze badges
37.5k10 gold badges168 silver badges382 bronze badges
add a comment |
add a comment |
$begingroup$
C, 181 bytes
#include<stdio.h>
#include<string.h>
main(a,c,d,e){c=a=d=0;char*b=calloc(99,1);gets(b);do{if(b[a]=='R'){c+=e*(1+d);e=0;d=1;}if(b[a]=='.')e++;a++;}while(b[a]);c+=e;printf("%i",c);}
New contributor
$endgroup$
add a comment |
$begingroup$
C, 181 bytes
#include<stdio.h>
#include<string.h>
main(a,c,d,e){c=a=d=0;char*b=calloc(99,1);gets(b);do{if(b[a]=='R'){c+=e*(1+d);e=0;d=1;}if(b[a]=='.')e++;a++;}while(b[a]);c+=e;printf("%i",c);}
New contributor
$endgroup$
add a comment |
$begingroup$
C, 181 bytes
#include<stdio.h>
#include<string.h>
main(a,c,d,e){c=a=d=0;char*b=calloc(99,1);gets(b);do{if(b[a]=='R'){c+=e*(1+d);e=0;d=1;}if(b[a]=='.')e++;a++;}while(b[a]);c+=e;printf("%i",c);}
New contributor
$endgroup$
C, 181 bytes
#include<stdio.h>
#include<string.h>
main(a,c,d,e){c=a=d=0;char*b=calloc(99,1);gets(b);do{if(b[a]=='R'){c+=e*(1+d);e=0;d=1;}if(b[a]=='.')e++;a++;}while(b[a]);c+=e;printf("%i",c);}
New contributor
New contributor
answered 14 mins ago
user1475369user1475369
1
1
New contributor
New contributor
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%2f191180%2fcount-rook-moves-1d%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