Setiap Orang Seharusnya Memiliki Teman

11

Karakter yang terisolasi adalah karakter (selain baris baru) yang tidak memiliki karakter yang berdekatan dengan tipe yang sama. Karakter yang berdekatan dapat ke kiri, kanan di atas atau di bawah, tetapi tidak diagonal. Misalnya dalam teks berikut Hini diisolasi:

Ybb
YH%
%%%%

Semua karakter lain tidak terisolasi karena masing-masing memiliki setidaknya satu karakter lain yang bertipe sama.

Tugas Anda adalah menulis program yang mengambil string sebagai input dan menentukan jumlah karakter yang terisolasi.

Mencetak gol

Jawaban Anda akan dicetak oleh dua metrik. Yang pertama adalah jumlah karakter yang terisolasi dalam program Anda. Anda harus berusaha meminimalkan ini. Yang kedua adalah jumlah byte dalam program Anda. Anda harus meminimalkan ini juga. Ukuran program akan bertindak sebagai pemutus dasi untuk kriteria pertama.

Aturan tambahan

  • Anda harus mendukung input pada rentang ascii yang dapat dicetak ditambah karakter apa pun yang Anda gunakan dalam program Anda.

  • Anda dapat mempertimbangkan jeda baris untuk menjadi karakter baris baru atau baris baru diikuti oleh umpan baris.

  • Anda dapat mengambil input dalam format apa pun yang masuk akal. Ini termasuk daftar garis.

Uji Kasus

Ybb
YH%
%%%%

1


Aaaab
uuu
yyybbb

2


A

1


qqWWaaww

0

Ad Hoc Garf Hunter
sumber
2
Apakah input string kosong valid dan jika demikian apakah skor 0? Juga seberapa fleksibel jenis input? Daftar garis baik-baik saja?
Veskah
Bisakah baris baru diisolasi?
Jo King
1
@DimChtz Karena ada Ydi bawahnya.
Erik the Outgolfer
1
Metrik pertama dapat dielakkan dalam bahasa pemrograman apa pun dan setiap jawaban memiliki skor 0 sekarang.
GB
1
@ GB Memang. Saya kira itu belum terlambat untuk mengubahnya menjadi restricted-sourcetantangan dan tidak mengizinkan karakter yang terisolasi sama sekali.
Arnauld

Jawaban:

7

Python 2 , 0 ( 350 344 314 309 301 298 291 byte)

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
 

Cobalah online!

-7 byte, terima kasih kepada Jo King

TFeld
sumber
291 bytes
Jo King
@JoKing, terima kasih! :)
TFeld
5

Bersih , 0 ( 439 ... 415 byte)

-11 Terima kasih kepada Ørjan Johansen

Akhirnya sebuah tantangan di mana saya bisa mencetak 0 dengan Bersih!
(dan biasanya buruk pada tantangan tata letak sumber!)

//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

Cobalah online!

TIO menggunakan tautan module mainkarena cara Clean diterapkan pada TIO, tetapi module dakan berfungsi jika Anda memberi nama file d.iclalih-alih main.iclseperti TIO.

Salah satu baris lama menjelaskan (versi baru adalah hal yang sama dalam urutan berbeda):

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
   ]
Suram
sumber
1
Tidak menggunakanlet menghemat 11 byte.
Ørjan Johansen
@ ØrjanJohansen Terima kasih! Saya juga mengubah header modul karena kami memiliki dberguna yang berdekatan
14urous
5

JavaScript (ES6), 0 (154 byte)

Disimpan 2 4 byte berkat @ ØrjanJohansen

Mengambil input sebagai array string.

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=

Cobalah online!

Arnauld
sumber
156 bytes
Ørjan Johansen
4

Jelly , 0 ( 41 27 25 byte)

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

Cobalah online!

Mengambil input sebagai daftar baris. Baris pertama dalam kode tidak pernah melakukan apa-apa dan hanya ada untuk meminimalkan karakter yang terisolasi.

ỴŒĠạþ`€Ẏ§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
dylnan
sumber
1

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)

Cobalah online!

Jonathan Frech
sumber
1

05AB1E , 0 (101 byte )

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

Cobalah online.

Ini adalah salah satu program 05AB1E paling jelek dan terpanjang yang pernah saya tulis ..>.> Tantangan ini sangat sulit di 05AB1E. Saya tidak ragu dalam pikiran byte-count setidaknya dapat dibelah dua atau bahkan tiga / empat kali lebih kecil dengan menggunakan pendekatan yang berbeda (atau bahkan dengan pendekatan yang sama), tetapi saya saat ini tidak melihat caranya. Saya hanya senang itu berfungsi sekarang .. Jika orang lain memposting jawaban 05AB1E jauh lebih pendek dengan beberapa trik pintar saya mungkin akan menghapus jawaban ini karena malu ... xD

Penjelasan:

ž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)
Kevin Cruijssen
sumber
1

Ruby , skor 0, 237 209 byte

##->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]]]==[[]]}}}

Cobalah online!

GB
sumber
0

JavaScript (Node.js) , 0 (279 byte)

  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

Cobalah online!

Terima input sebagai larik baris.

Shieru Asakoto
sumber