| Bytes | Lang | Time | Link |
|---|---|---|---|
| 030 | Dyalog APL | 250724T203614Z | Aaron |
| 024 | Haskell + hgl | 250724T193204Z | Wheat Wi |
| 303 | C gcc | 221018T140943Z | matteo_c |
| 008 | Vyxal | 210302T232537Z | lyxal |
| 038 | JavaScript ES6 | 200524T202327Z | Arnauld |
| 008 | Jelly | 200523T164024Z | Jonathan |
| 022 | Python 2 | 200522T235406Z | dingledo |
| 028 | Ruby nl | 200811T064809Z | Dingus |
| 001 | Retina | 200810T214901Z | user |
| 387 | COW | 200523T095748Z | Domenico |
| 049 | Perl 5 + plF | 200526T193508Z | Dom Hast |
| 144 | Whitespace | 200525T104541Z | Kevin Cr |
| 026 | Retina 0.8.2 | 200524T205925Z | math jun |
| 048 | Python 3 | 200524T215825Z | anroesti |
| 024 | J | 200524T094546Z | FrownyFr |
| 016 | Japt | 200524T030009Z | Quintec |
| 018 | Brachylog | 200524T011328Z | Unrelate |
| 010 | Husk | 200524T004303Z | Unrelate |
| 090 | R | 200523T154754Z | Dominic |
| 095 | Turing Machine Code | 200523T133548Z | ouflak |
| 128 | perl | 200523T133515Z | Abigail |
| 014 | 05AB1E | 200523T013728Z | user9206 |
| 012 | Stax | 200522T235028Z | Khuldrae |
| 054 | JavaScript Node.js | 200523T020142Z | Shieru A |
| 010 | Pyth | 200523T013837Z | math jun |
| 034 | J | 200523T001134Z | Jonah |
| 020 | Charcoal | 200522T231012Z | Neil |
Dyalog APL, 30 chars
{2×+/⍵∊⍨'2×+/⍵∊⍨{},3/',3/''''}
Fun!
Function checks for membership of the input in the string
2×+/⍵∊⍨{},3/'''''
Because it takes 2 single quote marks in a row inside a string to represent a 1 single quote mark, I had to use replicate (3/'''') to get extra copies in and concat that with the rest of the program string.
Haskell + hgl, 24 bytes
fce$mp~<sh$"fce$mp~<sh$"
Explanation
"fce$mp~<sh$"is a string, it is exactly equal to the rest of the source. More importantly it contains each character the same number of times as the rest of the text. Reordering this string won't break the program.mp~<shconcatenates the string with its string representation. This gives the exact source code.fcecounts the number of times a certain character appears in a string.
Reflection
This is not exactly a straightforward challenge so I'm not sure what can really be taken away from this. I don't think this is a great score, but it's not terrible and I'm not frustrated by any lack.
A few notes suggestions:
- It would be better for this challenge if
cacounted the number of equal pairs between two words. That would make it symmetric on the input words. Right now I could replacefcewithfca(and likewise take a length 1 string as input instead of a character), but not with the shortercasincecaonly considers the multiplicity of the second string. Maybe a good idea would be to add a version ofcawith this behavior, but it would need to be two bytes to save here.cashould definitely make it clear it doesn't do this since the docs are not explicit. I had to manually test it which was somewhat bothersome. - I was trying to do a version which just detected presence in a string (like this answer) and then multiplied by two with each character appearing exactly twice or nonce in the code. However I kept hitting snags. Things that would have helped would have been:
- A builtin just for the character
'"', the double quote. There are already builtins for newline (Nl) and backslash (Sl). This would allow me to put quotes into the string without having more than 2 quotes in the entire program. I have trouble imagining any other circumstance in which this would be useful though. - This is probably the most useful suggestion here: A function to convert
TrueandFalseinto 1 and 0 respectively. Probably this would just betoIntegerhowever there are two issues with this:- There does not appear to be a shortcut for
toInteger. This seems like an incredible oversight. I still have trouble believing it. (fromIntegerlikewise is missing a shortcut, which should be fixed but is not exactly relevant here.) Boolis not anIntegralanyway sotoIntegerwouldn't even convert a boolean if there was a shortcut.
- There does not appear to be a shortcut for
- A builtin just for the character
C (gcc), 303 bytes
n[4467]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,1,0,0,3,3,0,0,126,0,0,0,92,15,17,2,2,2,2,2,0,1,0,2,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,1,2,0,0,0,2,0,1,1,1,1,1,1,2,0,0,0,1,5,0,1,0,2,0,2,0,0,0,0,0,0,2,0,2};main(){printf("%d\n",n[getchar()]);}
Vyxal, 12 8 bytes
`I$O`I$O
Back then, there was no quine cheese. Now there is.
Explained
`I$O`I$O
`I$O` # The string "I$O"
I # surrounded in backticks and appended to itself
$O # Get the count of the input in that.
JavaScript (ES6), 40 38 bytes
A function made of 19 distinct characters used twice each.
_=>_.match`[\\]chmt[-a.02:=>?^^-]`?2:0
How?
The range going from [ to a allows us to match the backtick and the underscore, which are both already doubled in the code, without explicitly including them in the pattern.
This method comes with two minor drawbacks:
- Because this range also includes
^, we need to insert it twice in the code as well although it's not part of the payload. - We also have to insert a second
-.
Character set:
-.02:=>?[\]^_`achmt
Jelly, 12 10 8 bytes
-2 thanks to caird coinheringaahing (input is a character, not a string!)
“ḤṾċⱮ”Ḥċ
A monadic Link accepting a character that yields the count.
How?
“ḤṾċ”ḤṾċ - Main Link: character, c
(A full program with a single character as input gives the main
Link a list of characters - i.e. S = ['c'])
“ḤṾċ” - list of characters = ['Ḥ', 'Ṿ', 'ċ']
Ḥ - double = ["ḤḤ", "ṾṾ", "ċċ"] (Python strings)
Ṿ - un-eval = ['“', 'Ḥ', 'Ḥ', 'Ṿ', 'Ṿ', 'ċ', 'ċ', '”']
ċ - count occurrences of c (in the un-eval-ed list)
- implicit print (a list with a single entry just prints that entry)
Ruby -nl, 40...32 28 bytes
p~/[,-2\[-\]p~,..011?:]/?2:0
Derived from my answer to a related challenge. Each of the 14 distinct characters in the code appears twice. The input is tested against a regexp. The outer [] is a character class containing literal characters plus two character ranges: ,-2 is equivalent to the literal characters ,-./012 and \[-\] is equivalent to the literal characters [\]. The code prints 2 if the regexp matches the input character; 0 is printed otherwise.
Retina, 1 byte
1
Counts the number of 1s in the input, so the output is always 1 or 0.
Inspired by this answer on a related question.
COW, 711 519 504 390 387 bytes
moOMooMOOmOoMoOMoOMoOMoOMoOMoOMoOMOOmoOMOoMOoMOoMOoMOoMOoMOoMOoMOoMOoMOomOoMOomoomoOMMMMOOMOomoomoOMoOMoOMoOMoOMoOMoOMoOMoOmOomooMMMMOOMOoMOoMMMmoOMoOMoOmoOMOomOomOoOOOmooMMMMOOmOoMoOMoOMoOMoOMOOmoOMOoMOoMOoMOoMOoMOoMOoMOomOoMOomoomoOMMMmoOMOomoOMoOmOomOoOOOmooMMMMOOMoOMoOMMMmoOOOOMoOMoOMoOmoOMOoMOomOomOoOOOmooMMMMOOmoOmoomoOMOOmoOMoOMoOMoOMoOMoOMoOMoOMoOMoOMoOMoOMoOMoOmOoMOomoomoOOOM
I appreciated a lot the previous solution (504 bytes) because it's deducted step by step, if you are interested please take a look at that in the timeline...
Now I furnish the program with a more "sensible" structure that promises to be less expensive (in terms of total byte count) but finding just a working set of values isn't simple...
To find the best, brute force comes in.
The next sections refers to the 390 bytes answer since it's simpler to explain that and then tell what it's done to save 3 bytes.
What it does
\$L = \{M,O,o,m\}\$ is the set of used chars and \$n_1,\dots ,n_4\$ their count.
The program has a structure that allows to form the output number \$n_i\$ as
$$
\begin{align}
n_i = x_i\times cf+y_i
\end{align}
$$
Furthermore we don't need to form \$x_i\$ and \$y_i\$ every time from zero, we reach them using the partial sums of 2 sequences occurring in cells [2] and [3] respectively.
[0]: multiplier for [1] [1]: input-current_char [2]: x_i [3]: y_i
[1] = getchar()
if[1] { [1]-=77(M), [2]+=8, [3]+=0, cut[1] } paste[1] //checks for 'M' and prepares its count
if[1] { [1]-= 2(O), [2]+=2, [3]+=0, cut[1] } paste[1] //checks for 'O' and prepares its count
if[1] { [1]-=32(o), [2]-=1, [3]-=1, cut[1] } paste[1] //checks for 'o' and prepares its count
if[1] { [1]+= 2(m), [2]-=6, [3]-=2, cut[1] } paste[1] //checks for 'm' and prepares its count
if ![1] //input was one of the letters above
print [2]*13+[3]
else //input was something else
print [4] (0)
As long as the input doesn't match any letter in \$L\$, [1] stays ≠ 0 and [2] and [3] hold \$x_i\$ and \$y_i\$ of the last tested letter.
Otherwise, if [1] has become 0, those value are no longer updated and at the end they will form the related \$n_i\$.
(When the input isn't a letter in \$L\$, after the 4 tests [1] is still ≠ 0 so the flow enters a loop ([>]) that sabotages the placement of the head, thus prevents from printing \$n_4\$ (the number of o).)
How
I've first constructed the exoskeleton: the complete program without the info about its char count.
That is without \$cf\$(common factor) and the 2 sequences that form \$x_i\$ and \$y_i\$.
- The exoskeleton
moo ] mOo < MOo - OOO * Moo .
MOO [ moO > MoO + MMM = oom o
>.
[ <+++++++[>-----------<-]> > ? > ? << =*]=
[ -- > ? > ? << =*]=
[ <++++[>--------<-]> > ? > ? << =*]=
[ ++ > ? > ? << =*]=
[>]
>[> ? <-]>o
Some of them can be negative but I know that in order to write them I'll spend \$len\$: the sum of their absolute value in MoOs and MOos (+ and -).
Thanks to this condition the comptutation is rather straightforward.
Brute force
\$cf>0\qquad\qquad A=\{a_1,\ a_2,\ a_3,\ a_4,\ a_5,\ a_6,\ a_7,\ a_8\}\$
\$x_i=\sum_{j=1}^{i} a_j \qquad y_i=\sum_{j=5}^{4+i} a_j\$
\$n_i = x_i\times cf+y_i\$
\$len = cf + \sum_{j=1}^{8} |a_j|\$
$$ (*)\begin{cases} n_1=|M|+len\\ n_2=|O|+len\\ n_3=|o|+len\\ n_4=|m| \end{cases} $$
Given the letter count in the exoskeleton \$|M|=71,\ |O|=97,\ |o|=85,\ |m|=38\$
we can now search for \$cf\$ and \$A\$ that satisfies \$(*)\$ minimizing \$len\$.
cf = 13, A = [8, 2, -1, -6, 0, 0, 1, -2] is the best for that exoskeleton (\$len=33\$)
Finished program
>.
[ <+++++++[>-----------<-]> > ++++++++ > << =*]=
[ -- > ++ > << =*]=
[ <++++[>--------<-]> > - > + << =*]=
[ ++ > ------ > -- << =*]=
[>]
>[>+++++++++++++<-]>o
As you can se when some \$a_i\$ is \$0\$ its relative pair of > < became non-functional. But obviously we can't take them off in retrospect.
-3 bytes, 387 bytes
Juggling with the exoskeleton I've found that there is one configuration slightly different that overall saves 3 bytes (1 instruction). What's different?
- One
*is replaced with[-]that has the same effect (+2 instr.) - Using one
*\$x_4\$ is detached from the partial sum: \$x_4=a_4\$ (+1 instr.) - A pair of
><are saved because \$a_5=0\$ (-2 instr.)
The letter count of this exoskeleton is \$|M|=73,\ |O|=98,\ |o|=86,\ |m|=37\$
cf = 13, A = [8, 2, -1, 3, 0, -1, 1, -2] is the best. (\$len=31\$) (-2 instr.)
Perl 5 + -plF, 49 bytes
$_=q($_=grep/\Q@F/,qq(\$_=q($_);eval)=~/./g);eval
Explanation
Pretty much the standard quine with some minor changes.
The program itself is stored in $_ which is evaled (eval - along with most other functions - works on $_ by default if no argument is specified. When the program is evaluated, $_ is set to the number of matches (~~grep) against the input char /\Q@F/, which is interpolated at runtime and necessitates \Q to escape any special chars, against the list of chars resulting from the template string $_=q(...);eval with the actual program interpolated in the innermost %_, which is obtained with =~/./g (this returns a list of all the chars that matches the regex, . being match-all).
Whitespace, 144 bytes
[S S S N
_Push_0][S N
S _Duplicate_0][S N
S _Duplicate_0][T N
T S _Read_STDIN_as_character][T T T _Retrieve_input][S N
S _Duplicate][S S S T S S S S S N
_Push_32][T S S T _Subtract][N
T S S N
_If_0_Jump_to_Label_SPACE][S N
S _Duplicate][S S S T S S T N
_Push_9][T S S T _Subtract][N
T S T N
_If_0_Jump_to_Label_TAB][S S S T S T S N
_Push_10][T S S T _Subtract][N
T S S T N
_If_0_Jump_to_Label_NEWLINE][N
S N
N
_Jump_to_Label_PRINT][N
S S S N
_Create_Label_SPACE][S S S T S S T S T T N
_Push_75][N
S N
N
_Jump_to_Label_PRINT][N
S S T N
_Create_Label_TAB][S S S T S S S S T N
_Push_33][N
S N
N
_Jump_to_Label_PRINT][N
S S S T N
_Create_Label_NEWLINE][S S S T S S T S S N
_Push_36][N
S S N
_Create_Label_PRINT][T N
S T _Print_as_integer_to_STDOUT]
Letters S (space), T (tab), and N (new-line) added as highlighting only.
[..._some_action] added as explanation only.
Try it online (with raw spaces, tabs and new-lines only).
Explanation in pseudo-code:
Character c = STDIN as character
If(c == ' '):
Print 75 to STDOUT
Else-if(c == '\t'):
Print 33 to STDOUT
Else-if(c == '\n'):
Print 36 to STDOUT
Else:
Print 0 to STDOUT
This sounds pretty straight-forward, but it was reasonably tricky to get the correct numbers. Pushing a number in Whitespace is done as follows:
S: Enable Stack ManipulationS: Push numberS/T: Positive/negative respectively- Some
T/Sfollowed by a singleN: Decimal as binary, whereTis 1 andSis 0
So, after I created the template of my program, the amount of newlines were fixed and I could simply print 36 in that case.
But the amount of spaces and tabs are variable. If I correct the count-output of the amount of spaces, the tabs would be incorrect, and vice-versa. This required some tweaking, which I eventually did without wasting any bytes by using a Label ST for the Label_NEWLINE. Usually I create Labels in the following order, based on the amount of times it's used: (empty label); S; T; SS; ST; TS; TT; SSS; etc. In this case, I've skipped the SS and used ST instead, allowing me to print 75 and 33 with the binary pushes of TSSTSTT and TSSSST for the counts of spaces and tabs respectively.
Retina 0.8.2, 26 bytes
T`Tan[-a\]\n^0__-`2
[^2]
0
I got the idea to use [-a from @Arnauld's JavaScript answer.
Explanation
T`Tan[-a\]\n^0__-`2
Transliterate each of the following characters into a 2:
The letters
T,a, andnThe range
[-awhich also includes\,],^,_, and`Literal
]and literal newlineThe characters
^,0,_, and-
[^2]
0
Replace any character that is not a 2 with a 0
Python 3, 48 bytes
x="(x+2*chr(34)+'x=;f=eval(x)').count";f=eval(x)
Idea: Store code in string. The stored code returns function that counts characters in the string within which it is contained. Evaluate the string to get the function. Special care for characters wrapping the string.
Python 3 without eval I, 48 bytes
lambda c:3*(c in" \\\"(())**33::abbcddiillmmnn")
Python 3 without eval II, 124 bytes
And a more creative, but much longer solution:
lambda c:[0o3623>(ord(c)-76)**2>195,' !!""##$$%%&&++,-..//4457889:;;==ZZ\\^^__``beeffgghhiijjkklmnnppqqrssttuuvvwwxx'][0]<<1
Idea: Ensure all characters that satisfy a certain equation (195 < (c-76)² < 1939) appear exactly twice in the code, and return 2 for those characters (0 for all others). Maybe someone can think of a better compression for the long string, but remember expressing it may only use the same character twice.
Japt, 16 bytes
"UèiQÑ )"iQ èU)Ñ
Takes inspiration from the normal Japt quine. Essentially, counts the number of occurences in the string at the beginning (with a quote), then doubles it.
Brachylog, 18 bytes
∈"∈∈\\\"∧33||"∧3|∧
Brachylog doesn't really have any good way to get quotes without escaping them in a string literal (or using the Ṭ constant, which came out longer when I tried it), so the approach I came to is to simply triple down on everything else.
(I don't know why the testing header runs out of stack after it's done every test case; I'd think it's something to do with the unbound variable output for 0 but it works fine on individual inputs... so long as an unbound variable is indeed an acceptable way to give 0. If it's not, +3 bytes)
Husk, 10 bytes
D#hs"D#hs"
D Twice
# the number of occurrences of the input in
"D#hs" "D#hs",
s quoted,
h without the second quote.
For the same number of bytes:
Husk, 10 bytes
#S+s"#S+s"
Somewhat boring adaptation of the standard S+s"S+s+ quine.
R, 96 82 126 94 90 bytes
3*sum(unlist(strsplit(c('"',"#'''((()))*,,3=acilmnprsssttu"),''))==scan(,''))##()*,3amprst
Edit1: thanks to math junkie for pointing-out a horrible bug in the original version (the \ character): hence the temporary increase and subsequent decrease in byte-length, as successive patches were added in panic..
Edit2: -4 bytes: Copying the entire program into the 'look-up' string seemed wasteful (94 bytes), so instead added extra characters so that every character was present an even-number of times, and now just include half the program (character-wise) in the look-up string
Turing Machine Code, 95 bytes
I'm not sure this counts, and if so deemed, I'll make it non-competing (or delete it if you guys think it's too egregious). The reasons why have to do with the Turing Machine Code interpreter I'm using. This affects the space character ( ), asterisk (*), and semi-colon(;).
Space character
Basically it internally converts space characters into underscores '_'. Also, it interprets the lack of any character as a space character, and therefore interprets that as an underscore. Not only this, but it also interprets an actual underscore '_' as an underscore (or space, or lack of a character). Since the input is strictly limited to the text box on the web page, this means there is significant ambiguity as to how to count a space character. So that character will not work with this code. Any attempts at trying to fudge something here could easily and reasonably be discounted as wrong by atleast 3 different valid interpretations that I can come up with off the top of my head.
Asterisk
This character is set aside for a few special uses, depending on where in the code it's used. Most relevantly for this challenge, the asterisk - when used as an input check - acts as a special catch-all character. So any attempt at trying to catch this as input, catches anything and everything, including the aforementioned space character. It does so without any ability to discern an actual asterisk from the infinite possibilities. So that character will not work with this code.
Semicolon
Lastly, the semicolon is a very strict line comment. You put that anywhere in that line of code, and that's it. Everything after it (inclusive) on that line is interpreted as a comment, and is ignored. As a result of this, this Turing Machine interpretor will never be able to 'read' the semicolon character ';'. So that character will not work with this code.
0 0 1 r 0
0 _ 2 * 2
0 r 7 * r
0 7 3 * r
0 2 7 * 2
0 3 5 * r
0 1 2 * r
0 5 3 * r
0 * 0 * 2;3325_
I'm deeply suspicious that there is a two or three line solution to this. I'll probably play around with this for a bit some more. Having to use a comment to pad the numbers really sets off a flag in my head that this code could accomplish this task far more efficiently.
perl, 128 bytes, assuming ASCII input only
print 1;#!"$%&'()*+,-./023456789:<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghjklmoqsuvwxyz{|}~...
Replace the trailing ... with the 33 unprintable characters (ASCII 0 .. ASCII 31 + ASCII 127), with the newline at the end. (If anyone knows how to put unprintable characters in a textfield and have them show up here, I'm all ears).
05AB1E, 14 bytes
Port of the Stax answer. ¢ is order-sensitive, which is fairly annoying here.
"„Js¢·"„""Js¢·
05AB1E, 18 bytes
As for this... I wrote this myself.
„…J…¢s·'„'D''DJs¢·
Explanation
„…J 2-char string. …, J
…¢s· 3-char string. ¢, s, ·
'„ 1-char string. „
'D 1-char string. D
'' 1-char string. '
D Copy this character.
J Join the stack.
s¢ Count occurances of the input in the string.
· Multiply the count by 2. (Perfectly makes the 0-count still 0.)
Stax, 12 bytes
".+#H".""+#H
Takes as input a code point. Those provided in the test are for each character in the program, as well as their immediate predecessors and successors.
Explanation:
".+#H".""+#H
".+#H" String literal ".+#H"
."" String literal "\"\""
+ Concatenate
# Count occurrences of the input
H Double
Constructs a string of half the program, permuted somehow. Counts occurrences there, then doubles.
JavaScript (Node.js), 54 bytes
y=>"3.>=includes(*)\\\"y3.>=includes(*)".includes(y)*3
A bit convoluted but I tried to avoid using f= that would violate the proper quine requirement. The code is written in the way that all characters occur exactly 3 times.
Pyth, 10 bytes
y/+N"y/+N"
y/+N"y/+N"
"y/+N" String literal
+N Append the string `"`
/ Count occurrences of the input in that string
y Multiply by 2
J, 34 bytes
(2*1#.=&'(2*1#.=&)+6*=&')+6*=&''''
how
single quote
quoted program adjustment
| /
vvvvvvvvvvvvvvvv _____/___
(2*1#.=&'(2*1#.=&)+6*=&')+6*=&''''
^^^^^^^^ ^^^^^^^^^^
\ /
regular program
- Everything above a
^is part of the "regular program". - The rest is "the program quoted", but with one exception:
- The quoted program doesn't include the program's single quote characters
'
- The quoted program doesn't include the program's single quote characters
2*1#.=&'...'- Two times2*the sum of1#.the total number of times the input char matches a char in "the quoted program". One for the actual program char, one for its quoted twin.+6*=&''''- Plus six times+6*the1/0-indicator of whether the input char is equal to a single quote=&''''. This is the hardcoded knowledge that there are 6 single quotes in the program.
Charcoal, 20 bytes
I⊗№⁺´”´””yI⊗№⁺´´yS”S
Try it online! Explanation:
´”´” Literal string `””`
⁺ Concatenated with
”yI⊗№⁺´´yS” Literal string `I⊗№⁺´´yS`
№ S Count matches of input character
⊗ Doubled
I Cast to string
Implicitly print
Charcoal has two ways of quoting non-ASCII characters, ´ (which quotes a single character) and ”y...” (which quotes anything except ”). Trying to do everything with ´ is awkward because it uses too many of them; the best I could do was 26 bytes.