Last active
December 27, 2015 03:09
-
-
Save revmischa/7257619 to your computer and use it in GitHub Desktop.
Perl golf. See: http://scottlocklin.wordpress.com/2013/10/31/shannon-information-compression-psychic/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
y/01/_*/,print for map{sprintf("%011b\n",$_)}(32,32,80,136,260,1539,260,136,80,32,32) | |
# 41558682057254037406452518895026208 = bitmap of 0=_,1=*, packed as 11-bit lines | |
# 11-bit chunks: 32 32 80 136 260 1539 260 136 80 32 32 | |
# %011b gives: | |
# 00000100000 | |
# 00000100000 | |
# 00001010000 | |
# 00010001000 | |
# 00100000100 | |
# 11000000011 | |
# 00100000100 | |
# 00010001000 | |
# 00001010000 | |
# 00000100000 | |
# 00000100000 | |
# which can be turned into _s and *s with the transliteration operator, y/// | |
# bitmap-packing code | |
; | |
use bigint; | |
my $encoded = ''; | |
my $cs = 11; # do 11-bit chunks representing each line | |
for (my $i=0; $i < $cs*11; $i+=$cs) { | |
# mask on only bits that are set and in this current 11-bit chunk | |
my $mask = 0; | |
for (my $j=0; $j < $cs; $j++) { | |
my $bit = 1 << $i + $j; | |
$mask |= $bit; | |
} | |
my $chunk = 41558682057254037406452518895026208 & $mask; | |
$encoded = sprintf("%i", $chunk >> $i) . ',' . $encoded; | |
} | |
warn $encoded; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment