# Everyone Ought to Have a Friend

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.

• 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.

• You may take input in any reasonable format. This includes a list of lines.

## Test Cases

Ybb
YH%
%%%%


$1$

Aaaab
uuu
yyybbb


$2$

A


$1$

qqWWaaww


$0$

• Is an empty string valid input and if so would it score 0? Also how flexible is input type? List of lines fine? – Veskah Sep 13 '18 at 21:53
• Can newlines be isolated? – Jo King Sep 13 '18 at 21:55
• @DimChtz Because it has a Y below it. – Erik the Outgolfer Sep 13 '18 at 21:58
• The first metric can be circumvented in any programming language and every answer has a 0 score now. – G B Sep 14 '18 at 10:22
• @GB Indeed. I guess it's not too late for turning it into a restricted-source challenge instead and disallow isolated characters altogether. – Arnauld Sep 14 '18 at 10:56

# Python 2, 0 (350344314309301298 291 bytes)

def f(tt):11
def f(tt):
tt=tt.split('\n')
r =0#.split('\n')
#r  0#
#for
for  ii,ll in enumerate(tt):
for jj,cc in enumerate(ll):
##for+=1-(
r+=1-(cc in ll[(jj or 2)-1:jj+2:2]    +''.join(ll[jj:
jj+1]for ll in tt[(ii or 2)-1:ii+2:2]))##+''.join(ll[jj:
#  +1]for
#print r
print r



Try it online!

-7 bytes, thanks to Jo King

• 291 bytes – Jo King Sep 14 '18 at 11:54
• @JoKing Thanks! :) – TFeld Sep 14 '18 at 12:28

# Clean, 0 (439 ... 415 bytes)

-11 thanks to Ørjan Johansen

Finally a challenge where I can score 0 with Clean!
(and normally it's bad at source-layout challenges!)

//module
module d
import StdEnv,ArgEnv,Data.List,Data.Maybe
import StdEnv,ArgEnv,Data.List,Data.Maybe
Start=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=sum[1\\i<-l&v<-[0..],_<-i&u<-[0..]|all((<>)(?u v))[?(u-1)v,?(u+1)v,?u(v-1),?u(v+1)]]
l=mklines[c\\c<-:getCommandLine.[1]]
l=mklines[c\\c<-:getCommandLine.[1]]
?x=mapMaybe(\k=k!?x)o(!?)l
?x=mapMaybe(\k=k!?x)o(!?)l


Try it online!

The TIO link uses module main due to the way Clean is implemented on TIO, but module d will work if you name the file d.icl instead of main.icl as TIO does.

One of the old lines explained (new version is the same thing in a different order):

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
]

• Not using let saves 11 bytes. – Ørjan Johansen Sep 14 '18 at 5:35
• @ØrjanJohansen Thanks! I also changed the module header since we have an adjacent d handy – Οurous Sep 14 '18 at 5:37

# JavaScript (ES6), 0 (154 bytes)

Saved 2 4 bytes thanks to @ØrjanJohansen

Takes input as an array of strings.

s  =>
s//=>(s,y,a
.map((s,y,a)=>[...s]
.map((c,x  )=>[...s]
&&//++c,x
s[x+1]==c|
s[x-1]==c|
(a[y-1]||0)[x]==c|
(a[y+1]||0)[x]==c||
i++),i=0)
&&i//),i=


Try it online!

# Jelly, 0 (41 27 25 bytes)

ŒĠạþ€Ẏ§CẠ€S
ŒĠạþ€Ẏ§CẠ€S


Try it online!

Takes input as list of lines. The first line in the code 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


# 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'}.

# Python 3, 0 (323 bytes)

def f(s,e=enumerate):S={(x,y,c)for y,l in e(s.split("\n"))for x,c in e(l)};return-sum(~-any((x+u,y+v,c)in S for u,v in[(1,0),(~0,0),(0,~0),(0,1)])for x,y,c in S)
def f(s,e=enumerate):S={(x,y,c)for y,l in e(s.split("\n"))for x,c in e(l)};return-sum(~-any((x+u,y+v,c)in S for u,v in[(1,0),(~0,0),(0,~0),(0,1)])for x,y,c in S)


Try it online!

# 05AB1E, 0 (101 bytes)

žGçU|€SXζζD"εγεDgDisëXи]"©.V˜sø®.V€Sø˜‚øʒË}ʒXå≠}gq


Try it online.

This is one of the ugliest and longest 05AB1E programs I've ever written.. >.> This challenge is deceivingly hard in 05AB1E. I have no doubt in mind the byte-count can at least be halved or even three/four times as small by using a different approach (or even with a similar approach), but I currently don't see how. I'm just glad it's working right now.. If someone else posts a much shorter 05AB1E answer with some smart tricks I'll probably delete this answer out of shame... xD

Explanation:

žGç                # Character with unicode 32768 ('耀')
U               # Pop and store it in variable X
# (This character is not part of the printable ASCII, nor of my 05AB1E code)
|                  # Take the multi-line input as list
#  i.e. "Ybb\nYH%\n%%%%" → ["Ybb","YH%","%%%%"]
€S                # Convert each string to a list of characters
#  i.e. ["Ybb","YH%","%%%%"] → [["Y","b","b"],["Y","H","%"],["%","%","%","%"]]
Xζζ             # Zip with character X as filler twice to make the lines of equal length
#  i.e. [["Y","b","b"],["Y","H","%"],["%","%","%","%"]]
#   → [["Y","b","b","耀"],["Y","H","%","耀"],["%","%","%","%"]]
D            # Duplicate this list
"             "    # Create a string
©   # Which we store in the register (without popping)
.V # And execute that string as 05AB1E code
ε                 #  Map each inner list to:
γ                #   Split in chunks of the same characters
#    i.e. [["Y","b","b"],["Y","H","%"],["%","%","%","%"]]
#     → [[["Y"],["b","b"]],[["Y"],["H"],["%"]],[["%","%","%","%"]]]
ε               #   Map each of those to:
D              #    Duplicate the current inner list
gDi           #    If its length is exactly 1:
s          #     Swap so the mapping keeps the duplicated single character (as list)
ë           #    Else:
Xи         #     Take character X repeated the length amount of times
#      i.e. ["%","%","%","%"] (length 4) → ["耀","耀","耀","耀"]
]        #  Close the if-else and both maps
˜       #  Flatten the list to a single list of characters
#   i.e. [[["Y"],["耀","耀"],["耀"]],[["Y"],["H"],["%"],["耀"]],[["耀","耀","耀","耀"]]]
#    → ["Y","耀","耀","耀","Y","H","%","耀","耀","耀","耀","耀"]
s                  # Swap so the duplicate list is at the top of the stack
ø                 # Swap its rows and columns
#  i.e. [["Y","b","b","耀"],["Y","H","%","耀"],["%","%","%","%"]]
#   → [["Y","Y","%"],["b","H","%"],["b","%","%"],["耀","耀","%"]]
®.V              # Execute the same piece of code again that we've stored in the register
€S            # Convert each to a list of characters
#  i.e. [[["耀","耀"],["%"]],[["b"],["H"],["%"]],[["b"],["耀","耀"]],[["耀","耀"],["%"]]]
#   → [["耀","耀","%"],["b","H","%"],["b","耀","耀"],["耀","耀","%"]]
ø           # Swap its rows and columns back again
#  i.e. [["耀","b","b","耀"],["耀","H","耀","耀"],["%","%","耀","%"]]
˜          # Flatten this list as well
‚                  # Pair both lists together
#  i.e. [["Y","耀","耀","耀","Y","H","%","耀","耀","耀","耀","耀"],
#        ["耀","b","b","耀","耀","H","耀","耀","%","%","耀","%"]]
ø                 # Swap its rows and columns to create pairs
#  i.e. [["Y","耀"],["耀","b"],["耀","b"],["耀","耀"],["Y","耀"],["H","H"],["%","耀"],["耀","耀"],["耀","%"],["耀","%"],["耀","耀"],["耀","%"]]
ʒË}              # Filter out any inner lists where both characters are not equal
#  i.e. [["耀","耀"],["H","H"],["耀","耀"],["耀","耀"]]
ʒXå≠}         # Filter out any inner lists that contain the character X
#  i.e. [["H","H"]]
g                  # Take the length as result
#  i.e. [["H","H"]] → 1
q                 # Stop the program, making all other characters no-ops
# (and output the length above implicitly)


# Ruby, score 0, 237 209 bytes

##->a{['',*a,''].each_cons(3).sum{|a,b,c|(0..b.size).count{|x|[[x>0&&b[x-1],a[x],b[x+1],c[x]]&[b[x
->a{['',*a,''].each_cons(3).sum{|a,b,c|(0..b.size).count{|x|[[x>0&&b[x-1],a[x],b[x+1],c[x]]&[b[x]]]==[[]]}}}


Try it online!

# JavaScript (Node.js), 0 (279 bytes)

  s=>(b=s.map(Buffer)).map((x,i)=>x.filter((y,j)=>y-(g=(x,y)=>~~(b[x]&&b[x][y]))(i,j-1)&&y-g(i,j+1)&&y-g(i-1,j)&&y-g(i+1,j))).join.length
//s=>(b=s.map(Buffer)).map((x,i)=>x.filter((y,j)=>y-(g=(x,y)=>~~(b[x]&&b[x][y]))(i,j-1)&&y-g(i,j+1)&&y-g(i-1,j)&&y-g(i+1,j))).join.length


Try it online!

Receive input as array of lines.