Programmi di autogrammi


34

Questa frase impiega due a, due c, due d, ventotto e, cinque f, tre g, otto h, undici i, tre, due, tredici, nove, due, due, cinque, venticinque s, ventitre t, sei v, dieci w, due x, cinque y e una z.

Tali frasi sono chiamate autogrammi . Il tuo compito è scrivere un programma o una funzione che abbia una proprietà simile: prende come input un byte e genera un numero intero che rappresenta il numero di volte in cui quel byte appare nel codice sorgente del programma. Dovrebbe produrre 0 se il suo input non appare nella sua sorgente.

Il tuo programma non deve leggere il proprio codice sorgente , direttamente o indirettamente, ma invece tutti i dati necessari per fornire il proprio output devono essere codificati, in stile quine. Il programma deve avere almeno un byte di lunghezza.

Questo è , quindi vince la voce più breve (in byte). Non accetterò una risposta, quindi sentiti libero di competere per la soluzione più breve nella tua lingua preferita.

Sei fortemente incoraggiato a includere una spiegazione nel tuo post.

Classifiche

Di seguito sono riportate le classifiche sia per il punteggio complessivo che per la lingua:


5
Sandbox . (È lì dal 2014!)
Nathaniel,

Sono consentite le funzioni o solo programmi completi?
Uriel,

1
Nota: le risposte simili a questa non valgono più come valide.
user202729

1
FWIW, la definizione di quine corretta è un po 'più forte di quella che vorrei davvero per questa sfida. Ad esempio, sarei abbastanza felice di vedere un programma che utilizza tutti i caratteri in un intervallo contiguo e genera solo un 1 se il suo input è in tale intervallo, anche se questo non ha "una sezione del programma che codifica una parte diversa del programma ". Per questo motivo sarei stato disposto a consentire l'esempio della retina, anche se sarebbe stato deludente se avesse scoraggiato gli altri dalla pubblicazione, dato che era già solo 1 byte. (@ user202729)
Nathaniel

1
A causa del mio commento sopra, ho rimosso l'affermazione che si applicano le regole di quine. (Ho tenuto la parte sul non leggere il codice sorgente.)
Nathaniel

Risposte:


15

Ottava , 44 byte

@(x)sum([[39,'(())*,239==@[[]]msuxx']]==x)*2

Provalo online!

Il risultato della funzione, quando si utilizza ciascuno dei caratteri nella funzione come input:

'  (  )  *  ,  2  3  9  =  @  [  ]  m  s  u  x  
2  4  4  2  2  2  2  2  4  2  4  4  2  2  2  4  

Tutti gli altri caratteri di input restituiscono uno zero.

Il fatto che dovessi scappare mi ha 'reso molto più lungo. Per tenere conto delle 4 parentesi, ho appena avuto un duplicato nella stringa. Tuttavia, la duplicazione di apostrofi richiede apostrofi, quindi questo ci porta ulteriormente dal risultato corretto. Pertanto, ho dovuto contare il numero di apostrofi con il suo ASCII-valore 39. Questo naturalmente significa che ho dovuto controllare per i personaggi 3e 9anche, il che rende tutto molto più a lungo.


2
Questo è il genere di cose che spero di vedere di più. (Ottimizzazione per il problema, piuttosto che prendere semplicemente un quine standard e aggiungere codice per contare i caratteri.)
Nathaniel

7

Excel, 84 byte

=IFERROR(MID("65496331125442343343233",FIND(A1,"""123456789,()=MINORFADEOMEN"),1),0)

Find()cercherà il valore nella cella A1all'interno della stringa "123456789,()=MINORFADEOMEN( """all'inizio è per sfuggire al carattere e valuterà come giusto ").

Sulla base del risultato di Find(), la Mid()funzione restituirà il carattere corrispondente dalla stringa di numeri. Questa stringa è stata creata tramite iterazione fino a quando non ha smesso di cambiare.

Se il carattere in A1non viene trovato, Find()restituisce un errore in modo che la IfError()funzione sfugga a quella da restituire 0.

Alla OMENfine della stringa cercata nella Find()funzione ci sono lettere duplicate, quindi la loro posizione non verrà mai restituita ma erano necessarie per regolare il conteggio dei caratteri. Senza di loro, c'era un ciclo infinito di conti mutevoli. La disposizione delle lettere è una scelta stilistica.


5

JavaScript (ES6), 70 byte

Non legge l'origine della funzione, ma è piuttosto lunga. Accetta l'input come stringa di 1 carattere.

i=>~(n='\\34=\'in(|)0257?:.>[]Odefx~'.indexOf(i))?'3733544333'[n]||2:0

Provalo online!


Puoi accorciarlo un po 'rielaborando un po' per ridurre la quantità di caratteri distinti: i=>'36335633'[i='\\36=\'i|&()25.>[]Odefx~n'.indexOf(i)]||~i&&2(8 byte salvati)
Yair Rand

5

Retina , 1 byte

Ciò non sarebbe valido se si seguissero rigorosamente le regole del quine, ma l'OP lo ha esplicitamente permesso in un commento .

x

Provalo online!

Nella retina un programma a riga singola conta le occorrenze di quella regex nell'input. Questa sfida è risolto da qualsiasi carattere ASCII singola eccezione `, ., +, *, ?, [, (, ), ^, $, \e ritorno a capo.


5

C # (compilatore Visual C #) , 88 57 byte

x=>"\\\"(())==>>??..::3300CCaaiinoossttx".Contains(x)?3:0

Provalo online!

Restituisce 3 se la stringa contiene il carattere passato, altrimenti restituisce 0. La stringa contiene almeno una volta ciascun carattere del codice ed esattamente la quantità richiesta per avere il carattere 3 volte nel codice.

-31 byte grazie a Kevin Cruijssen


Bel approccio! +1 da me. È possibile salvare alcuni byte rimuovendo la parentesi (a)=>e prendendo l'input come stringa anziché come carattere, quindi .ToString()non è più necessario (e a+""avrebbe potuto essere utilizzato anche, per le sfide del code-golf non avrete mai bisogno .ToString()). Inoltre, poiché .Containscontiene già un a, potresti rendere la variabile di input un diverso carattere inutilizzato (come xo q) per avere 3 per ogni carattere invece di 4. EDIT: Non importa l'ultima parte, vedo che "viene usata anche 4 volte adesso.
Kevin Cruijssen,

@KevinCruijssen Grazie per il consiglio. Ma la stringa è valida come input? Considerando OP ha richiesto un byte come input?
Hyarus,

Fintanto che è una stringa di caratteri singoli, sono abbastanza sicuro che lo sia. La maggior parte delle lingue può comunque inserire solo valori stringa, e alcuni hanno caratteri che scelgono comunque di inserire come stringa (come Java, JavaScript e Japt, ad esempio. I formati di input e output sono di solito piuttosto flessibili. Ma potresti sempre chiedere OP in un commento se hai ancora dubbi. :)
Kevin Cruijssen,

1
@KevinCruijssen sembra ragionevole. E se
sfuggiamo a

2
Le stringhe di caratteri singoli come input vanno bene per me.
Nathaniel,

4

Haskell , 66 byte

Le prime due versioni sono essenzialmente una quine che filtra i caratteri richiesti e quindi prende la lunghezza:

q c=length.filter(==c)$id<>show$"q c=length.filter(==c)$id<>show$"

Provalo online o prova con tutti i personaggi! *


Alternativa senza (<>), 72 byte

q c=length.filter(==c)$(++)<*>show$"q c=length.filter(==c)$(++)<*>show$"

Provalo online o prova con tutti i personaggi!


Alternativa non nulla, 87 86 byte

Potrebbe essere ancora migliorato, ma sono felice di averlo ridotto a soli tre gruppi diversi.

Questa mi piace di più, anche se conta il maggior numero di byte. Calcola il conteggio di caratteri / byte come somma di 2,3 e 7 (nota come alcuni caratteri sono in più gruppi):

u t=sum[fst e|e<-[(2," ()-237<=dflnst|"),(3," ()[\\]`mstu"),(7,"\",ee")],t`elem`snd e]

Provalo online o prova con tutti i personaggi!


* importa (<>)perché la versione GHC di TIO è 8.0.2


3

Python 2 , 54 52 32 byte

-20 byte grazie agli ovs

("''+.23cnotu()"*2+'"*'*3).count

Provalo online!


1
How come you don't have either lambda or input?
Stewie Griffin

2
@StewieGriffin Python automatically binds a method reference, so "xxx".count is equivalent to lambda c:"xxx".count(c).
Neil

@StewieGriffin and you use both in the same way ->f="xxx".count and f=lambda c:"xxx".count(c) will be called as f('a')
Rod

3

Husk, 11 10 8 bytes

Thanks Leo for -2 bytes!

#sD"#sD"

Try it online!

Explanation

This makes sure that it only uses ASCII (because show would mess it up) and that each character is contained twice:

#sD"#sD"  -- character as input, eg. '"'
   "#sD"  -- string literal (note the redundant '"'): "#sD"
  D       -- double: "#sD#sD"
 s        -- show: "\"#sD#sD\""
#         -- number of occurences: 2

Standard quine extension, 11 bytes

#hS+s"#hS+s

Try it online!

Explanation

#hS+s"#hS+s  -- character as input, eg. '"'
     "#hS+s  -- string literal: "#hS+s"
  S+         -- join with itself: ("#hS+s"++)
    s        -- | and itself shown: "\"#hS+s\""
             -- : "#hS+s\"#hS+s\""
 h           -- init: "#hS+s\"#hS+s"
#            -- number of occurences in string: 1

1
You can manage the quotes more easily by doubling the string before showing it: Try it online!
Leo

3

Java 10, 164 81 57 bytes

q->"q-->>\\\"..ccoonttaaiiss(())??33::00".contains(q)?3:0

Port of @Hyarus' C# answer, so make sure to upvote him!

Explanation:

Try it online.

q->      // Method with String parameter and integer return-type
  "q->\\\"..ccoonttaaiiss(())??33::00".contains(q)?
         //  If the string above contains the input character
   3     //   Return 3
  :      //  Else:
   0     //   Return 0

Old 164-bytes answer:

c->{var s="c->{var s=%c%s%1$c;return s.format(s,34,s).replaceAll(%1$c[^%1$c+c+']',%1$c%1$c).length();}";return s.format(s,34,s).replaceAll("[^"+c+']',"").length();}

Explanation:

Try it online.

c->{                                  // Method with char parameter and integer return-type
  var s="c->{var s=%c%s%1$c;return s.format(s,34,s).replaceAll(%1$c[^%1$c+c+']',%1$c%1$c).length();}";
                                      //  Unformatted source code
  return s.format(s,34,s)             //  Create the formatted source code (the quine),
          .replaceAll("[^"+c+']',"")  //  remove all characters not equal to the input,
          .length();}                 //  and return the length

-part:

  • The String s contains the unformatted source code.
  • %s is used to input this String into itself with the s.format(...).
  • %c, %1$c and the 34 are used to format the double-quotes.
  • s.format(s,34,s) puts it all together

Challenge part:

  • .replaceAll("[^"+c+']',"") removes all characters except those equal to the input.
  • .length() then takes the length of this String.

NOTE: .split(c).length (with String input instead of char) may seem shorter, but has two problems:

  1. The first character gives an incorrectly result, so if c (with c-> as leading part) is input, it will incorrectly return one character too few. This can be fixed by adding +(c==99?1:0) in both the source code and unformatted source code String (and changing .split(c) to .split(c+""), but then we still have the following problem:
  2. If a regex character (i.e. $) is input, the .split will interpret it as a regex, giving an incorrect result.

3

Haskell, 58 bytes

f c=sum[2|x<-succ '!':"f c=sum[2|x<-succ '!':,x==c]",x==c]

Try it online! or verify the solution.

Same byte count alternatives:

f c=sum[2|x<-tail$show"f c=sum[2|x<-tail$show,x==c]",x==c]
f c=sum[1|x<-id<>show$"f c=sum[1|x<-id<>show$,x==c]",x==c]

Haskell, 90 bytes

sum.($zip"1234\"$,.[\\]opu+()iklmsz652"$[4,4,11,3,3,3,16,5,3,3,3,3,3,3,3]++[2,2..]).lookup

Try it online! or verify the solution. Uses the fact that sum Nothing = 0 and e.g. sum (Just 4) = 4.


3

Smalltalk, 112 132 bytes

Smalltalk isn't exactly known for its golfing suitability :-)

Method defined in class Character (tested in VA Smalltalk and Squeak, should work in other dialects such as VisualWorks and Pharo as well):

a^(0to:6),#(10 18)at:(#('' ',[]^|+68cForu' '#0adefln' '1it' '()' ':s' ' ' '''')findFirst:[:s|s includes:self])+1

Characters occurring in the source are grouped by occurrence count. The groups are tested for the first one which contains the receiver, and the matching occurrence count is returned.

Old method:

a^('''''((((())))):::[[[[]]]]^^^^^0000066666aaaacccccdddddeefffFFFFFiiilllnnnnnrrrrrsssTTTTTuuuu'includes:self)ifTrue:[6]ifFalse:[0]

Every character that does appear in the method appears exactly 6 times (by being repeated in the string constant), so the method just checks whether the receiver is contained in the string and returns 6 if it is, 0 otherwise.

After defining the method as above, you can validate it using

| code |
code := Character sourceCodeAt: #a.
((0 to: 255) collect: [:b | b asCharacter]) reject: [:c | c a = (code occurrencesOf: c)]

The result should be empty.


1
Welcome to PPCG!
Martin Ender

2

JavaScript, 31 bytes

f=c=>~-`f=${f}`.split(c).length

Try it online


1
I think this reads its own source code, doesn't it? That's explicitly banned, in bold, in the question.
Nathaniel

1
@Nathaniel, see here.
Shaggy

2
Hmm. Well honestly I think that's stupid, but I can't really disagree with meta consensus I suppose.
Nathaniel

1
@Nathaniel For all programs that make eval easy, they also make quine easy.
user202729

1
@user202729 I don't think that follows, does it? You can't do the same trick in Python, for example. This is a reverse eval, not an eval
Nathaniel



2

Haskell, 96 bytes

n"n"=15;n"="=14;n" "=2;n";"=13;n"\\"=3;n"\""=25;n"0"=2;n"1"=4;n"2"=4;n"3"=4;n"4"=5;n"5"=5;n n3=0

Try it online!

Haskell, 109 bytes

n 'n'=15;n '='=14;n ' '=14;n ';'=13;n '\\'=3;n '\''=25;n '0'=2;n '1'=5;n '2'=4;n '3'=4;n '4'=5;n '5'=5;n n3=0

Try it online!

Haskell, 122 bytes

n 'n'=21
n '='=14
n ' '=14
n '\n'=12
n '\\'=4
n '\''=32
n '0'=2
n '1'=5
n '2'=5
n '3'=3
n '4'=5
n '5'=5
n nnnnnn4'''''''=0

Try it online!

Explanations

These answers are not terribly sophisticated. It is a series of declarations, one for each character present in the program. At the end we have a catch all that returns 0 for characters not present in the program.

I use a couple of tricks to minimize the number of characters necessary in the program and from there I fiddled with things until the numbers turned out just right. You can see that I've padded the variable name in the last declaration, in all 3 of them. The difference between the 3 programs is whether I chose to use a new line or ; for line breaks and whether I chose to take Chars as input or Strings. The ; approach doesn't seem inherently superior to the others it just gets luck and ends up shorter, however it does seem that using Strings is a better idea than Chars because Chars require spaces after the function name an Strings do not.


:| what happened to your spelling in the explanation
ASCII-only

2

Whitespace, 140 bytes

[S S S N
_Push_0][S N
S _Duplicate][S N
S _Duplicate][T N
T   S _Read_STDIN_as_character][T   T   T   _Retrieve][S S S T  S S T   N
_Push_9][T  S S T   _Subtract][S N
S _Duplicate][N
T   S S T   N
_If_0_Jump_to_Label_TAB][S S S T    N
_Push_1][T  S S T   _Subtract][S N
S _Duplicate][N
T   S S N
_If_0_Jump_to_Label_NEWLINE][S S S T    S T T   S N
_Push_22][T S S T   _Subtract][N
T   S T N
_If_0_Jump_to_Label_SPACE][N
S T N
_Jump_to_Label_PRINT][N
S S S T N
_Create_Label_TAB][S S S T  S S T   S T N
_Push_37][N
S T N
_Jump_to_Label_PRINT][N
S S S N
_Create_Label_NEWLINE][S S S T  S S S S T   N
_Push_33][N
S T N
_Jump_to_Label_PRINT][N
S S T   N
_Create_Label_SPACE][S S S T    S S S T T   S N
_Push_70][N
S S N
_Create_Label_PRINT][T  N
S T _Print_as_integer]

Letters S (space), T (tab), and N (new-line) added as highlighting only.
[..._some_action] added as explanation only.

70 spaces, 37 tabs, and 33 new-lines used.

Usually I use the Create Labels in the order NSSN, NSSSN, NSSTN, NSSSSN, NSSSTN, NSSTSN, NSSTTN, etc. But because printing a number where the binary S=0/T=1 is used affects the number I need to output, I used the labels NSSN, NSSSN, NSSTN, and NSSSTN instead, which gave the perfect amount of spaces/tabs to be printed with the binary numbers SSSTSSSSTN (33; amount of new-lines), SSSTSSTSTN (37; amount of tabs), and SSSTSSSTTSN (70; amount of spaces).

Explanation in pseudo-code:

Character c = STDIN-input as character
If c is a tab:
  Print 37
Else if c is a new-line:
  Print 33
Else if c is a space:
  Print 70
Else
  Print 0

Example runs:

Input: space

Command       Explanation                   Stack       Heap     STDIN   STDOUT   STDERR

SSSN          Push 0                        [0]
SNS           Duplicate top (0)             [0,0]
SNS           Duplicate top (0)             [0,0,0]
TNTS          Read STDIN as character       [0,0]       {0:32}   \n
TTT           Retrieve                      [0,32]      {0:32}
SSSTSSTN      Push 9                        [0,32,9]    {0:32}
TSST          Subtract top two (32-9)       [0,23]      {0:32}
SNS           Duplicate top (23)            [0,23,23]   {0:32}
NTSSTN        If 0: Jump to Label_TAB       [0,23]      {0:32}
SSSTN         Push 1                        [0,23,1]    {0:32}
TSST          Subtract top two (23-1)       [0,22]      {0:32}
SNS           Duplicate top (22)            [0,22,22]   {0:32}
NTSSN         If 0: Jump to Label_NEWLINE   [0,22]      {0:32}
SSSTSTTSN     Push 22                       [0,22,22]   {0:32}
TSST          Subtract top two (22-22)      [0,0]       {0:32}
NTSTN         If 0: Jump to Label_SPACE     [0]         {0:32}
NSSTN         Create Label_SPACE            [0]         {0:32}
SSSTSSSTTSN   Push 70                       [0,70]      {0:32}
NSTN          Jump to Label_PRINT           [0,70]      {0:32}
NSSN          Create Label_PRINT            [0,70]      {0:32}
TNST          Print as integer              [0]         {0:32}            70
                                                                                  error

Program stops with an error: No exit defined.
Try it online (with raw spaces, tabs, and new-lines only).

Input: tab

STDIN will be \t (9) instead, in which case it will be 0 at the first If 0 check, goes to LABEL_TAB/NSSSTN, and will push and print 37 instead.

Try it online (with raw spaces, tabs, and new-lines only).

Input: new-line

STDIN will be \n (10) instead, in which case it will be 0 at the second If 0 check, goes to Label_NEWLINE/NSSSN, and will push and print 33 instead.

Try it online (with raw spaces, tabs, and new-lines only).

Input: anything else

Any other input-character will do NSTN (Jump to Label_PRINT) after the third If 0 check, printing the 0 that was still on the stack (which we've duplicated at the very beginning).

Try it online (with raw spaces, tabs, and new-lines only).


1
This is a great language for this challenge.
qwr

2

Japt, 27 bytes

\Ua"" a a a " ÄU\\\"a "aU Ä
            " ÄU\\\"a "     // Given this string literal,
                       aU   // find the last index of the input
                          Ä // and add +1.
\Ua"" a a a                 // Do nothing useful, but make the results match.

Longer than the existing Japt answer, but uses a different approach.
Has the inherent limitation that all chars need to occur a unique number of times.

Takes input as a string.

Try it online!


2

Perl, 130 bytes

+print+0+((0)x40,6,6,0,3,43,0,0,0,22,12,6,3,5,2,4,0,1,0,0,0,1,0,1,(0)x28,1,0,1,(0)x6,1,(0)x4,1,(0)x4,1,1,1,0,2,0,1,0,0,0,5)[ord<>]

Has no newline or other whitespace. Reads a line from the standard output, but only cares about its first bytes, then prints the number of times that byte occurs in its own source code in decimal, without a newline.

The program is straightforward. Most of the source code is occupied by a literal table that gives the answer for the each possible byte. Trailing zeros are omitted, and adjacent zeros are run-time compressed, but there's no special trick other than that. The rest of the program simply reads the input and looks up the answer in the table.

For example, the part 22, 12, 6, 3, 5, 2, 4, 0, 1, 0 in the source code gives the frequency of digits, so there are 22 zeroes, 12 ones, 6 twos etc in the source code. As a result, if you enter 0 to the standard input of the program, the program will print 22.


2

C (gcc), 1033 bytes

#include <stdio.h>
int main(int argc,char *argv[]){int r=0*14811;switch((int)argv[1][0]){case' ':r=6;break;case'"':r=3;break;case'#':r=2;break;case'%':r=2;break;case'\'':r=101;break;case'(':r=5;break;case')':r=5;break;case'*':r=5*1*1;break;case'.':r=2;break;case':':r=51;break;case';':r=103;break;case'<':r=2;break;case'=':r=52;break;case'>':r=2;break;case'[':r=4;break;case'\\':r=3;break;case']':r=4;break;case'0':r=11;break;case'1':r=20;break;case'2':r=20;break;case'3':r=9;break;case'4':r=7;break;case'5':r=12;break;case'6':r=3;break;case'7':r=2;break;case'8':r=5;break;case'9':r=2;break;case'a':r=106;break;case'b':r=51;break;case'c':r=55;break;case'd':r=4;break;case'e':r=102;break;case'f':r=2;break;case'g':r=4;break;case'h':r=4;break;case'i':r=10;break;case'k':r=51;break;case'l':r=2;break;case'm':r=2;break;case'n':r=8;break;case'o':r=2;break;case'p':r=2;break;case'r':r=108;break;case's':r=53;break;case't':r=8;break;case'u':r=2;break;case'v':r=3;break;case'w':r=2;break;case'{':r=3;break;case'}':r=3;break;}printf("%d",r);}

Try it online!

This is by NO means a golfed answer, but it was fun to try to accomplish this challenge in a language which I am not familiar with. It wasn't a particularly difficult challenge until it came time to find the occurrences of the digits, now THAT was a challenge. Had to do a little creative balancing :)


2

C (gcc), 192 bytes

F(J){J=J-70?J-40?J-41?J-74?J-'{'?J-'}'?J-39?J-48?J-49?J-50?J-51?J-52?J-53?J-54?J-55?J-56?J-57?J-47?J-61?J-63?J-45?J-58?J-59?0:1:23:23:23:1:2:3:3:4:4:14:14:10:10:15:6:4:2:2:25:1:1:1;}//84332211

Try it online!

Probably possible to golf down further. Uses a comment at the end as a 'scratch space' to add extra digits. When I have to change a digit from X to Y, I change one of the Y's in the scratch to an X to compensate. Other than that this is just a function that takes an integer, using the assignment trick to return a value based on a large ternary conditional.


I can save 1 byte by moving the numeric constant to after the zero as 0*84332211 like in the other C answer, but I can also save bytes by refactoring that to be smaller, so I'm not going to bother just yet.
LambdaBeta

Can you save bytes by using a range check for the less common numbers? that is if we assme 4-9 are rare characters, we can handle them all at once with a conditional check.
qwr

Almost certainly. We could probably remove even more by picking clever names for things so that certain symbols never appear or so that the bit-patterns can be used (e.g. J&1? to match all odd characters). I may further golf this if I find the time to do so.
LambdaBeta

2

x86 .COM, 17 bytes, controversial

0120 BF2001        MOV     DI,0120 (120 be the current address)
0123 B91100        MOV     CX,0011
0126 AE            SCASB
0127 7502          JNZ     012B
0129 FEC4          INC     AH
012B E2F9          LOOP    0126
012D C1E808        SHR     AX,8
0130 C3            RET

36 bytes

0100 BF????        MOV     DI,(an copy of this code)
0103 B91200        MOV     CX,0012
0106 AE            SCASB
0107 7503          JNZ     010C
0109 80C402        ADD     AH,02
010C E2F8          LOOP    0106
010E C1E808        SHR     AX,8
0111 C3            RET

2
Isn't this reading it's own code?
Razvan Socol

@RazvanSocol I think it's like the JavaScript solution
l4m2

@RazvanSocol well it doesn't require saving in a file so it's okay. a lot of fungeoids and JS also do this
ASCII-only

I argue you are reading code that is executed, which counts as "reading own source code". Now you could argue having a string copy of the program, as most solutions here do, is also "reading source code", but the string isn't executed.
qwr

1

Japt, 14 bytes

Qi"QiUè² " ²èU

Try it


Explanation

                   :Implicit input of character string U
  "QiUè² "         :String literal
Qi                 :Append a quotation mark
           ²       :Repeat twice
            èU     :Count the occurrences of U


1

Jelly, 16 bytes

“;⁾w⁸a2”;⁾“”w⁸a2

Try it online!

Every character appears exactly twice.

“;⁾w⁸a2”;⁾“”w⁸a2
“;⁾w⁸a2”;⁾“”     the string ;⁾w⁸a2“”
            w⁸a2 return 2 if input in string

1

x86, 42 40 bytes

Here I use the same strategy as others: Create a string copy of the program with unique bytes, then return 2 in al if the input al is in the string. If we allow ourselves to read code that is actually run, we get l4m2's solution.

I got to make use of a cool string instruction scasb. To the best of my knowledge, there are no duplicate bytes, but this is something I'd easily screw up. Loading the string address takes 5 bytes but I'm not aware of any shorter solution (64-bit lea of rip with offset takes 6 bytes).

-2 by jumping backwards to avoid using 02 twice.

.section .text
.globl main
main:
        mov     $0xff, %eax

start:
        push    $20             # program length
        pop     %ecx            # counter
        mov     $str, %edi      # load string

loop:
        scasb                   # if (al == *(edi++))  
        jne     loop1           
        mov     $2, %al         # ret 2
end:    ret             
loop1:
        loop    loop            # do while (--counter)
        xor     %eax, %eax      # ret 0
        jmp     end

str:    .byte 0x6a,0x14,0x59,0xbf,0xf4,0x83,0x04,0x08 
        .byte 0xae,0x75,0x03,0xb0,0x02,0xc3,0xe2,0xf8
        .byte 0x31,0xc0,0xeb,0xf9 

Hexdump (of binary file format elf32-i386, as obj file unfortunately has 00 bytes for str address):

000003e0  6a 14 59 bf f4 83 04 08  ae 75 03 b0 02 c3 e2 f8  |j.Y......u......|
000003f0  31 c0 eb f9 6a 14 59 bf  f4 83 04 08 ae 75 03 b0  |1...j.Y......u..|
00000400  02 c3 e2 f8 31 c0 eb f9                           |....1...|

x86, 256 bytes

Boring answer that is the equivalent of a giant comment. Input in cl, immediately returns 1 in al. I'll make an actual answer when I have the free time.

00000039  b0 01 c3 00 02 03 04 05  06 07 08 09 0a 0b 0c 0d  |................|
00000049  0e 0f 10 11 12 13 14 15  16 17 18 19 1a 1b 1c 1d  |................|
00000059  1e 1f 20 21 22 23 24 25  26 27 28 29 2a 2b 2c 2d  |.. !"#$%&'()*+,-|
00000069  2e 2f 30 31 32 33 34 35  36 37 38 39 3a 3b 3c 3d  |./0123456789:;<=|
00000079  3e 3f 40 41 42 43 44 45  46 47 48 49 4a 4b 4c 4d  |>?@ABCDEFGHIJKLM|
00000089  4e 4f 50 51 52 53 54 55  56 57 58 59 5a 5b 5c 5d  |NOPQRSTUVWXYZ[\]|
00000099  5e 5f 60 61 62 63 64 65  66 67 68 69 6a 6b 6c 6d  |^_`abcdefghijklm|
000000a9  6e 6f 70 71 72 73 74 75  76 77 78 79 7a 7b 7c 7d  |nopqrstuvwxyz{|}|
000000b9  7e 7f 80 81 82 83 84 85  86 87 88 89 8a 8b 8c 8d  |~...............|
000000c9  8e 8f 90 91 92 93 94 95  96 97 98 99 9a 9b 9c 9d  |................|
000000d9  9e 9f a0 a1 a2 a3 a4 a5  a6 a7 a8 a9 aa ab ac ad  |................|
000000e9  ae af b1 b2 b3 b4 b5 b6  b7 b8 b9 ba bb bc bd be  |................|
000000f9  bf c0 c1 c2 c4 c5 c6 c7  c8 c9 ca cb cc cd ce cf  |................|
00000109  d0 d1 d2 d3 d4 d5 d6 d7  d8 d9 da db dc dd de df  |................|
00000119  e0 e1 e2 e3 e4 e5 e6 e7  e8 e9 ea eb ec ed ee ef  |................|
00000129  f0 f1 f2 f3 f4 f5 f6 f7  f8 f9 fa fb fc fd fe ff  |................|

1

APL (Dyalog Classic), 30 bytes

⊢⊢⊢11-11-11-'''''''1''⊢-⍳⍳0'⍳⊢

Try it online!

Explanation

In APL, single quotes within strings are escaped by doubling, so '''''''1''⊢-⍳⍳0' is the string '''1'⊢-⍳⍳0, which contain every character used in the program.

APL arrays are by default 1-indexed, and the index-of function, interestingly, returns 1 + max index if the element is not found.

So, by using index-of on the string and the input returns

Input    Index    Count
'        1        10
1        4        7
⊢        6        5
-        7        4
⍳        8        3
0        10       1
<other>  11       0

As one can see, 11 - index gives the count of the character in the program. So, the basic algorithm is

11-'''''''1''⊢-⍳⍳0'⍳⊢

The rest is bloating the character counts to allow them to fit nicely into the slots.


1

R, 135 bytes

Inspired by this Python answer.

Previous versions were broken. Thanks to @Giuseppe for pointing out that paste was not required it saved 18 bytes or so. lengths(regmatches(z,gregexpr(x,z)))is from this answer.

function(x,z=rep(c("afilo2679=:","hmpu15'","nstxz","cgr","e","()",'"',","),c(2:5,7,9,15,16)))sum(lengths(regmatches(z,gregexpr(x,z))))

Try it online!



0

Ruby, 48 bytes

->x{%q[->x{%q[].count(x.chr)*2}].count(x.chr)*2}

%q[str] is a more convenient way to write a string literal than "str" because it can be nested inside itself without any escaping. So I just put the entire code except for the copy inside it, then double the count.


Utilizzando il nostro sito, riconosci di aver letto e compreso le nostre Informativa sui cookie e Informativa sulla privacy.
Licensed under cc by-sa 3.0 with attribution required.