Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

Return to Revisions

4 of 5
typo..
Kevin Cruijssen
  • 136.2k
  • 14
  • 154
  • 394

#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ö5B"•ÿ•"®""ÿ"ÿ"®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μb4ΘYQmœ1μû4p ́ζÂĆ_×ばつðòË|4#1¶úôÂ-Í| ̄ε1⁄4É2ïδ&×ばつHÃBjý2ĆÉ≠FYÂÂèC j‘£Å5Œ•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
 5B # 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
Magic Octopus Urn
  • 20.9k
  • 6
  • 66
  • 140

AltStyle によって変換されたページ (->オリジナル) /