Do you have any tips for ing in 05AB1E, a golfing language created by Adnan?

Your tips should be at least somewhat specific to 05AB1E.

Please post one tip per answer.

13 Answers 13

Implicit input

Back in the days when 05AB1E was released, implicit input was quite new and fancy. Nowadays it seems to be necessary in order to keep track with other competitive languages (like Jelly, MATL, Pyth, etc.).

For example, when you want to add two numbers, you can do II+:

I    # Input_1.
 I   # Input_2.
  +  # Add them up.

Test it here


However, using implicit input, we can shorten in to just 1 byte, namely +:

+    # Take two numbers implicitly and add them up.

Test it here


This only happens when the length of the stack is smaller than the arity of the operator. A last example is 3+. The arity of the + operator is 2 while there is only 1 element in the stack:

3    # Push the number 3 on top of the stack.
 +   # Request implicit input and add to the 3.

Test it here

Since it wasn't part of the Wiki on 05AB1E's GitHub pages (I think it should), I'm just gonna add it here now that I better understand it myself.

How to use the dictionary?

05AB1E has the following words.ex dictionary file containing all the words it knows. But how do we access the words in this dictionary? Let's take the word "testing" as example:

"testing" can be found on row 1453 of the dictionary file. Since the first two lines aren't words, and we need the 0-indexed word, we subtract 3.
So, now we have the index (1450), but how to use it?

We open and start a compressed string with . We then look at the second column of the info.txt file. (So is 00; is 01; etc.)
In the case of "testing" this means î (14) and » (50).

The compressed String for "testing" is therefore: “ Try it online. As with almost all 05AB1E pieces of code, the trailing is optional if you don't access the string, so without works as well in this case.

Some things to note:

All characters that doesn't have any index in the info.txt file can be used as is. This can be useful for adding an s to output a plural instead of singular word or use punctuation like ,.?! for example.
ÿ (string-interpolation) can also be used when you want to insert values from the stack within the string.
NOTE: Every loose character that doesn't have any index in the info.txt file counts as a word for the compression types below.

There are different types of compressed strings you can use:

  • ': Take a single compressed word as is (no trailing ' required) - 'î»: "testing"
  • : Takes two compressed words with space delimiter (no trailing required) - „î»î»: "testing testing"
  • : Takes three compressed words with space delimiter (no trailing required) - …î»î»î»: "testing testing testing"
  • : Take the compressed string with space delimiter - “î»î»“: "testing testing"
  • : Take the compressed string as is without implicit spaces - ’î»î»’: "testingtesting"
  • : Take the compressed string in title-case with space delimiter - ”î»î»”: "Testing Testing"
  • : Take the compressed string in full uppercase with space delimiter - ‘î»î»‘: "TESTING TESTING"

Here a useful program to get the compressed string based on a space-delimited input of words:

'“? lAð«Ã#¸˜ vyU "€‚ƒ„…†‡ˆ‰Š‹ŒŽ•–—™š›œžŸ¡¢£¤¥¦§¨©ª«¬®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîï" Dâ vy"“ÿ“".V XlQi y?1#] '“,

Try it online.

This program will:

  1. Take the input as lowercase, removes any non-alphabetic characters (except for spaces), and then splits the words by spaces (lAð«Ã#), or wraps the words in a list if only a single word was inputted (¸˜)
  2. Loops over each word (vyU)
  3. Then has an inner loop over each compressed word from the dictionary ("€...ï"Dâvy), which it will try to run as 05AB1E program ("“ÿ“".V)
  4. And if it's equal to the current word it will print it and break the inner loop XlQiy?1#

With an input good bye world the output would therefore be “‚¿Þ¡‚ï“. Try it online.

NOTE: You'd still have to see if the word exist in the dictionary in order for this generator to work, and it will ignore any special characters or plural words. Only the words that are exactly the same in the dictionary will be found.

Here an example where I use …Ÿ™‚ï! for the string "hello world!" and ’‚¿Þ¡ ÿ ‚ï!’ for the string "goodbye ÿ world!". Note how the spaces and exclamation mark are used as is, because they don't have indices in the info.txt file. In addition, it uses ÿ to insert the "cruel" that was at the top of the stack, which unfortunately wasn't part of the dictionary (but was still compressed using the method at the section below).

How to compress strings not part of the dictionary?

Although the words.ex dictionary file is pretty big (10,000 words to be exact), it can happen that you need a word which isn't part of it, or a string which is just plain gibberish. So is there a way to compress those as well?
There certainly is, by using .•, which is a compressed base-255 alphabet based string. NOTE: This method can only be used for characters in the lowercase alphabet.

Here is a useful program to convert a word/string to the compressed base-255 alphabet based string:
SAsk> 27β 255B ".•ÿ•".

What this program above does is:

  • SAsk>: Take the 1-indexed alphabet indices of the individual letters of the input
  • 27β: Convert these indices from base 27 to a single number
  • 255B: Convert this number to Base-255 using 05AB1E's own code page
  • ".•ÿ•": Places a leading .• and trailing before this compressed string

Here an example answer where @Kaldo uses .•zíΘ• to compress the word "goose".

How to compress large integers?

Let's say we want to use a very big number for something, but it can't really be retrieved by pow-calculations. For example, let's say we want to access the large integer 18238098189071058293 for whatever reason.

In this case we can use both a leading and trailing to compress a number in the format [1-9][0-9]+.
The example number above will become •15Y₁Ò'Θpc•. Try it online. Again, just like with the compressed dictionary string, the trailing can optionally be removed.

Optionally, when the integer is small enough so only 2 compressed characters have to be used, we can use Ž instead, in which case we won't need a trailing byte to close it. For example, the integer 13562 would result in •rl•, but because it only uses two characters, it can be Žrl instead.

How are these 15Y₁Ò'Θpc and rl created? Very simple, the number is converted to Base-255 using 05AB1E's own codepage. So you can use the following program to get a compressed number:
255B Dg2Qi "Žÿ" ë "•ÿ•"

Here an example answer where @Emigna uses •3Èñ• for the integer 246060.

How to compress integer lists?

Sometimes you want to compress an entire list of integers instead of a single number. For example, let's say we want the list [5,93,17,83,4,44,32,19,4,45,83,90,0,14,3,17,17,81] for whatever reason. In this case, we can use the following instead: •4βŸ{©£MG]q‡dZΘp•94в Try it online.

Here a useful program to generate both this compressed number and the Base we want to convert to: Z>© β 255B ®s"•ÿ•ÿв"

What this program above does is:

  • Z>: Get the max number + 1 of the input-list (©: and store it in the register)
  • β: Convert the input list from base max+1 to a single number
  • 255B: Compress this single number (as we did at the section above)
  • ®s"•ÿ•ÿв": Returns the result in the format: leading , compressed number, , max+1, trailing в

Here an example answer where I use •4Œ”dóŒfÝŸĀTUÕáOyÖOÀÁàu¼6¹₆Žr‡_›y³eß₂©ǝ²ƶ"SAÎAñ'¡û†Ø(•91в to compress the list [85,30,29,39,28,37,33,88,31,40,34,89,35,41,32,90,36,38,42,43,44,60,45,61,46,62,47,63,48,64,49,65,81,50,66,51,67,52,68,53,69,86,54,70,87,55,71,56,72,82,57,73,79,80,58,74,59,75,76,77,78,83,84].
PS: In this answer •6j|eDEJÞó(ÍêΓλùÄÞKüzHÇ-ø`JδŠ₂+Öηôî®À8†6/ðÎ6ùøΓ°ÓĆ;ˆ©Ā•2ô is an equal-bytes (57) alternative, since all numbers have exactly two digits. In some cases (especially small lists) this can be a shorter alternative.

Integer compression vs Integer list compression:

With these two it can go either way. Sometimes a compressed list is shorter, sometimes a compressed integer, sometimes a completely different alternative is shorter. So always use your own judgment and golfing skills to possibly golf things further, instead of relying on the above generators completely. Here some examples:

[44, 59] (used in this answer of @Emigna):

[2,4,6,0] (used in this answer of @Emigna):

  • •3ā•7в is 6 bytes (generated by the compressed integer list generator)
  • Ž3ā7в is 5 bytes
  • But in this case, Ž9¦S with 4 bytes would be the best option (compressed integer 2460 to a list of digits)

10101001100101001 (used in this answer of mine):

  • •a½₄Ƶ6®í• is 9 bytes (generated by compressed large integer generator)
  • •1∊}•2вJ is 8 bytes (generated by compressed integer list generator with added join)
  • But in this case •1∊}•b with 6 bytes would be the best option (compressed integer list, with a to binary instead of , which implicitly joins)

Substrings

£ is the command for taking the first b characters of string a.
ex: "hello_world"5£ -> "hello"

But if b is a list of indices it instead splits the string into parts of (upto) those sizes.
ex: "hello_world"5L£ -> ['h', 'el', 'lo_', 'worl', 'd']

Predefined variables

They are a bit hidden in 05AB1E. Here's a list of all the predefined variables:

  • ¾, pushes 0.
  • X, pushes 1.
  • Y, pushes 2.
  • ®, pushes -1.
  • 18
    ¾ pushes 0 => that's about as non-mnemonic as it gets – Fatalize Oct 16 '16 at 12:23
  • 5
    @Fatalize: 0 pushes 0 as well. ¾ pushes a counter variable which is initialized as 0. If you only want to push 0, 0 is of course more natural, but if you want to push 5,0,7, 5¾7 is 2 bytes shorter than 5 0 7. – Emigna Oct 16 '16 at 12:31
  • 7
    Back in my day, ¾ meant .75, and I once beat Pyth with that fact. These newfangled golfing languages don't have a clue about mnemonics... – ETHproductions Oct 16 '16 at 14:10
  • 26
    No idea what you all are talking about :p. print(3 / 4) in Python 2 gives me 0. – Adnan Oct 16 '16 at 14:13
  • If at the beginning, M pushes -Inf. – mbomb007 Mar 8 '17 at 21:23

Pop or get

As in other stack-based languages, 05AB1E's functions usually pop (consume) their inputs from the stack and push their outputs onto the stack.

However, some functions get their inputs from the stack without consuming them. An example is the head function, ¬, which produces the first element from the input list. See an example program here: ¬+. This adds the first number of the input list to each number of that list.

To know which functions pop and which get, see the corresponding column in the function information file.

  • @NeilA. Thanks! Link updated – Luis Mendo May 21 '17 at 2:12

Conditionals and loops

Loops and conditionals automatically receive closing brackets at the end of a program, so you only need to add them in the code if you need to something outside of the loop/conditional.

For example, this (ungolfed) program creating a list of the first n prime numbers do not need closing brackets. [¹¾Q#NpiNˆ¼

But if we wanted to perform some operation on the resulting list, for example taking delta's we'd need to close the loop first. [¹¾Q#NpiNˆ¼]¯¥

Automatic vectorization

Note that some operators in 05AB1E vectorize automatically on arrays. For example, the code 5L3+, which disassembles to the following pseudocode:

[1, 2, 3, 4, 5] + 3

would become:

[4, 5, 6, 7, 8]

If it doesn't vectorize automatically, you can also use the operator. It takes a single character command, and performs that (monadic) operator on each element. An example to split each element is the following code (try it here):

€S

Whereas the normal S operator would split each element in the array and flattens it into a single array (try it here).

  • How do you assign to nth element in an array? – Andrew Savinykh Jan 2 '17 at 10:53
  • @AndrewSavinykh Right now, there is no builtin for that, but it is something that I want to implement. – Adnan Jan 2 '17 at 17:04
  • @Adnan I found a way to do it. Create another list that has the value to assign also at the nth index. Then merge the lists using ñ preceded by the value of n (the index). tio.run/nexus/05ab1e#@2/iw2XiE2Tio81ldHjj//8A – mbomb007 Mar 8 '17 at 20:57
  • @mbomb007 That is possible, the only problem is that you cannot modify the array afterwards, since the merge command only takes strings as arguments (and converts the list to a string). – Adnan Mar 8 '17 at 21:55

Small 05AB1E golfing tips

Will expand this with small golfing tips I learned along the way. (Only just started 05AB1E personally.)

  • D (duplicate) and Ð (triplicate) in combination with s (swap) and Š (triple-swap a,b,c to c,a,b) are usually shorter than using © (save in global_variable) and ® (push global_variable) inside loops. This saved a byte in this answer of mine, as well as two in this answer of mine.
  • ½ (if 1, then increase counter_variable by 1) isn't necessary at the end of a µ (while counter_variable != a, do...), since it's done implicitly (saved a byte in this answer of mine).
  • .B implicitly splits on new-lines. This was useful in this answer of mine when we were looking for an alternative for ¡ (split) while still keeping empty items (NOTE: Solution in the linked answer doesn't work when elements contain trailing spaces after splitting.) - Hopefully a builtin will be added to split but keep empty lines in the future.
  • (which of the digits of the input-integer can evenly divide the input-integer) will contain the number itself for the digits 0 (instead of divide-by-zero errors). For example, 1053 will result in [1,1053,0,1] (1053 is divisible by 1 and 3; is not divisible by 5; and gives a division-by-zero error for 0). This was pretty useful in this answer of mine by taking the power of the list, since only 1 is truthy in 05AB1E and everything else is falsey. SÖP resulting in truthy (1) therefore means an input-integer is evenly divisible by each of its digits.
  • After seeing û (palindromize a given string) I was surprised there isn't an is_palindrome builtin. But later on I realized only 2 bytes are needed to accomplish that, which are ÂQ (where  is bifurcate, which is short for DR: Duplicate & Reverse copy; and Q is to check if the top two values on the stack are equal).
  • * has a hidden feature of repeating a certain character/string n amount of times. I used to use s∍ to lengthen repeat a single character b an a amount of times (like this: 'xs∍), but apparently * does the same and it doesn't matter if the stack order of the number and string is a,b or b,a: Try it online: 'x*. (NOTE: Only works in the Python legacy version, not anymore in the Elixir rewrite.)
  • When you want to filter a list by multiple things, it's usually cheaper to have multiple loose filters rather than all combined in one. Because when you have two filters you'll need something along the lines of Ds* (duplicate, swap, multiply to act as logical-AND) vs (close first filter, filter again) when you use two filters. For example: in this challenge we have to list all numbers of four digits long, containing at least one 0, and with a digit sum equal to 9. Using a range [1000,10000] covers the number of four digits long, but then you are left with two more filters. Initially I used ₄4°ŸʒD0åsSO9Q* (14 bytes), but by using two filters a byte can be saved: ₄4°Ÿʒ0å}ʒSO9Q (13 bytes).
  • When you want to zip with integer 0 as filler, you could use . One issue with this however is that the filler 0 will become a string "0", so if you later try to sort with mixed strings and integer, this will most likely not give the result you'd want. Here an example of how it will sort the zipped inner lists: 0ζ€{. This can be fixed by adding an explicit cast to int (ï) after the zip, and only then sort: 0ζï€{. However, using the ¾ as constant 0 with the zip-filler, will cause it to remain an integer instead of a string during the zip. So ¾ζ€{ will save a byte here. This tip was provided by @Mr.Xcoder to save a byte in this answer of mine.

Using the Canvas (Λ or )

Since it wasn't part of the docs, and @Adnan is currently a bit too busy to write it, I asked permission to add it as a tip here for now.

The Canvas function (Λ or ) can be used to draw ASCII-lines on the screen. It has three required parameters:

  • a Length: The size of the line(s). This can be a single integer, or a list of integers
  • b String: The character(s) we want to display. This can be a single character, a string, or a list of characters (in the last two cases it will use all of them one-by-one including wrap-around)
  • c Direction: The direction the character-lines should be drawn in. In general we have the digits [0,7] for the directions, for which we can use one or multiple. There are also some special options that require a certain character (more about that later).

The direction-digits [0,7] map to the following directions:

7   0   1
  ↖ ↑ ↗
6 ← X → 2
  ↙ ↓ ↘
5   4   3

Some example 05AB1E answers where the Canvas is used:

Let's do something similar as the last one, so assume we use the Canvas Λ function with the following three parameters:

  • a: [3,3,5,5,7,7,9,9]
  • b: !@#
  • c: [0,2,4,6]

This will give the following output:

  !@#!@#!
  #     @
  @ #!@ #
  ! @ # !
  # ! ! @
  @   @ #
  !#@!# !
        @
@!#@!#@!#

Try it online.

So how does it work? Well, here are the steps with these inputs above:

  1. Draw 3 characters (!@#) upwards (direction 0)
  2. Draw 3-1 characters (!@) towards the right (direction 2)
  3. Draw 5-1 characters (#!@#) downwards (direction 4)
  4. Draw 5-1 characters (!@#!) towards the left (direction 6)
  5. Draw 7-1 characters (@#!@#!) upwards (direction 0)
  6. Draw 7-1 characters (@#!@#!) towards the right (direction 2)
  7. Draw 9-1 characters (@#!@#!@#) downwards (direction 4)
  8. Draw 9-1 characters (!@#!@#!@) towards the left (direction 6)

The -1 are there because the lines overlap. So the first two steps are:

#
@
!

And

 !@

Which combined is:

#!@
@
!

Some minor notes:

  • I wasn't even aware that 05AB1E had a canvas! – MilkyWay90 Nov 11 at 23:10

05AB1E ASCII-Art Golfing

The below code helps turn ASCII-art into 05AB1E using base compression and transliterate.

|»©ÐÙSDŠ¢øΣθ}R€н¬®sÅ?iD2£RDŠKsì}J©žLR‡®gö₅B"•ÿ•"®"“ÿ“ÿ"®g"ÿвèJ"«

Try it online.

This is accomplished by:

  1. Listing out the unique characters in the ASCII drawing.
  2. Order them with the ones occurring most first so the compressed integer is as small as possible.
  3. Reverse the first two items if the ASCII drawing starts with the most occurring character (to prevent leading 0s in the compressed integer).
  4. Map the characters of the input to 0-9A-Za-z in that order, each distinct character getting its own mapping-character, until every one has been replaced.
  5. Base compress it, using the highest base you needed to replace (based on the amount of unique characters).
  6. Base convert it again to base-255 (for 05AB1E compression).
  7. Format everything in the format: “<sorted_distinct_characters>“•<compressed_integer>•<integer>вèJ.

The allows you to also compress string-quotes "; the •<compressed_integer>•<integer>в will generate a list of integers; è will index these integers in the string “<sorted_distinct_characters>“; and J will join all these characters together to a single string, which is output implicitly.

Accepts patterns with up to and including 62 unique characters, good for ASCII-art.
The less amount of unique characters, the better the compression.


Example output for Draw the XNOR digital timing diagram (214 bytes, 9 unique characters):

    ┌─┐ ┌─┐ ┌─────┐ ┌─┐ ┌─┐ ┌───┐  
A ──┘ └─┘ └─┘     └─┘ └─┘ └─┘   └──
  ┌───┐ ┌───┐ ┌─┐ ┌─────┐   ┌─┐ ┌─┐
B ┘   └─┘   └─┘ └─┘     └───┘ └─┘ └
    ┌─────┐   ┌─┐   ┌─┐   ┌───┐   
X ──┘     └───┘ └───┘ └───┘   └────

Would be:

05AB1E, 108 bytes

“─ └┘┐┌
XBA“•I£.µ*:]ó±øqaµb₄ΘYQmœ¹µû₄p´ζÂĆ_5ŠKÑ×ðòË|₄#¹¶úôÂ-Í|¯ε¼É₂ïδ&é–9»ÞFò1î×HÃBjý2ĆÉ≠FYÂÂèC j‘£Å₅Œ•10вèJ

Try it online.

(108/214)*100 = 50.47% the size of the original ASCII-art string.

Which is only 2 bytes longer than my actual submission for that challenge in 05AB1E.


Code explanation:

NOTE: Code is absolutely not golfed. It's quickly written to convert ASCII art to the most efficient compression, so it's quite ugly and long..

|»               # Take multi-line input
  ©              # Store it in the register to reuse later                         
ÐÙS              # Only leave unique characters (as list)
   DŠ¢ø          # Map it with the count for each of those characters
       Σθ}R      # Sort it based on that count (highest to lowest)
           €н    # Remove the count again, so the sorted characters remain
¬®sÅ?i           # If the input starts with the most occurring character:
      D2£RDŠKsì} #  Swap the first two characters in the list
J©               # Join everything together, and store it in the register to reuse later
  žLR‡           # Map each character to [0-9A-Za-z]
      ®gö        # Get the amount of unique characters, and convert it to that Base
         ₅B      # And then convert that to Base-255
"•ÿ•"            # Put that compressed integer between •
     ®"“ÿ“ÿ"     # Put the string between “, and append the compressed integer
®g"ÿвèJ"         # Create a string with the amount of unique characters, followed by "вèJ"
        «        # And append it to the string and compressed integer, and ouput implicitly
  • 1
    By the way, since 05AB1E has changed the code page, the maximum base is changed from 214 to 255. – Adnan May 17 '17 at 7:21
  • 1
    Perhaps something to add to your answer (or modify the generator with), but if less than 10 distinct characters are being used in the ASCII art, you can golf it by two bytes. I.e. your generator gives this 22-byter, but it can be this 20-byter instead. – Kevin Cruijssen Oct 29 at 11:10
  • @KevinCruijssen the idea was what I was trying to convey, not really claiming that generator is anything good :P. I honestly doubt it even still runs on osabie. I wrote that a looong time ago! – Magic Octopus Urn Nov 2 at 18:53
  • @MagicOctopusUrn Not sure if it runs in the Elixir rewrite, but it certainly still works in the legacy version. I have already edited the Base-214 to Base-255 about halve a year ago as was mention in Adnan's comment above. Apart from that it works great and I've used it a few times (although golfed it every time further. ;) ). The generation of both the string and number work great! – Kevin Cruijssen Nov 3 at 10:32
  • Here is an improved version. (Very ugly and fastly written, but it works). It would make your example 108 bytes instead of 113. The improvements I did are: sort the distinct characters on highest occurrence first (unless the highest occurrence is the first character, in which case it'll swap the top two characters) so the compressed integer is as small as possible; using <str><compr_int><int>вèJ instead of your <compr_int><int>BžLR<str>‡; and using instead of " as string-quotes, so " can be part of the input. – Kevin Cruijssen Dec 5 at 9:22

Strings and ints are equal types

Not something that everybody agrees with, but it works.

Consider the following two programs:

4 5+
"4""5"+

They both result into 9. That is because every value is first evaluated (with ast.literal_eval). Because of that, we can perform all string manipulation operators on ints and all int manipulation operators on strings.

For example, 12345û palindromizes the number 12345, resulting in 123454321. After that, we can do the regular math on this number.

12345û50000-

This would result into: 123404321.

Ordering of the Inputs

The order that you take input can have a drastic effect on your code, and, oftentimes if you're using s to swap the top of the stack with the next highest thing on the stack, you're not thinking about the problem correctly. Try to reorder inputs and see if you can get rid of the need to swap by either swapping the inputs ahead of time, adding it to the stack earlier on or duplicating it somewhere. The most obvious I&O can be the least successful 05AB1E answer.

Hidden loops and iterators

05AB1E has the following normal loops and iterators:

  • F, which iterates through 0 .. n-1.
  • G, which iterates through 1 .. n-1.
  • ƒ, which iterates through 0 .. n.
  • v, which iterates over each element s[0], s[1], .., s[n].
  • ʒ, which is not exactly a loop, but a filter-by command. We abuse this command for it's unintended behaviour of looping through each element.

Using these loops, we can derive the following hidden loops:

  • Instead of gF, you can use v which also has an N-index that can be used.
  • The vy -> ʒ replacement is a bit more tricky:
    • You need to immediately print the results. This surpresses the automatic print from printing the top of the stack.
    • The code snippet is run on a new temporary stack. This means that stack-dependent snippets cannot be used.
    • Invoking y is not possible in these kind of loops.
  • I know this has been posted a year ago, but aren't [, µ and ε also part of the normal loops/iterations? – Kevin Cruijssen Jul 2 at 12:21
  • Also, invoking y is possible with some of these now. – Magic Octopus Urn Oct 22 at 16:19

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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