Everyone Ought to Have a Friend
Clash Royale CLAN TAG#URR8PPP
up vote
7
down vote
favorite
An isolated character is a character (other than a newline) that doesn't have an adjacent character of the same type. Adjacent characters can be to the left, the right above or below, but not diagonals. For example in the following text H
is isolated:
Ybb
YH%
%%%%
All the other characters are not isolated because each of them has at least one other character of the same type adjacent.
Your task is to write a program that takes a string as input and determines the number of isolated characters.
Scoring
You answer will be scored by two metrics. The first is the number of isolated characters in your program. You should aim to minimize this. The second will be the number of bytes in your program. You should minimize this as well. Program size will act as a tie breaker for the first criterion.
Additional Rules
You should support input on the printable ascii range plus any characters you use in your program.
You may consider a line break to either be a newline character or a newline followed by a line feed.
Test Cases
Ybb
YH%
%%%%
$1$
Aaaab
uuu
yyybbb
$2$
A
$1$
qqWWaaww
$0$
string code-challenge source-layout
 |Â
show 2 more comments
up vote
7
down vote
favorite
An isolated character is a character (other than a newline) that doesn't have an adjacent character of the same type. Adjacent characters can be to the left, the right above or below, but not diagonals. For example in the following text H
is isolated:
Ybb
YH%
%%%%
All the other characters are not isolated because each of them has at least one other character of the same type adjacent.
Your task is to write a program that takes a string as input and determines the number of isolated characters.
Scoring
You answer will be scored by two metrics. The first is the number of isolated characters in your program. You should aim to minimize this. The second will be the number of bytes in your program. You should minimize this as well. Program size will act as a tie breaker for the first criterion.
Additional Rules
You should support input on the printable ascii range plus any characters you use in your program.
You may consider a line break to either be a newline character or a newline followed by a line feed.
Test Cases
Ybb
YH%
%%%%
$1$
Aaaab
uuu
yyybbb
$2$
A
$1$
qqWWaaww
$0$
string code-challenge source-layout
1
Is an empty string valid input and if so would it score 0? Also how flexible is input type? List of lines fine?
â Veskah
3 hours ago
Can newlines be isolated?
â Jo King
3 hours ago
1
@DimChtz Because it has aY
below it.
â Erik the Outgolfer
3 hours ago
@JoKing No it cannot.
â W W
3 hours ago
What do you mean by type? Is it corresponding to the character's codepoint?
â AlexRacer
3 hours ago
 |Â
show 2 more comments
up vote
7
down vote
favorite
up vote
7
down vote
favorite
An isolated character is a character (other than a newline) that doesn't have an adjacent character of the same type. Adjacent characters can be to the left, the right above or below, but not diagonals. For example in the following text H
is isolated:
Ybb
YH%
%%%%
All the other characters are not isolated because each of them has at least one other character of the same type adjacent.
Your task is to write a program that takes a string as input and determines the number of isolated characters.
Scoring
You answer will be scored by two metrics. The first is the number of isolated characters in your program. You should aim to minimize this. The second will be the number of bytes in your program. You should minimize this as well. Program size will act as a tie breaker for the first criterion.
Additional Rules
You should support input on the printable ascii range plus any characters you use in your program.
You may consider a line break to either be a newline character or a newline followed by a line feed.
Test Cases
Ybb
YH%
%%%%
$1$
Aaaab
uuu
yyybbb
$2$
A
$1$
qqWWaaww
$0$
string code-challenge source-layout
An isolated character is a character (other than a newline) that doesn't have an adjacent character of the same type. Adjacent characters can be to the left, the right above or below, but not diagonals. For example in the following text H
is isolated:
Ybb
YH%
%%%%
All the other characters are not isolated because each of them has at least one other character of the same type adjacent.
Your task is to write a program that takes a string as input and determines the number of isolated characters.
Scoring
You answer will be scored by two metrics. The first is the number of isolated characters in your program. You should aim to minimize this. The second will be the number of bytes in your program. You should minimize this as well. Program size will act as a tie breaker for the first criterion.
Additional Rules
You should support input on the printable ascii range plus any characters you use in your program.
You may consider a line break to either be a newline character or a newline followed by a line feed.
Test Cases
Ybb
YH%
%%%%
$1$
Aaaab
uuu
yyybbb
$2$
A
$1$
qqWWaaww
$0$
string code-challenge source-layout
string code-challenge source-layout
edited 3 hours ago
asked 4 hours ago
W W
33.6k10148348
33.6k10148348
1
Is an empty string valid input and if so would it score 0? Also how flexible is input type? List of lines fine?
â Veskah
3 hours ago
Can newlines be isolated?
â Jo King
3 hours ago
1
@DimChtz Because it has aY
below it.
â Erik the Outgolfer
3 hours ago
@JoKing No it cannot.
â W W
3 hours ago
What do you mean by type? Is it corresponding to the character's codepoint?
â AlexRacer
3 hours ago
 |Â
show 2 more comments
1
Is an empty string valid input and if so would it score 0? Also how flexible is input type? List of lines fine?
â Veskah
3 hours ago
Can newlines be isolated?
â Jo King
3 hours ago
1
@DimChtz Because it has aY
below it.
â Erik the Outgolfer
3 hours ago
@JoKing No it cannot.
â W W
3 hours ago
What do you mean by type? Is it corresponding to the character's codepoint?
â AlexRacer
3 hours ago
1
1
Is an empty string valid input and if so would it score 0? Also how flexible is input type? List of lines fine?
â Veskah
3 hours ago
Is an empty string valid input and if so would it score 0? Also how flexible is input type? List of lines fine?
â Veskah
3 hours ago
Can newlines be isolated?
â Jo King
3 hours ago
Can newlines be isolated?
â Jo King
3 hours ago
1
1
@DimChtz Because it has a
Y
below it.â Erik the Outgolfer
3 hours ago
@DimChtz Because it has a
Y
below it.â Erik the Outgolfer
3 hours ago
@JoKing No it cannot.
â W W
3 hours ago
@JoKing No it cannot.
â W W
3 hours ago
What do you mean by type? Is it corresponding to the character's codepoint?
â AlexRacer
3 hours ago
What do you mean by type? Is it corresponding to the character's codepoint?
â AlexRacer
3 hours ago
 |Â
show 2 more comments
3 Answers
3
active
oldest
votes
up vote
1
down vote
Clean, 0 (439 433 bytes)
Finally a challenge where I can score 0 with Clean!
(and normally it's bad at source-layout challenges!)
//module m
module m
import StdEnv,ArgEnv,Data.List,Data.Maybe
import StdEnv,ArgEnv,Data.List,Data.Maybe
Start=let l=mklines[c\c<-:getCommandLine.[1]];?x y=mapMaybe(k=k!?x)(l!?y)in sum[1\i<-l&v<-[0..],_<-i&u<-[0..]|all((<>)(?u v))[?(u-1)v,?(u+1)v,?u(v-1),?u(v+1)]]
Start=let l=mklines[c\c<-:getCommandLine.[1]];?x y=mapMaybe(k=k!?x)(l!?y)in sum[1\i<-l&v<-[0..],_<-i&u<-[0..]|all((<>)(?u v))[?(u-1)v,?(u+1)v,?u(v-1),?u(v+1)]]
Try it online!
The TIO link uses module main
due to the way Clean is implemented on TIO, but module m
will work if you name the file m.icl
instead of main.icl
as TIO does.
One of the lines explained:
Start // entry point
= let // define locals
l = mklines // `l` is argument split at newlines
[c \ c <-: getCommandLine.[1]]; // the second command-line arg turned into a [Char]
? x y // function ? of coordinate (x,y)
= mapMaybe // if the argument isn't Nothing
(k = k!?x) // try taking the `x`-th index
(l!?y) // of the `y`-th index of `l`
in // in the context of
sum [ // the sum of
1 // the integer one
\ i <- l & v <- [0..] // for every index in `l`
, _ <- i & u <- [0..] // for every subindex in `l`
| all ( // where all of the second argument
(<>)(?u v) // doesn't equal the first argument
) [?(u-1)v, ?(u+1)v, ?u(v-1), ?u(v+1)] // over every adjacent element
]
add a comment |Â
up vote
0
down vote
MATL, 0 (54 bytes)
TTTTf"Go@X!0JQJ&(d@_X!]***ggss
%%%%f"Go@X!0JQJ&(d@_X!]
Input is a cell array of strings, one for each line: 'line 1', 'line 2', 'and line 3'
.
Try it online! Or verify test cases and source code.
add a comment |Â
up vote
0
down vote
Jelly, 0 (41 27 bytes)
Ã
Âàạþ`â¬áºÂç1eâ±®CS
Ã
Âàạþ`â¬áºÂç1eâ±®CS
Try it online!
Takes input as list of lines. The first line in the code is never does anything and is only there to minimize isolated characters.
á»´Ã
Âàạþ`â¬áºÂç1eâ±®CS
á»´ Split the text on newlines.
Ã
ÂÃÂ Group the multidimensional indices by their value.
⬠For each list of indices:
ạ Take the absolute difference...
þ` ...between each pair.
ẠConcatenate the lists of differences.
ç Sum the x & y differences. This computes the Manhattan distance.
At this point we have a list for each character in the text of
Manhattan distances between it and it's identical characters.
1eâ±® Is there a 1 in each of the lists? None for isolated characters.
C Complement: 0 <-> 1.
S Sum. Counts the isolated characters
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
Clean, 0 (439 433 bytes)
Finally a challenge where I can score 0 with Clean!
(and normally it's bad at source-layout challenges!)
//module m
module m
import StdEnv,ArgEnv,Data.List,Data.Maybe
import StdEnv,ArgEnv,Data.List,Data.Maybe
Start=let l=mklines[c\c<-:getCommandLine.[1]];?x y=mapMaybe(k=k!?x)(l!?y)in sum[1\i<-l&v<-[0..],_<-i&u<-[0..]|all((<>)(?u v))[?(u-1)v,?(u+1)v,?u(v-1),?u(v+1)]]
Start=let l=mklines[c\c<-:getCommandLine.[1]];?x y=mapMaybe(k=k!?x)(l!?y)in sum[1\i<-l&v<-[0..],_<-i&u<-[0..]|all((<>)(?u v))[?(u-1)v,?(u+1)v,?u(v-1),?u(v+1)]]
Try it online!
The TIO link uses module main
due to the way Clean is implemented on TIO, but module m
will work if you name the file m.icl
instead of main.icl
as TIO does.
One of the lines explained:
Start // entry point
= let // define locals
l = mklines // `l` is argument split at newlines
[c \ c <-: getCommandLine.[1]]; // the second command-line arg turned into a [Char]
? x y // function ? of coordinate (x,y)
= mapMaybe // if the argument isn't Nothing
(k = k!?x) // try taking the `x`-th index
(l!?y) // of the `y`-th index of `l`
in // in the context of
sum [ // the sum of
1 // the integer one
\ i <- l & v <- [0..] // for every index in `l`
, _ <- i & u <- [0..] // for every subindex in `l`
| all ( // where all of the second argument
(<>)(?u v) // doesn't equal the first argument
) [?(u-1)v, ?(u+1)v, ?u(v-1), ?u(v+1)] // over every adjacent element
]
add a comment |Â
up vote
1
down vote
Clean, 0 (439 433 bytes)
Finally a challenge where I can score 0 with Clean!
(and normally it's bad at source-layout challenges!)
//module m
module m
import StdEnv,ArgEnv,Data.List,Data.Maybe
import StdEnv,ArgEnv,Data.List,Data.Maybe
Start=let l=mklines[c\c<-:getCommandLine.[1]];?x y=mapMaybe(k=k!?x)(l!?y)in sum[1\i<-l&v<-[0..],_<-i&u<-[0..]|all((<>)(?u v))[?(u-1)v,?(u+1)v,?u(v-1),?u(v+1)]]
Start=let l=mklines[c\c<-:getCommandLine.[1]];?x y=mapMaybe(k=k!?x)(l!?y)in sum[1\i<-l&v<-[0..],_<-i&u<-[0..]|all((<>)(?u v))[?(u-1)v,?(u+1)v,?u(v-1),?u(v+1)]]
Try it online!
The TIO link uses module main
due to the way Clean is implemented on TIO, but module m
will work if you name the file m.icl
instead of main.icl
as TIO does.
One of the lines explained:
Start // entry point
= let // define locals
l = mklines // `l` is argument split at newlines
[c \ c <-: getCommandLine.[1]]; // the second command-line arg turned into a [Char]
? x y // function ? of coordinate (x,y)
= mapMaybe // if the argument isn't Nothing
(k = k!?x) // try taking the `x`-th index
(l!?y) // of the `y`-th index of `l`
in // in the context of
sum [ // the sum of
1 // the integer one
\ i <- l & v <- [0..] // for every index in `l`
, _ <- i & u <- [0..] // for every subindex in `l`
| all ( // where all of the second argument
(<>)(?u v) // doesn't equal the first argument
) [?(u-1)v, ?(u+1)v, ?u(v-1), ?u(v+1)] // over every adjacent element
]
add a comment |Â
up vote
1
down vote
up vote
1
down vote
Clean, 0 (439 433 bytes)
Finally a challenge where I can score 0 with Clean!
(and normally it's bad at source-layout challenges!)
//module m
module m
import StdEnv,ArgEnv,Data.List,Data.Maybe
import StdEnv,ArgEnv,Data.List,Data.Maybe
Start=let l=mklines[c\c<-:getCommandLine.[1]];?x y=mapMaybe(k=k!?x)(l!?y)in sum[1\i<-l&v<-[0..],_<-i&u<-[0..]|all((<>)(?u v))[?(u-1)v,?(u+1)v,?u(v-1),?u(v+1)]]
Start=let l=mklines[c\c<-:getCommandLine.[1]];?x y=mapMaybe(k=k!?x)(l!?y)in sum[1\i<-l&v<-[0..],_<-i&u<-[0..]|all((<>)(?u v))[?(u-1)v,?(u+1)v,?u(v-1),?u(v+1)]]
Try it online!
The TIO link uses module main
due to the way Clean is implemented on TIO, but module m
will work if you name the file m.icl
instead of main.icl
as TIO does.
One of the lines explained:
Start // entry point
= let // define locals
l = mklines // `l` is argument split at newlines
[c \ c <-: getCommandLine.[1]]; // the second command-line arg turned into a [Char]
? x y // function ? of coordinate (x,y)
= mapMaybe // if the argument isn't Nothing
(k = k!?x) // try taking the `x`-th index
(l!?y) // of the `y`-th index of `l`
in // in the context of
sum [ // the sum of
1 // the integer one
\ i <- l & v <- [0..] // for every index in `l`
, _ <- i & u <- [0..] // for every subindex in `l`
| all ( // where all of the second argument
(<>)(?u v) // doesn't equal the first argument
) [?(u-1)v, ?(u+1)v, ?u(v-1), ?u(v+1)] // over every adjacent element
]
Clean, 0 (439 433 bytes)
Finally a challenge where I can score 0 with Clean!
(and normally it's bad at source-layout challenges!)
//module m
module m
import StdEnv,ArgEnv,Data.List,Data.Maybe
import StdEnv,ArgEnv,Data.List,Data.Maybe
Start=let l=mklines[c\c<-:getCommandLine.[1]];?x y=mapMaybe(k=k!?x)(l!?y)in sum[1\i<-l&v<-[0..],_<-i&u<-[0..]|all((<>)(?u v))[?(u-1)v,?(u+1)v,?u(v-1),?u(v+1)]]
Start=let l=mklines[c\c<-:getCommandLine.[1]];?x y=mapMaybe(k=k!?x)(l!?y)in sum[1\i<-l&v<-[0..],_<-i&u<-[0..]|all((<>)(?u v))[?(u-1)v,?(u+1)v,?u(v-1),?u(v+1)]]
Try it online!
The TIO link uses module main
due to the way Clean is implemented on TIO, but module m
will work if you name the file m.icl
instead of main.icl
as TIO does.
One of the lines explained:
Start // entry point
= let // define locals
l = mklines // `l` is argument split at newlines
[c \ c <-: getCommandLine.[1]]; // the second command-line arg turned into a [Char]
? x y // function ? of coordinate (x,y)
= mapMaybe // if the argument isn't Nothing
(k = k!?x) // try taking the `x`-th index
(l!?y) // of the `y`-th index of `l`
in // in the context of
sum [ // the sum of
1 // the integer one
\ i <- l & v <- [0..] // for every index in `l`
, _ <- i & u <- [0..] // for every subindex in `l`
| all ( // where all of the second argument
(<>)(?u v) // doesn't equal the first argument
) [?(u-1)v, ?(u+1)v, ?u(v-1), ?u(v+1)] // over every adjacent element
]
edited 3 hours ago
answered 3 hours ago
ÃÂurous
5,2831931
5,2831931
add a comment |Â
add a comment |Â
up vote
0
down vote
MATL, 0 (54 bytes)
TTTTf"Go@X!0JQJ&(d@_X!]***ggss
%%%%f"Go@X!0JQJ&(d@_X!]
Input is a cell array of strings, one for each line: 'line 1', 'line 2', 'and line 3'
.
Try it online! Or verify test cases and source code.
add a comment |Â
up vote
0
down vote
MATL, 0 (54 bytes)
TTTTf"Go@X!0JQJ&(d@_X!]***ggss
%%%%f"Go@X!0JQJ&(d@_X!]
Input is a cell array of strings, one for each line: 'line 1', 'line 2', 'and line 3'
.
Try it online! Or verify test cases and source code.
add a comment |Â
up vote
0
down vote
up vote
0
down vote
MATL, 0 (54 bytes)
TTTTf"Go@X!0JQJ&(d@_X!]***ggss
%%%%f"Go@X!0JQJ&(d@_X!]
Input is a cell array of strings, one for each line: 'line 1', 'line 2', 'and line 3'
.
Try it online! Or verify test cases and source code.
MATL, 0 (54 bytes)
TTTTf"Go@X!0JQJ&(d@_X!]***ggss
%%%%f"Go@X!0JQJ&(d@_X!]
Input is a cell array of strings, one for each line: 'line 1', 'line 2', 'and line 3'
.
Try it online! Or verify test cases and source code.
answered 2 hours ago
Luis Mendo
72.6k885284
72.6k885284
add a comment |Â
add a comment |Â
up vote
0
down vote
Jelly, 0 (41 27 bytes)
Ã
Âàạþ`â¬áºÂç1eâ±®CS
Ã
Âàạþ`â¬áºÂç1eâ±®CS
Try it online!
Takes input as list of lines. The first line in the code is never does anything and is only there to minimize isolated characters.
á»´Ã
Âàạþ`â¬áºÂç1eâ±®CS
á»´ Split the text on newlines.
Ã
ÂÃÂ Group the multidimensional indices by their value.
⬠For each list of indices:
ạ Take the absolute difference...
þ` ...between each pair.
ẠConcatenate the lists of differences.
ç Sum the x & y differences. This computes the Manhattan distance.
At this point we have a list for each character in the text of
Manhattan distances between it and it's identical characters.
1eâ±® Is there a 1 in each of the lists? None for isolated characters.
C Complement: 0 <-> 1.
S Sum. Counts the isolated characters
add a comment |Â
up vote
0
down vote
Jelly, 0 (41 27 bytes)
Ã
Âàạþ`â¬áºÂç1eâ±®CS
Ã
Âàạþ`â¬áºÂç1eâ±®CS
Try it online!
Takes input as list of lines. The first line in the code is never does anything and is only there to minimize isolated characters.
á»´Ã
Âàạþ`â¬áºÂç1eâ±®CS
á»´ Split the text on newlines.
Ã
ÂÃÂ Group the multidimensional indices by their value.
⬠For each list of indices:
ạ Take the absolute difference...
þ` ...between each pair.
ẠConcatenate the lists of differences.
ç Sum the x & y differences. This computes the Manhattan distance.
At this point we have a list for each character in the text of
Manhattan distances between it and it's identical characters.
1eâ±® Is there a 1 in each of the lists? None for isolated characters.
C Complement: 0 <-> 1.
S Sum. Counts the isolated characters
add a comment |Â
up vote
0
down vote
up vote
0
down vote
Jelly, 0 (41 27 bytes)
Ã
Âàạþ`â¬áºÂç1eâ±®CS
Ã
Âàạþ`â¬áºÂç1eâ±®CS
Try it online!
Takes input as list of lines. The first line in the code is never does anything and is only there to minimize isolated characters.
á»´Ã
Âàạþ`â¬áºÂç1eâ±®CS
á»´ Split the text on newlines.
Ã
ÂÃÂ Group the multidimensional indices by their value.
⬠For each list of indices:
ạ Take the absolute difference...
þ` ...between each pair.
ẠConcatenate the lists of differences.
ç Sum the x & y differences. This computes the Manhattan distance.
At this point we have a list for each character in the text of
Manhattan distances between it and it's identical characters.
1eâ±® Is there a 1 in each of the lists? None for isolated characters.
C Complement: 0 <-> 1.
S Sum. Counts the isolated characters
Jelly, 0 (41 27 bytes)
Ã
Âàạþ`â¬áºÂç1eâ±®CS
Ã
Âàạþ`â¬áºÂç1eâ±®CS
Try it online!
Takes input as list of lines. The first line in the code is never does anything and is only there to minimize isolated characters.
á»´Ã
Âàạþ`â¬áºÂç1eâ±®CS
á»´ Split the text on newlines.
Ã
ÂÃÂ Group the multidimensional indices by their value.
⬠For each list of indices:
ạ Take the absolute difference...
þ` ...between each pair.
ẠConcatenate the lists of differences.
ç Sum the x & y differences. This computes the Manhattan distance.
At this point we have a list for each character in the text of
Manhattan distances between it and it's identical characters.
1eâ±® Is there a 1 in each of the lists? None for isolated characters.
C Complement: 0 <-> 1.
S Sum. Counts the isolated characters
edited 23 mins ago
answered 3 hours ago
dylnan
3,4982526
3,4982526
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%2f172146%2feveryone-ought-to-have-a-friend%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
1
Is an empty string valid input and if so would it score 0? Also how flexible is input type? List of lines fine?
â Veskah
3 hours ago
Can newlines be isolated?
â Jo King
3 hours ago
1
@DimChtz Because it has a
Y
below it.â Erik the Outgolfer
3 hours ago
@JoKing No it cannot.
â W W
3 hours ago
What do you mean by type? Is it corresponding to the character's codepoint?
â AlexRacer
3 hours ago