| Bytes | Lang | Time | Link |
|---|---|---|---|
| 009 | Thunno 2 B | 230723T094106Z | The Thon |
| 021 | K4 | 180423T211245Z | mkst |
| nan | C++ | 180427T221302Z | Mario |
| 083 | Forth gforth | 180427T151642Z | reffu |
| 087 | bash + bc + xxd | 180425T221449Z | Master_e |
| 024 | GolfScript | 180425T191720Z | wastl |
| 141 | Java 8 | 180423T074153Z | Kevin Cr |
| 065 | brainfuck | 180423T013745Z | Jo King |
| 088 | Python 2 | 180423T143727Z | ElPedro |
| 041 | REXX | 180425T134707Z | idrougge |
| 071 | Coconut | 180425T104402Z | ovs |
| 034 | Perl 5 lp | 180425T101250Z | Ton Hosp |
| 010 | 05AB1E | 180422T235218Z | Magic Oc |
| 079 | Perl 6 | 180423T075754Z | elcaro |
| 166 | Rust | 180424T012952Z | Noskcaj |
| 134 | C tcc | 180424T002036Z | Marcos |
| 011 | Stax | 180423T001352Z | Khuldrae |
| nan | C gcc | 180423T060510Z | ErikF |
| 042 | Ruby | 180423T092101Z | Kirill L |
| 093 | Haskell | 180423T070249Z | user7946 |
| 095 | Scala | 180423T171603Z | Shikkou |
| 040 | Perl 5 p | 180423T165008Z | Xcali |
| 071 | R | 180423T022305Z | Giuseppe |
| 075 | Haskell | 180423T163624Z | Lynn |
| 009 | Pyth | 180423T012540Z | user4854 |
| 123 | Google Sheets | 180423T142849Z | Engineer |
| 030 | Z80 machine code on an Amstrad CPC | 180423T135348Z | CJ Denni |
| 052 | bash | 180423T011305Z | Doorknob |
| 061 | PHP + GNU Multiple Precision | 180423T113304Z | Christop |
| 071 | PHP | 180423T091914Z | Titus |
| 067 | JavaScript Node.js | 180423T073107Z | Arnauld |
| 158 | C# Visual C# Compiler | 180423T115113Z | Hyarus |
| 077 | Python 2 | 180423T110711Z | maxb |
| 110 | Red | 180423T072222Z | Galen Iv |
| 059 | Perl 6 | 180423T105206Z | Phil H |
| 011 | Japt | 180423T071016Z | Shaggy |
| 079 | Ruby | 180423T090059Z | lfvt |
| 030 | APL+WIN | 180423T090735Z | Graham |
| 077 | Python 3 | 180423T082346Z | ovs |
| 007 | Jelly | 180423T050113Z | DELETE_M |
| 008 | Jelly | 180423T013939Z | dylnan |
| 017 | Charcoal | 180423T005838Z | Neil |
| 086 | Python 3 | 180423T000919Z | Jo King |
| 013 | J | 180422T234332Z | Bolce Bu |
Thunno 2 B, 9 bytes
Ḣsḣ8ẇQ2ȷḋ
Explanations
Ḣsḣ8ẇQ2ȷḋ # Implicit input, converted to charcodes
Ḣ # Extract the first character from the input
sḣ # And remove the next character from the rest
8ẇ # Split it into groups of eight
Q # For each: is it not equal to the first character
ȷ # For each inner list in this nested list:
2 ḋ # Convert it back from binary to decimal
# Implicit output, converted to characters
Old:
2ɱ$2ỵ8ẇsȷbC # Implicit input
2ɱ # Take the first two characters
$2ỵ # And then push the rest of the string
8ẇ # Split it into groups of eight
sȷb # Convert each from base-(first two characters)
C # Convert to characters
# Implicit output
K4, 22 21 bytes
Solution:
"c"$2/:'0N 8#?/0 2_x:
Example:
q)k)"c"$2/:'0N 8#?/0 2_x:"ABAABBAAAAAABBAAABAABBAABA"
"012"
Explanation:
Evaluated right-to-left:
"c"$2/:'0N 8#?/0 2_x: / the solution
x: / save input as x
0 2_ / cut x at 0 and 2nd index
?/ / lookup (?) over (/)
0N 8# / reshape into 8-long rows
2/:' / decode (/:) each (') from base 2
"c"$ / cast to character
Notes:
- -1 byte thanks to ngn!
C++, 181 Bytes (VS 2017)
#include<iostream>
int main(int,char**a){int c=7;uint8_t b=0;for(int i=2;a[1][i]!='\0';++i,--c){if(a[1][i]!=a[1][0]){b|=(1<<c);}if(c==0){std::cout<<static_cast<char>(b);c=8;b=0;}}}
Formatted, with better variable names and comments:
#include <iostream>
int main(int argc, char* argv[])
{
int counter = 7;
uint8_t byte = 0;
// Loop until input string is fully read
for (int i = 2; argv[1][i] != '\0'; ++i, --counter)
{
if (argv[1][i] != argv[1][0])
{
byte |= (1 << counter); // Shift 2^counter into the current byte
}
if (counter == 0)
{
std::cout << static_cast<char>(byte); //
counter = 8;
byte = 0;
}
}
}
Forth (gforth), 83 bytes
: f over c@ 0 rot 2 do 2* over i 4 pick + c@ <> - i 8 mod 1 = if emit 0 then loop ;
Input is a standard Forth string (address and length) output is printed to stdout
Explanation
over c@ \ get the value of the first character in the string
0 rot \ add a starting "byte" value of 0 and put the length on top of the stack
2 do \ start a loop from 2 to length-1
2* \ multiply the current byte value by 2 (shift "bits" left one)
over \ copy the reference char to the top of the stack
i 4 pick + \ add the index and the starting address to get address of the current char
c@ <> \ get the char at the address and check if not equal to the reference char
- \ subtract the value from our bit count, -1 is default "true" value in forth
i 8 mod 1 = \ check if we are at the last bit in a byte
if \ if we are
emit 0 \ print the character and start our new byte at 0
then \ and end the if statement
loop \ end the loop
bash + bc + xxd, 87 bytes
a="${1:2:${#1}}"
b="${a//${1::1}/0}"
bc<<<"obase=16;ibase=2;${b//${1:1:1}/1}"|xxd -r -p
Usage:
bash foo.bash 'ABAABBAAAAAABBAAABAABBAABA'
GolfScript, 24 bytes
1>(:x;8/{{x=}%2base}%''+
Explanation:
1>(:x;8/{{x=}%2base}%''+ Full program, implicit input.
Stack: "ABAABBAAAAAABBAAABAABBAABA"
1> Pop the first character (str[1:])
Stack: "BAABBAAAAAABBAAABAABBAABA"
(:x; Pop the next character and assign it to x
Stack: "AABBAAAAAABBAAABAABBAABA"; x = 66
8/ Split into groups of 8
Stack: ["AABBAAAA" "AABBAAAB" "AABBAABA"]; x = 66
{ }% Map
Stack (first run): "AABBAAAA"; x = 66
{ }% Map again
Stack (first run): 65; x = 66
x= Compare to x
Stack: 0; x = 66
Stack: [0 0 1 1 0 0 0 0]; x = 66
Well, actually, it is a string of 0 and 1 bytes.
2base Convert binary string to number
Stack: 48; x = 66
Stack: [48 49 50]; x = 66
''+ Convert to string
Stack: "012"; x = 66
Implicit output
Java 8, 143 142 141 bytes
s->{char i=47;for(;++i<50;)s=s.replace(s.charAt(i%2),i);for(i=2;i<s.length();)System.out.print((char)Long.parseLong(s.substring(i,i+=8),2));}
-1 byte thanks to @OlivierGrégoire.
Explanation:
s->{ // Method with String parameter and no return-type
char i=47; // Index character, starting at 47
for(;++i<50;) // Loop 2 times
s.replace(s.charAt(i%2),i) // Replace first characters to 0, second characters to 1
for(i=2;i<s.length();) // Loop `i` from 2 upwards over the String-length
System.out.print( // Print:
(char) // As character:
Long.parseLong( // Convert Binary-String to number
s.substring(i,i+=8) // The substring in range [i,i+8),
,2));}
brainfuck, 76 71 65 bytes
-6 bytes thanks to Nitrodon!
,>>,,[>>++++++++[-[->+<]<<<<[->+>-<<]>>[[-]>>+<<]>[->++<],>>]<.<]
Feels weird beating Python...
Python 2, 88 bytes
i=input()
f=''.join('10'[x==i[0]]for x in i[2:])
while f:print chr(int(f[:8],2));f=f[8:]
Not the shortest - just an alternative way.
Following version prints the output on one line for 98 bytes although the rules state that trailing whitespace is allowed.:
i=input();f=''.join('10'[x==i[0]]for x in i[2:]);o=""
while f:o+=chr(int(f[:8],2));f=f[8:]
print o
Coconut, 71 bytes
s->k(chr..int$(?,2)..k$(str..s.index),groupsof(8,s[2:]))
k=''.join..map
05AB1E, 10 bytes
¦¦Sk8ôJCçJ
-3 thanks to emigna.
Ù # Unique letters, in order they appear.
v # For each...
yN: # Push letter and index, replace in input.
} # End loop.
¦¦ # Remove first x2.
8ô # Split into eighths.
C # Convert to integer.
ç # Convert to char.
J # Join together entire result.
Perl 6, 82 bytes 79 bytes
{.[2..*].map({+($^s ne.[0])}).rotor(8)».join».parse-base(2)».chr with .comb}
Shortened with help from Phil H (but his solution is still shorter, so go look at his one)
Rust, 166 bytes
fn x(x:&str){for i in(2..x.len()-2).step_by(8){print!("{}",u8::from_str_radix(&x[i..i+8].chars().map(|c|(c as u8-17)as char).collect::<String>(),2).unwrap()as char)}}
Playground link because it requires nightly and tio seems to use stable.
C (tcc), 134 bytes
f(char*x){
int a=*x,i,l=strlen(x);
for(i=2;i<l;)
x[i] = x[i++] - a ? 49 : 48;
for(i=10;i<=l;i+=8)
a=x[i],
x[i]=0,
putchar(strtol(x+i-8,0,2)),
x[i]=a;
}
I am still trying to think of a better way than that ugly swap :P
Stax, 15 11 bytes
ó║¥U⌂½íèäöñ
Run and debug it at staxlang.xyz!
Quick 'n' dirty approach. Working on improving it. Improved it!
Unpacked (13 bytes) and explanation
2:/8/{{[Im:bm
2:/ Split at index 2. Push head, then tail.
8/ Split into length-8 segments.
{ m Map block over each segment:
{ m Map block over each character:
[ Copy first two elements (below) in-place.
I Index of character in first two characters.
:b Convert from binary.
Implicit print as string.
C (gcc), 94 86 bytes (107 for leading whitespace)
Thanks to Jonathan Frech for saving 8 bytes!
f(s,v,i,j)char*s,*v;{for(v=++s,i=j=0;*++s;j&7||(putchar(i),i=0))i|=!(*s-*v)<<(--j&7);}
Original solution:
f(s,v,i,j)char*s,*v;{for(v=++s,i=j=0;*++s;){i|=!(*s-*v)<<(--j&7);if(!(j&7)){putchar(i);i=0;}}}
This assumes that main()'s stripping of whitespace is allowable. Otherwise, I got it to 107 bytes:
f(s,v,i,j)char*s,*v;{while(*s++<33);for(v=s,i=j=0;*++s;){i|=!(*s-*v)<<(--j&7);if(!(j&7)){putchar(i);i=0;}}}
Haskell, 124 105 93 bytes
f(x:_:y)=fromEnum.(/=x)<$>y
g[]=[]
g s=(toEnum.sum.zipWith((*).(2^))[7,6..0])s:g(drop 8s)
g.f
f converts the string to a list of bits by comparing each character to the first one, turning the Bools into zeros and ones with fromEnum. g divides this list into groups of 8, converts them to decimal, and takes the value of the resulting number as an Enum, which Char is an instance of.
Changes:
- -19 bytes thanks to @Laikoni (removing import, embedding
mapinto function) - -12 bytes inspired by @Lynn's answer (getting rid of
takeby zipping with shorter list)
Scala, 95 bytes
s.substring(2).replace(s(0),'0').replace(s(1),'1').grouped(8).map(Integer.parseInt(_,2).toChar)
R, 71 bytes
function(s)intToUtf8(2^(7:0)%*%matrix((y=utf8ToInt(s))[-1:-2]==y[2],8))
Surprisingly golfy!
First, converts the string to ascii code-points with utf8ToInt, saving it as y. Removing the first two characters with negative indexing is shorter than using tail.
The array y[-1:-2]==y[2] is equivalent to the bits when %*% (matrix multiplication) is applied, but first we reshape that array into a matrix with nrow=8, converting from a linear array to byte groupings. Fortuitously, we can then convert to the ascii code points using matrix multiplication with the appropriate powers of 2, 2^(7:0), and then we convert the code points back to a string with intToUtf8.
Haskell, 75 bytes
f[_,_]=""
f(z:o:s)=toEnum(sum[2^b|(b,c)<-zip[7,6..0]s,c==o]):f(z:o:drop 8s)
Pyth, 20 9 bytes
CittxLQQ2
Saved 11 bytes thanks to FryAmTheEggman.
Explanation
CittxLQQ2
xLQQ Find the index of each character in the string.
tt Exclude the first 2.
i 2 Convert from binary.
C Get the characters.
Google Sheets, 123 bytes
=ArrayFormula(Join("",IfError(Char(Bin2Dec(Substitute(Substitute(Mid(A1,3+8*(Row(A:A)-1),8),Left(A1),0),Mid(A1,2,1),1))),""
Input is in cell A1. Google will automatically add ))) to the end of the formula.
Explanation:
Mid(A1,3+8*(Row(A:A)-1),8)grabs chunks of characters 8 at a time, starting with the third.Substitute(Mid(~),Left(A1),0)replaces each instance of the first character with 0.Substitute(Substitute(~),Mid(A1,2,1),1)replaces the second character with 1.Char(Bin2Dec(Substitute(~)))converts the chunk to decimal and then to ASCII.IfError(Char(~,""))corrects all the errors that result from the fact thatRow(A:A)returns far more values than we soBin2Decgives us a lot of zero values andCharerrors out on zero.ArrayFormula(Join("",IfError(~)))joins together all theCharresults andArrayFormulais what makes theRow(A:A)return an array of values instead of just the first value.
Z80 machine code on an Amstrad CPC, 32 31 30 bytes
000001 0000 (9000) ORG &9000
000002 9000 EB EX DE, HL
000003 9001 46 LD B, (HL)
000004 9002 23 INC HL
000005 9003 5E LD E, (HL)
000006 9004 23 INC HL
000007 9005 56 LD D, (HL)
000009 9006 1A LD A, (DE)
000010 9007 05 DEC B
000011 9008 13 INC DE
000012 9009 4F LD C, A
000014 900A Light
000015 900A 26 01 LD H, &01
000016 900C Last
000017 900C 13 INC DE
000018 900D 05 DEC B
000019 900E C8 RET Z
000021 900F Loop
000022 900F 1A LD A, (DE)
000023 9010 B9 CP C
000024 9011 28 01 JR Z, Lable
000025 9013 37 SCF
000026 9014 Lable
000027 9014 ED 6A ADC HL, HL
000028 9016 30 F4 JR NC, Last
000029 9018 7D LD A, L
000030 9019 CD 5A BB CALL &BB5A
000032 901C 18 EC JR Light
The code takes the instruction replace each character with 0 if that character is the same as the first character of the original string, and with 1 otherwise literally and doesn't ever bother to check that a character matches the second character in the input string. It just checks for same-as-first-character and different-from-first-character.
I ran out of registers (the Z80 only has 7 easily usable 8-bit registers, the rest need longer instructions) so I put &01 in H, along with using L to build up the ASCII character (I just realised it's unnecessary to initialise L, saving one byte). When H overflows into the Carry flag, the character in L is ready to be output. Luckily, there is a 16-bit ADC (Add with Carry) that does the job of a left-shift instruction.
(DE) can only be read into A although (HL) can be read into any 8-bit register, so it was a compromise which one to use. I couldn't compare (DE) with C directly, so I had to load one into A first. The labels are just random words that start with L (a requirement of the assembler).
Athe Accumulator - the only register that can do comparisonsBthe counter registerfor the instruction. By rearranging the code, I was able to do the job ofDJNZ: Decrement (B) and Jump if Non ZeroDJNZwith one fewer byteCthe first character in the input stringD,EasDEthe address of the current input characterHthe carry trigger (every 8th loop)Lthe output character being built up
bash, 59 58 52 bytes
tr -t "$1" 01 <<<$1|cut -c3-|fold -8|sed 'i2i
aP'|dc
Thanks to Cows quack for saving 6 bytes.
This challenge works remarkably well with a series of coreutils (and dc to do the conversion and output at the end). First, we use
tr -t "$1" 01 <<<$1
to transliterate the two characters in the input to zeroes and ones. The -t flag truncates the first argument to the length of the second, so this reduces to transliterating the first two characters in the input to 0 and 1, which is what we want. Then,
cut -c3-
removes the first two characters, and
fold -8
outputs 8 of the characters per line. Finally, the sed command turns each line into a dc snippet that reads the number as binary and outputs that byte.
PHP + GNU Multiple Precision, 63 61
<?=gmp_export(gmp_init(substr(strtr($argn,$argn,"01"),2),2));
sadly the GMP extention is not default activated (but shipped).
Run like this:
echo "ABABABAAAAABABAAAAAABAABBAABAAAABBABAAABBB" | php -F a.php
PHP, 73 71 bytes
while($s=substr($argn,-6+$i+=8,8))echo~chr(bindec(strtr($s,$argn,10)));
Run as pipe with -nR or try it online.
golfings:
- start index at
-6and pre-increment by8 - exploit that
strtrignores excessive chars in the longer parameter (nosubstrneeded) - translating to
10and then inverting needs no quotes -> -1 byte - invert character instead of ascii code -->
~serves as word boundary -> -1 byte.
JavaScript (Node.js), 67 bytes
s=>s.replace(/./g,x=(c,i)=>(x=x*2|c==s[1],Buffer(i<3|i&7^1?0:[x])))
How?
We use two different syntaxes of the Buffer constructor:
Buffer([n])generates a buffer containing the sole byte n and is coerced to the corresponding ASCII character. Only the 8 least significant bits of n are considered.Buffer(n)generates a buffer of n bytes. Therefore,Buffer(0)generates an empty buffer, which is coerced to an empty string.
Note: They both are deprecated in recent Node versions. Buffer.from([n]) and Buffer.alloc(n) should be used instead.
Commented
s => // given the input string s
s.replace(/./g, x = // initialize x to a non-numeric value (will be coerced to 0)
(c, i) => ( // for each character c at position i in s:
x = x * 2 | // shift x to the left
c == s[1], // and append the new bit, based on the comparison of c with s[1]
Buffer( // invoke the constructor of Buffer (see above):
i < 3 | // if i is less than 3
i & 7 ^ 1 ? // or i is not congruent to 1 modulo 8:
0 // replace c with an empty string
: // else:
[x] // replace c with the ASCII char. whose code is the LSB of x
) // end of Buffer constructor
)) // end of replace(); return the new string
C# (Visual C# Compiler), 158 bytes
using System.Linq;a=>new string(a.Skip(2).Where((c,i)=>(i-2)%8==0).Select((c,i)=>(char)a.Skip(8*i+2).Take(8).Select((d,j)=>d!=a[0]?1<<7-j:0).Sum()).ToArray())
Python 2, 77 bytes
lambda s:[chr(int(`map(s.find,s)`[i:i+24:3],2))for i in range(7,3*len(s),24)]
Red, 110 bytes
func[s][t: 0 i: 128 foreach c next next s[if c = s/2[t: t + i]i: i / 2 if i = 0[prin to-char t t: 0 i: 128]]]
Explanation:
A simple straightforward solution, no builtins.
f: func [s] [ ; s is the argument (string)
t: 0 ; total - initially 0
i: 128 ; powers of 2, initially 0
b: s/2 ; b is the second charachter
foreach c next next s [ ; for each char in the input string after the 2nd one
if c = b [t: t + i] ; if it's equal to b than add the power of 2 to t
i: i / 2 ; previous power of 2
if i = 0 [ ; if it's 0
prin to-char t ; convert t to character and print it
t: 0 ; set t to 0
i: 128 ; i to 128
]
]
]
Perl 6, 59 bytes
{/(..)(.**8)*/;$1>>.trans(~$0=>"01")>>.parse-base(2)>>.chr}
Matches the pattern first, then uses a transliteration to 01 and parses the number.
Japt, 11 bytes
¤£bXÃò8 ®Íd
Explanation
¤ :Slice from the 3rd character
£ Ã :Map over each X
bX : Get the first 0-based index of X in the input
ò8 :Split to an array of strings of length 8
® :Map
Í : Convert from base-2 string to base-10 integer
d : Get the character at that codepoint
Ruby, 82 79 bytes
->s{s[2..-1].tr(s[0,2],'01').chars.each_slice(8).map{|s|s.join.to_i(2).chr}*''}
APL+WIN, 30 bytes
Index origin 0. Prompts for input of string
⎕av[2⊥¨(+\0=8|⍳⍴b)⊂b←2↓s≠↑s←⎕]
Explanation:
s≠↑s←⎕ prompts for string and creates binary vector not equal to first character
b←2↓s drops first two elements of binary
(+\0=8|⍳⍴b)⊂ splits binary into groups of 8
2⊥¨ converts each group to decimal
⎕av[...] displays decoded characters
Python 3, 77 bytes
a,b,*r=input();x=i=0
for c in r:i*=2;i|=a!=c;x+=1;x%8or print(end=chr(i&255))
Jelly, 7 bytes
ḢnḊs8ḄỌ
Who needs ṫ (tail)?
Coincidentally Because there is only one way to do it, the 4 last bytes are identical to dylnan's answer.
Actually there is another way, providing there are no leading NUL bytes in the output:
Jelly, 7 bytes
ḢnḊḄb⁹Ọ
Jelly, 8 bytes
ṫ3nḢs8ḄỌ
ṫ3nḢs8ḄỌ
ṫ3 All but first two characters...
n does not equal...
Ḣ the first character.
s8 Split into groups of 8.
Ḅ Convert from binary (vectorizes).
Ọ chr() (vectorizes).
Charcoal, 17 bytes
F⪪E✂θ²Lθ¹⌕θι⁸℅↨ι²
Try it online! Link is to verbose version of code. Explanation:
✂θ²Lθ¹ Slice first two characters from input
E ⌕θι Replace each character with its index in the input
⪪ ⁸ Split into groups of 8
F Loop over each group
↨ι² Convert from binary
℅ Convert from code to character
Implicitly print
Python 3, 99 86 bytes
lambda s:[chr(int(str(list(map(s.find,s[i:i+8])))[1::3],2))for i in range(2,len(s),8)]
Thanks to ASCII-only for basically the whole thing really
J, 17 13 Bytes
u:_8#.\2}.1{=
-4 thanks to FrownyFrog
Old version:
u:_8#.\2&({.i.}.)
Explanation:
u:_8#.\2}.1{=
= | Self classify, for each unique element x of y, compute x = y, element-wise
1{ | Second row
2}. | Drop 2
_8#.\ | Convert non-intersecting subarrays of length 8 from binary
u: | Convert to characters
Examples:
= 'ABAABBAAAAAABBAAABAABBAABA'
1 0 1 1 0 0 1 1 1 1 1 1 0 0 1 1 1 0 1 1 0 0 1 1 0 1
0 1 0 0 1 1 0 0 0 0 0 0 1 1 0 0 0 1 0 0 1 1 0 0 1 0
2}.1{= 'ABAABBAAAAAABBAAABAABBAABA'
0 0 1 1 0 0 0 0 0 0 1 1 0 0 0 1 0 0 1 1 0 0 1 0
_8#.\2}.1{= 'ABAABBAAAAAABBAAABAABBAABA'
48 49 50
u:_8#.\2}.1{= 'ABAABBAAAAAABBAAABAABBAABA'
012
