| Bytes | Lang | Time | Link |
|---|---|---|---|
| nan | Trilangle | 231209T030841Z | Bbrk24 |
| nan | owl lisp | 230826T165321Z | krzych |
| nan | ForWhile | 230725T075910Z | bsoelch |
| nan | Ruby | 230727T194148Z | Value In |
| nan | Bash | 230727T121236Z | Nahuel F |
| nan | Common Lisp | 230727T120539Z | coredump |
| nan | C gcc | 230725T102042Z | Peter |
| nan | brainfuck | 230726T033459Z | guest |
| 009 | Retina | 230725T194655Z | Neil |
| nan | Nim | 230725T213707Z | xigoi |
| nan | Go | 230725T205858Z | c-- |
| nan | C++14 | 230725T202816Z | c-- |
| nan | Rust | 230725T193847Z | LeopardS |
| 030 | Lua | 230725T153507Z | val - di |
| nan | Thunno 2 | 230725T120816Z | The Thon |
| nan | 05AB1E | 230725T115336Z | Kevin Cr |
| nan | Haskell | 230725T093213Z | matteo_c |
| nan | PowerShell | 230725T095330Z | user3141 |
| nan | JavaScript V8 | 230725T090815Z | Arnauld |
| nan | Python | 230725T045714Z | SuperSto |
| 009 | Charcoal | 230725T063508Z | Neil |
| nan | ><> Fish | 230725T055119Z | mousetai |
| nan | Vyxal | 230725T032339Z | lyxal |
| nan | GolfScript | 230725T051217Z | Bubbler |
Trilangle, 104 (program) + 48 + 82 + 175 + 175 (outputs) = score 584
'0.<_.<.>i,S)#..\_//222*3'/."-S:2'*)<.L-3',o\.><#.(,o.""<._.......j)2<>(<#,,o@o+0"@#.,#,o'"........^oo",
Try it in the online interpreter!
The score is kinda terrible because the output is quadratic in the size of the input. This program:
- Collects the input and keeps track of length
' 0 < _ < . > i , S ) # . \ _ / - Computes
triangular(3n) - 3n - 2, the amount of NOPs needed to put the work all on the bottom row of the output program/ 2 2 2 * 3 ' / - S : 2 ' * ) - Outputs a leading
\to redirect the IP to the bottom row, all the filler NOPs, and the "end program" instruction" < L - 3 ' , o \ > < # . ( , o . " " < _ , o @ - Repeats the input in a format that Trilangle can understand it. This relies on implementation-defined behavior; it works in my reference interpreter as of v1.6.2.
j ) 2 < > ( < # , o + 0 " @ # . , # , o ' " ^ o o " , .
owl lisp, 51 + 176 = 227
this is a very quick and dirty solution
(λ (s) `(print (list->string ',(string->list s))))
example:
> (eval ((λ (s) `(print (list->string ',(string->list s)))) "cat") *toplevel*)
cat
ForWhile, 42 (Program) + (9+10+14+14) (Output) = 89 bytes
34#::`[_~:[~;;=[92#):92=[:#)#])#"(,#)"(,#)
Explanation:
ForWhile allows multi-line strings, so the only characters that need to be escaped are " and \
34# \ print a "
::`[ \ loop up to 34^34 times (for-while has no infinite loops)
_ \ read a character
~:[~ \ character is not end-of file
;;=[92#) \ print a backslash if character is "
:92=[:#) \ print a backslash if character is \
# \ print the character
]) \ break loop if character was end of file
#"(,#)"(,#) \ print "(,#) to print the string produced by the program
The printed programs are just a (multi-line) string literal containing the Input-string, followed by (,#) which will print the string.
Output for Test-cases:
"cat"(,#)
"fish"(,#)
"m
ta-c t"(,#)
"m\"'`\\c"(,#)
ForWhile, 35 (Program) + (4*17+3+4+8+6) (Output) = 124 bytes
Shorter program but slightly longer output.
"17-9:`[1-:@:[#])?"(,#)9:`[_~:[~#])
Prints a program (of length 17) that prints all its source code except the first 17 characters and then terminates, followed by the input string.
Ruby, 19 bytes +10+11+16+15 = 71
Chose to count the trailing newline in the score because it's possible to print without it, as per the other answer.
$><<"$><<"
p$<.read
Ruby, 23 bytes +9+10+15+14 = 71
Output doesn't include a trailing newline, but the score turned out to be the same anyways because the solution required 4 more bytes.
$><<"$><<"+$<.read.dump
Common Lisp, 21 + 52 = 73
(lambda(s)`(print,s))
For example:
(funcall (lambda(s)`(print,s)) "cat"))
=> (print "cat")
C (gcc), 108 + 84 = 192 bytes
Lots of bytes less thanks to @c--.
f(int*s){for(printf("main(){puts(\"");*s;s++)printf(*s-34?*s-92?*s-10?s:"\\n":"\\\\":"\\\"");puts("\");}");}
brainfuck, 78 + 2109 = 2187 bytes
+++++[>+++++++++<-]>[->+>>+>++>+>++<<<<<<]>-->>+>+>>+++<<<<,[[-<.>]>[.>]<[<],]
Retina, 9 bytes, score 39
¶
$¶
^
K`
Try it online! Explanation:
¶
$¶
Change newlines into pilcrows.
^
K`
Prepend the K instruction (replace buffer with literal string).
The test cases encode as follows:
K`cat
K`fish
K`m¶eta-c t
K`m"'`\c
Go, 91 + 216 = 307 bytes
import."fmt"
func f(s string){Printf(`package main
import."fmt"
func main(){Print(%q)}`,s)}
C++14, 132 + 176 = 308 bytes
[](auto&s){auto t=s;s="#include<cstdio>\nint main(){puts(\"";for(auto c:t)s+=c-34?c-92?c-10?s+=c,"":"\\n":"\\\\":"\\\"";s+="\");}";}
Rust, 51 bytes + 110 bytes = 161 bytes
fn f(x:&str){print!("fn main(){{print!({:?})}}",x)}
We take the input as a string, but print the output, saving having to specify f's return type. Debug does the escaping for us.
Lua, 30 bytes, score 82
print(('print%q'):format(...))
No need for parentheses in resulting program since we're printing a string literal.
Thunno 2, 2 + 28 = 30
Ṙṫ
Take the string Ṙepresentation, then remove the ṫail (last character, the end quote).
05AB1E, score: 28 (3 bytes + 25 output sum)
"“ÿ
The outputs for the four test cases are:
Input: cat
Output (4): “cat
Input: fish
Output (5): “fish
Input: """m
ta-c t""" (multiline input)
Output (9): “m
ta-c t
Input: m"'`\c
Output (7): “m"'`\c
Explanation:
" "# Push the following string:
“ # Regular dictionary quote for the output program
ÿ # String interpolation, which is automatically replaced with the (implicit) input
# (after which the result is output implicitly - with trailing newline)
If there is no trailing " or “, 05AB1E outputs everything after it as a string.
PowerShell, 24 + 30 = 54 bytes
% *ce "'" "''"|%{"'$_'"}
Input comes through the pipeline.
% *ce "'" "''" - % is an alias for the ForEach-Object cmdlet, which takes the input string, and runs the string's method "Replace()" (the only one matching "*ce") against it, replacing all single quotes with a double single quote.
This is then piped further to %{"'$_'"}; again ForEach-Object, but this time invoking the scriptblock, which simply takes the input string, and wraps it in single quotes; output is implicit.
In PowerShell, anything inside a string enclosed in single quotes is taken verbatim, so no need to worry about escaping anything, except for the single quotes.
As an alternative, a Here-String @'<LF>...<LF>'@ could be used, which would remove the need to escape even the single quotes:
%{"@'`n$_`n'@"}
This is only 15 bytes instead of 24, but the output has 6 additional characters per test case instead of 2, and would come to a total of 60 bytes.
In the TIO above, the code can just be replaced.
Charcoal, 9 bytes, score 33
SUT¿ⅉ´U´T
Try it online! Link is to verbose version of code. Explanation:
S
Write the input string to the canvas. This is sufficient to generate a program that prints only printable ASCII; the rest is newline handling.
UT
Turn off right-padding. (Has no effect if there was no newline in the input.)
¿ⅉ
If the input contained at least one newline...
´U´T
... output the command to turn off right-padding.
The test cases encode as follows:
cat
fish
m
eta-c tUT
m"'`\c
Try it online! (Third test case only; the others are trivial.)
><> (Fish), 71 + 46 bytes = 117
Hover over any symbol to see what it does
Unformatted version if the code above appears glitched:
"<o>'"i:0(?v:\
< \ >o<"'"~< |.!05
^v?= "'":v?=a/
^ "'a'"~/
~/^1"'d3*'"
Only characters that need escaping are the single quote and line break
Vyxal, 3 bytes + 23 character sum = 26 score
øDṪ
So funny story, my previous answer wasn't actually valid because øD already wraps in quotes, making the q useless.
Explained
øDṪ
øD # dictionary compress the input, quoting it at the same time
Ṫ # Remove the trailing backtick