| Bytes | Lang | Time | Link |
|---|---|---|---|
| 077 | Swift 6 | 250705T145657Z | macOSist |
| 015 | Pip | 250703T205055Z | DLosc |
| 009 | Vyxal 3 | 250702T081337Z | Themooni |
| 011 | Uiua | 240802T005200Z | janMakos |
| 184 | Setanta | 240802T001931Z | bb94 |
| 033 | Raku Perl 6 rakudo | 240802T001229Z | bb94 |
| 012 | UiuaSBCS | 240801T204331Z | Europe20 |
| 029 | Pyth | 220307T205200Z | sinvec |
| 051 | Haskell | 200617T155630Z | Laikoni |
| 1716 | x8616 machine code | 200603T214342Z | 640KB |
| 055 | Haskell | 200605T182830Z | cole |
| 060 | Java OpenJDK 8 | 200604T025910Z | branboye |
| 068 | Wolfram Language | 200604T221217Z | DanTheMa |
| 009 | Stax | 200604T183725Z | recursiv |
| 022 | APL Dyalog Unicode | 200603T082529Z | RGS |
| 025 | Ly | 200604T111945Z | LyricLy |
| 046 | C gcc | 200603T111218Z | Noodle9 |
| 100 | TSQL | 200603T091237Z | t-clause |
| 068 | Erlang escript | 200603T231652Z | user9206 |
| 057 | R | 200603T103657Z | Dominic |
| 032 | Perl 5 p | 200603T083718Z | Nahuel F |
| 048 | C gcc | 200603T100231Z | Asone Tu |
| 009 | Jelly | 200603T120454Z | Jonathan |
| 009 | 05AB1E | 200603T082648Z | Kevin Cr |
| 054 | Python 3.8 | 200603T084544Z | ovs |
| 020 | Befunge98 FBBI | 200603T115157Z | ovs |
| 019 | K oK | 200603T113140Z | Galen Iv |
| 019 | Keg | 200603T114451Z | lyxal |
| 025 | Befunge93 | 200603T103120Z | Abigail |
| 042 | JavaScript Node.js | 200603T102522Z | Arnauld |
| 019 | Retina 0.8.2 | 200603T101326Z | Neil |
| 009 | Charcoal | 200603T095637Z | Neil |
| 077 | JavaScript | 200603T093041Z | darrylye |
| 022 | J | 200603T090836Z | Bubbler |
| 022 | APL Dyalog Extended | 200603T090436Z | Adá |
| 042 | Ruby | 200603T085844Z | Asone Tu |
| 021 | APL dzaima/APL | 200603T085400Z | Bubbler |
Swift 6, 77 bytes
{($0+"",""+$0.utf8.enumerated().map{.init(.init(($0+Int($1)-32)%95+32)!)}).1}
Pip, 15 bytes
(PA_+A B-32MEa)
Explanation
(PA_+A B-32MEa)
a ; Command-line argument
ME ; Map-enumerate (_ is index, B is character):
_ ; Index
+A B ; Plus ASCII value of character
-32 ; Minus 32
(PA ) ; Use those values as indices into PA, a builtin for printable ASCII
; Indexing is cyclical, which takes care of the wraparound
; Concatenate that list of characters and autoprint (implicit)
Alternate solutions
Indexing into PA instead of using the ASCII code yields a couple of less-straightforward solutions that are also 15 bytes:
(PAaME_+YPA@?B)
(PA$+GMEPA@?^a)
Vyxal 3, 9 bytes
O③-:ƶ+kPi
-5 bytes by pacman256
O③-:ƶ+kPi
O # convert ascii to values
③- # substract 32
:ƶ+ # add each value's position to itself
kPi # index that value into printable ascii (wraps)
💎
Created with the help of Luminespire.
<script type="vyxal3">
O③-:ƶ+kPi
</script>
<script>
args=[["Hello, world!"],["The random guy"],["zzzzzzzzz"]]
</script>
<script src="https://themoonisacheese.github.io/snippeterpreter/snippet.js" type="module"/>
Uiua, 11 bytes
⍜⊙-◿95@ +°⊏
Explanation:
⍜⊙-◿95@ +°⊏
°⊏ # list of the indices of each char
+ # add to each char its index
⊙- @ # find the ascii difference between each char and SPACE
◿95 # Modulus 95
⍜ # turn back into chars
Setanta, 184 bytes
Setanta doesn’t have a chr or ord so I have to include the entire set of printable ASCII characters.
gniomh(s){A:=go_liosta@" !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"()t:=""le i idir(0,fad@s)t+=A[(aimsigh@A(s[i])+i)%95]toradh t}
UiuaSBCS, 12 bytes
+@ ◿95-@ +°⊏
Explanation
+@ ◿95-@ +°⊏
°⊏ create an array of all whole numbers less than the length of the string
+ for each pair, shift the character by the number
-@ convert the characters to codepoints from the space character
◿95 modulo 95
+@ convert them back into characters
Haskell, 52 51 bytes
zipWith(%)[-32..]
n%c=toEnum$32+mod(fromEnum c+n)95
Haskell, 52 bytes
zipWith(%)[0..]
n%c=snd(span(<c)$cycle[' '..'~'])!!n
Explanation
zipWith(%)[0..]calls the infix function%on each character of the input string along with its index.cycle[' '..'~']builds a list where the ASCII chars are repeated infinitely.span(<c)partitions this list into a prefix of chars smaller than the current charactercand a remainder.snddrops the prefix, so only the list of ASCII chars starting withcremains.!!nreturns thenth element from that list.
x86-16 machine code, IBM PC DOS, 17 16 bytes
Binary:
Build STL.COM using xxd -r:
00000000: b120 b408 cd21 2ac1 d45f 0420 cd29 e0f2 . ...!*.._. .)..
Listing:
B1 20 MOV CL, 32 ; set up offset / position counter
CLOOP:
B4 08 MOV AH, 8 ; use DOS API get char from STDIN function
CD 21 INT 21H ; read input char into AL
2A C1 SUB AL, CL ; subtract offset
D4 5F AAM 95 ; AL = AL % 95
04 20 ADD AL, 32 ; restore ASCII offset
CD 29 INT 29H ; output AL to console
E0 F2 LOOPNZ CLOOP ; keep looping until break, decrement CL
Standalone IBM PC DOS executable program. Input via STDIN, output to console.
Runtime:
Haskell, 55 bytes
g '~'=' '
g c=succ c
i!c=iterate g c!!i
zipWith(!)[0..]
The TIO link has f=, but that doesn't contribute to the byte count because I defined it point-free. It's mostly there so that the code compiles.
I didn't play around much with using a point-free definition instead of !, so that might be a place where some bytes can be shaved off. I just figured that the parens and dots would add up. I also want to get rid of the call to iterate, but I'm not sure how. I want something like mtimes...
Haskell + -XParallelListComp, 55 bytes
g '~'=' '
g c=succ c
f s=[iterate g c!!i|c<-s|i<-[0..]]
Unfortunately this isn't any shorter, but I thought it was a cool usage of a pragma. It seems most of the time that pragmas aren't too helpful in golfing.
Explanation
-- g gives the successor of each character according to the specification
g :: Char -> Char
-- the successor of '~' is ' ' (wrap around)
g '~'=' '
-- all other characters have their normal successor
g c=succ c
-- (!) is an infix function that enciphers a character, given an int
(!) :: Int -> Char -> Char
-- iterate produces an infinite list of 'g' applied to 'c' repeatedly,
-- and '!!' indexes into that list at index 'i'. This has the effect
-- of applying 'g' to 'c' 'i' times.
i!c=iterate g c!!i
-- Point-free definition that applies '!' to each character of the input along with its index
zipWith(!)[0..]
Java (OpenJDK 8), 80 60 bytes
s->{int i=0;for(char n:s)s[i]+=i-(n+i++>126?95:0);return s;}
Thanks for help from Kevin Cruijssen
Wolfram Language, 68 bytes
FromCharacterCode@Mod[#&~Array~Length@#+#-1,95,32]&@*ToCharacterCode
Uses a little function composition to make it shorter, but other than that, does pretty much what it says on the tin. Converts the string to character codes, adds the proper offset to each, wraps around if needed using Mod (there's an optional argument for offset that I use here), then converts back to a string.
APL (Dyalog Unicode), 28 24 22 bytes SBCS
-4 bytes thanks to @Adám; -2 bytes thanks to @ngn;
Monadic function expecting a string:
⎕ucs 32+95|⎕ucs-32-⍳∘≢
Try it online! Assumes ⎕IO←0. How the 24-byter works:
32(⎕ucs⊣+95|⊢--∘⍳∘≢)⎕ucs ⍝ monadic function taking a character vector on the right
⎕ucs ⍝ convert to unicode code points
32( ) ⍝ and then evaluate the expression with 32 as left argument and the code points as right arg
-∘⍳∘≢ ⍝ do 32 minus (-) the range 0 1 ... [length of input string] (⍳∘≢)
⊢- ⍝ take the right argument (⊢) [the codepoints] and subtract the previous calculation from those [effectively adding the positions and subtracting 32]
95| ⍝ and take those numbers modulu 95.
⊣+ ⍝ Take the left (⊣) argument [32] and add it to the numbers we just did mod 95
⎕ucs ⍝ and convert the new code points to characters.
@Bubbler has a similar answer but making use of an operator that hasn't been implemented in Dyalog APL yet, check it out.
C (gcc), 48 47 46 bytes
Saved a byte thanks to 640KB!!!
Saved a byte thanks to dingledooper!!!
i;f(char*s){for(i=32;*s;)*s++=(*s-i--)%95+32;}
T-SQL, 100 bytes
Added some line changes to make it readable
DECLARE @x INT=0
WHILE @x<len(@)
SELECT
@=substring(@,2,999)+char((ascii(@)+@x-32)%95+32),
@x+=1
PRINT @
Erlang (escript), 68 bytes
f(I,[H|T])->[(H+I-32)rem 95+32]++f(I+1,T);f(_,[])->[].
f(I)->f(0,I).
R, 56 57 bytes
function(s)intToUtf8((utf8ToInt(s)-33+1:nchar(s))%%95+32)
Edit: thanks to Giuseppe for bug-spotting!
I am beginning to despise string manipulations in R, and the intToUtf8() / utf8ToInt() function names in particular...
Jelly, 9 bytes
O+J_32ịØṖ
How?
O+J_32ịØṖ - Link: list of characters, S - e.g. "zzzzzzzzz"
O - cast (S) to ordinal values [122,122,122,122,122,122,122,122,122]
J - range of length (S) [ 1, 2, 3, 4, 5, 6, 7, 8, 9]
+ - add (vectorises) [123,124,125,126,127,128,129,130,131]
32 - thirty-two 32
_ - subtract [ 91, 92, 93, 94, 95, 96, 97, 98, 99]
ØṖ - printable ASCII characters " !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~"
ị - index into (1-based & modular) "z{|}~ !"#"
05AB1E, 16 10 9 bytes
žQDIkā<+è
-1 byte by porting @Neil's Charcoal answer, so make sure to upvote him!
I/O as a list of characters.
Try it online or verify all test cases.
Explanation:
žQ # Push the printible ASCII string builtin
D # Duplicate it
I # Push the input-list of characters
k # Get each index in the ASCII string
ā # Push the list [1, length] (without popping)
< # Decrease it by 1 to make it a 0-based range [0, length)
+ # Add the values at the same positions in the lists together
è # Index each into the ASCII string (0-based and with automatic wraparound)
# (after which the resulting list of characters is output implicitly)
Python 3.8, 55 54 bytes
Output is a list of characters.
lambda s,d=33:[chr((ord(c)-(d:=d-1))%95+32)for c in s]
Befunge-98 (FBBI), 20 bytes
'!v
-1<,+ '%_'-\~@#:
Initially pushes d=33 on the stack. On every iteration, d is reduced by one and (input-d)%95+32 is printed.
Keg, 19 bytes
0&(⑻+:\~>[\~% +;],⑹
Woooo! I managed to beat APL somehow! This is a very literal interpretation of the challenge, except for the part where overflow values are decremented after modulusing.
Befunge-93, 25 bytes
:~:1+!#@_" "-+"_"%" "+,1+
Keeps the character count on the bottom of the stack. Then in a loop, reads a character at the time, subtracts 32 to the character, adds the character count, mods it with 95, adds 32 again, then prints the character. Finally, it adds 1 to the character count.
Retina 0.8.2, 19 bytes
{*\M!`^.
^.
T`~p`p
Try it online! Link includes test cases. Explanation:
{
Repeat until the input is empty.
*\M!`^.
Output the first character.
^.
Remove the first character.
T`~p`p
Cyclically increment the remaining characters.
12 bytes in Retina 1:
1,Tv`~p`p`.+
Try it online! Link includes test cases. Explanation:
v`.+
Create overlapping matches that start at each character and end at the end of the string.
1,
Ignore the match of the whole input.
T`~p`p`
Cyclically shift each character according to the number of times it was matched.
Charcoal, 9 bytes
⭆S§γ⁺κ⌕γι
Try it online! Link is to verbose version of code. Explanation:
S Input string
⭆ Map over characters and join
ι Current character
⌕ Find position in
γ Printable ASCII
⁺ Plus
κ Current index
§ Cyclically indexed into
γ Printable ASCII
Implicitly print
JavaScript, 77 bytes
Iterative!
s=>[...s].map((c,i)=>String.fromCharCode((c.charCodeAt()+i-32)%95+32)).join``
JavaScript, 77 bytes
Outer recursive!
f=(s,i=0)=>s[i]?String.fromCharCode((s.charCodeAt(i)+i-32)%95+32)+f(s,i+1):''
JavaScript, 77 bytes
Inner recursive!
s=>(F=i=>s[i]?String.fromCharCode((s.charCodeAt(i)+i-32)%95+32)+F(i+1):'')(0)
J, 22 bytes
(95|<:+#\)&.(_32+3&u:)
Almost word-to-word translation of my dzaima/APL solution.
How it works
(95|<:+#\)&.(_32+3&u:) NB. Input: string S
( X )&.( Y ) NB. The "Under" operator; do Y, do X and undo Y
_32+3&u: NB. Convert chars to codepoints and subtract 32
#\ NB. One-based index
<:+ NB. Add to the codepoints minus 1
95| NB. Modulo 95
_32+3&u: NB. Undo this: add 32 and convert to chars
APL (Dyalog Extended), 22 bytes (SBCS)
Full program. Requires ⎕IO←0
⍞(⊢⊇⍨95|⍳⍨+~⍋⊣)' '…'~'
' '…'~' the printable ASCII range
⍞(…) with the input as left argument, apply the following tacit function to that:
⊣ the left argument (the input)
⍋ the sorting permutation to sort according to the following order:
~ the input without any printable ASCII, i.e. an empty string (this means leave all in current positions)
⍳⍨ the indices of the input characters in the printable ASCII
95| division remainder when divided by 95
⊇⍨ use those indices to select from:
⊢ the printable ASCII
Ruby, 42 bytes
->s{i=33;s.map{((_1.ord-i-=1)%95+32).chr}}
Takes input and returns output as an array of characters.
44 byte version because TIO doesn't support ruby 2.7's _1 syntax
->s{i=33;s.map{|c|((c.ord-i-=1)%95+32).chr}}
APL (dzaima/APL), 21 bytes
{95|⍵+⍳≢⍵}⍢(¯32+⎕UCS)
Just trying out the experimental Under operator ⍢ because the task is screaming for it.
How it works
{95|⍵+⍳≢⍵}⍢(¯32+⎕UCS) ⍝ Input: string S
⍢(¯32+⎕UCS) ⍝ Convert S to Unicode codepoints and subtract 32
{ ⍵+⍳≢⍵} ⍝ Add the index to each char
95| ⍝ Modulo 95
⍢(¯32+⎕UCS) ⍝ Undo the operation:
⍝ Add 32 and convert back to Unicode chars
