#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"«
This is accomplished by:
- Listing out the unique characters in the ASCII drawing.
- Order them with the ones occurring most first so the compressed integer is as small as possible.
- Reverse the first two items if the ASCII drawing starts with the most occurring character (to prevent leading 0s in the compressed integer).
- Map the characters of the input to
0-9A-Za-zin that order, each distinct character getting its own mapping-character, until every one has been replaced. - Base compress it, using the highest base you needed to replace (based on the amount of unique characters).
- Base convert it again to base-255 (for 05AB1E compression).
- 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:
"─ └┘┐┌
XBA"•I£.μ*:]ó±øqaμb4ΘYQmœ1μû4p ́ζÂĆ_×ばつðòË|4#1¶úôÂ-Í| ̄ε1⁄4É2ïδ&×ばつHÃBjý2ĆÉ≠FYÂÂèC j‘£Å5Œ•10вèJ
*(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
- 20.9k
- 6
- 66
- 140