16
\$\begingroup\$

Golfeek is the working name of an esolang I'm occasionally working on. One distinctive feature of this language is that each statement is encoded as a sequence of bits rather than a sequence of bytes.

Instead of just showing a long binary sequence, the 'gutter' of the source editor contains a representation of each statement in a more digest and (slightly) more readable format:

example Golfeek source code

Format

  • The binary sequence is always read from left to right.

  • It is first split into nibbles (groups of 4 bits) shown as uppercase hexadecimal between brackets. A space is inserted after each byte, provided that it's followed by another byte or a last lone nibble.

    Example:

    [1,1,1,1,0,0,0,0,1,0,1,0] --> "[F0 A]"
     \_____/ \_____/ \_____/
    
  • If there are more than 14 nibbles, only the first 6 bytes are shown, followed by a space, followed by .., followed by the closing bracket.

  • If the number of bits is not a multiple of 4, the remaining bit(s) is/are appended as '0' or '1' right after the closing bracket.

    Example:

    [0,1,1,1,1,0,0,1,0,1] --> "[79]01"
     \_____/ \_____/ \_/
    

Your task

Given a sequence of bits, return its representation in Golfeek format.

Rules

  • The sequence of bits may be taken in any reasonable and non-ambiguous format. Please note that the sequence may contain leading zeros, which your format must be able to deal with.

  • You can assume that there will always be at least 4 bits in the input sequence, and therefore at least one nibble between the brackets.

  • The output must be a string (or a list of characters) that conforms exactly to the format described above, with uppercase hexadecimal and correct spacing. A trailing linefeed is optional.

  • This is .

Test cases

Input

0101
00001
110101
10100111
101001111100
000000000000000
011110101101001110010011000111
11011110101011011011111011101111
11110101000110000001111011001110011100100110111000100000100
111101010001100000011110110011100111001001101110001000001001
1111010100011000000111101100111001110010011011100010000010010

Output

[5]
[0]1
[D]01
[A7]
[A7 C]
[00 0]000
[7A D3 93 1]11
[DE AD BE EF]
[F5 18 1E CE 72 6E 20]100
[F5 18 1E CE 72 6E ..]
[F5 18 1E CE 72 6E ..]0
\$\endgroup\$

10 Answers 10

5
\$\begingroup\$

05AB1E, 36 34 33 31 bytes

Ć4ô¤¨s¨Ch2ôJDg8@i6∍„..ª}ðý"[ÿ]ÿ

Try it online!

\$\endgroup\$
4
\$\begingroup\$

Jelly, 35 bytes

s4µLÐṀ©s14Ḅ‘ịØHUḢaȧ⁾..$ƊUs2KØ[j;ḟ®F

A full program which, given a list format of 1s and 0s, prints the result.

Try it online! Or see the test-suite.

\$\endgroup\$
4
\$\begingroup\$

Retina 0.8.2, 112 111 bytes

.{4}
$& 
(.+) 
[$1]
T`d`L`.*]
B
AB
+`BA
ABB
A+(B*)
$.1
T`d`L`1\d(?=.*])
B(.)
$1
T` `_`\w..
(\[.{18})...+]
$1..]

Try it online! Link includes test cases. Edit: Saved 1 byte thanks to @Grimmy. Explanation:

.{4}
$& 

Group the nibbles.

(.+) 
[$1]

Bracket the nibbles.

T`d`L`.*]

Change the bits used by the nibbles from 01 to AB to distinguish them from the spare bits.

B
AB
+`BA
ABB

Convert from binary to unary.

A+(B*)
$.1

Convert from unary to decimal.

T`d`L`1\d(?=.*])
B(.)
$1

Convert 10-15 from decimal to hexadecimal.

T` `_`\w..

Group pairs of nibbles into bytes.

(\[.{18})...+]
$1..]

Truncate the output if there are more than 14 nibbles.

\$\endgroup\$
  • 1
    \$\begingroup\$ When converting 10-15 to A-F, \d\d can be 1\d to save a byte: TIO. \$\endgroup\$ – Grimmy Dec 19 '19 at 16:38
3
\$\begingroup\$

JavaScript (Node.js), 129 124 113 bytes

-5 thanks @tsh 11 more thanks @Arnauld

s=>'['+s.replace(/..../g,(x,i)=>(i<56?"9ABCDEF."[x=!s[59]|i<48?"0b"+x-9:7]||x+9:"")+(s[i+7]?i&4&&x-7?' ':'':']'))

Try it online!

\$\endgroup\$
2
\$\begingroup\$

Red, 208 bytes

func[s][r: :rejoin b: parse s[collect[any[keep[4 skip]]copy t to end]]forall
b[b/1: pick form debase/base r["0000"b/1]2 4]b: collect[foreach[x y]b[keep
r[x any[y""]]]]if b/8[clear at b 8 b/7:".."]r["["b"]"t]]

Try it online!

\$\endgroup\$
2
\$\begingroup\$

Charcoal, 50 bytes

[≔⪪S⁴θF›⁴L§θ±¹≔⊟θω≔⪫⪪↥⭆θ⍘↨²ι¹⁶¦² θ⎇‹²⁰Lθ⁺…θ¹⁸..θ]ω

Try it online! Link is to verbose version of code.

[

Output the opening [.

≔⪪S⁴θ

Split the input into nibbles.

F›⁴L§θ±¹≔⊟θω

Save the last slice if it's not a whole nibble.

≔⪫⪪↥⭆θ⍘↨²ι¹⁶¦² θ

Convert each nibble into uppercase base 16, joining with alternate spaces.

⎇‹²⁰Lθ⁺…θ¹⁸..θ

If the resulting string is greater than 20 characters then truncate it to 18 and add .. otherwise output the string.

]

Output the closing ].

ω

Output any left-over bits.

\$\endgroup\$
2
\$\begingroup\$

Perl 5 (-p), 91, 89 bytes

-2 bytes thanks to Xcali++

s/.{4}/sprintf"%X",oct"0b$&"/eg;$\=$';s/$\$//;s-.{12}\K...+-..-;s-..(?=.)-$& -g;$_="[$_]"

Try it online!

\$\endgroup\$
  • \$\begingroup\$ Using $\ instead of $x would allow the last statement to be $_="[$_]", saving 2 bytes. This, however, would only allow it to process one input per run legibly. \$\endgroup\$ – Xcali Jan 16 at 22:58
1
\$\begingroup\$

Japt, 50 bytes

ò4
jJUÌʦ4
®n2G uÃò mq
"[]"i1WjT7 ¸h18WÊ?"..":P)+V

Try it

ò4            (Input as a string) partitioned on each 4 bits.

jJ             V = get n elements from last index and remove them
  UÌʦ4        n = is last element length != 4 ?

®n2G uÃò mq           Convert each nibble to base 16,
     uà       to uppercase,
       ò mq    and join each pair.

"[]"i1         Inserts between squares :
      WjT7      first 7 elements(removing them),
           ¸    joined with space.
h18             Then replace characters at index 18 with :
   WÊ?".."       '..' if not empty
          :P     else empty string.
)+V           Finally appends remaining bits and outputs implicitly.
\$\endgroup\$
1
\$\begingroup\$

C (gcc), 194 181 191 190 bytes

Thanks to ceilingcat (-14 bytes) for the suggestions. Fixed an edge case (+10 bytes).

The function takes the input string and builds up byte-sized representations a nibble at a time, outputting the results until the sixth byte, and changing the [ to a space after the first byte output (if there is one). The buffer is reset after each output.

After the sixth byte, nothing is output until the entire string is processed, but if the length exceeds 59 bytes (7 bytes + 3 bits), the stored representation changes to .., which is then output at the end. Excess bits that don't fit in a nibble are printed from the end of the input string. There is an edge case that I had to specifically check for, which is an input between 49 and 51 bytes (6 bytes + 3 bits), as an extra space would normally get printed before the ending bracket.

c,t;f(char*s){char u[4]="[";for(t=c=0;*s;c++>58?u[1]=u[2]=46:0)t+=t+*s++-48,c%4>2?u[c%8/4+1]=t+48+7*(t>9),t=0:0,c%8>6&c<48?printf(u),*(int*)u=32:0;printf("%s]%s",u+(c%8<1|c-48U<4),s---c%4);}

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Burlesque, 48 bytes

4co{L[4.<}ptl_FLjb2b62co)++sa7.>{6.+".."[+}if+]Q

Try it online!

4co       #Split into nibbles
{L[4.<}pt #Partition into those whose lengths >4 and those <4
l_FL      #Take the remainder as a flat list
jb2b6     #Convert the main block into base 16 (via binary)
2co)++    #Join as pairs
sa7.>     #Length greater than 7
{
 6.+      #Take first 6 discarding remainder
 ".."[+   #Append ".."
}if       #If length > 6
+]        #Append remainder bits
Q         #Prettify
\$\endgroup\$
  • \$\begingroup\$ @Arnauld Ah, thanks. I just missed that. \$\endgroup\$ – DeathIncarnate Jan 16 at 15:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.