Hacker News new | ask | show | jobs
by im3w1l 28 days ago
Representation for this I came up with: Allocate one magic number for "start line with space" another for "start line with asterisk" then any other number means this many of the current character and then swap character.

Then it's a matter of picking a suitable way to encode numbers into bits.

I came up with groups of four bits. If the high bit is not set that is the number, if it is set then read an additional group of four bits, and interpret as a biased 7 bit number. This came out to 400 bytes not including decompressor.

1 comments

> Allocate one magic number for "start line with space" another for "start line with asterisk" then any other number means this many of the current character and then swap character.

The galaxy brain move would be to always arbitrarily start with one character, replace the two special numbers with a single "don't output any characters, just swap" number, and then... congratulations, you just reinvented CompuServe RLE!

http://fileformats.archiveteam.org/index.php?title=CompuServ...

While the idea of using RLE with implicit swapping is the same, everything else is different. I'm not sure it's better in this case. One thing my version did is that it had explicit linebreaks, this approach was chosen as the map in the article had lines of varying lengths. If you don't now you have to accomplish that some other way, e.g. explicitly fill the line up with spaces and some mechanism of communicating the width of the image.

I also think using a varint encoding is a big win over fixed-width.