0
\$\begingroup\$

Description

Here's a challenge with a somewhat practical use, at least for us golfers.

Your mission, should you choose to accept it, is to write a program that can take arbitrary binary data (either from stdin, or a file) and output some valid source code which provides access to the data. The generator and target languages need not be the same.

Scoring

Your program will be run against two inputs: a 1 KiB file of random characters, and the lyrics of Never Gonna Give You Up by Rick Astley. The program with the lowest score wins. Your score is calculated as follows:

(A + B) × loge(Z)

Where:

  • A is the number of bytes in the output for the random data;
  • B is the number of bytes in the output for Never Gonna Give You Up; and
  • Z is the number of useful bytes in the target programming language (useful is a very broad definition and may be refined in the future)

For example:

  • in C++ the generally allowed characters are "\t \r\nABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!\"#%&'()*+,-./:;<=?[\\]^_{|}~" which would give Z = 94

  • in Brainfuck the only non-ignored characters are "<>+-.,[]" which gives Z = 8

Examples

Two examples in Python, both generating python source:

Python allows files encoded in UTF-16, so the valid python bytes are from 0x00-0xFF

Using Repr

import sys
print 'data=',repr(sys.stdin.read())

Score: (2898 + 2098) × loge(256) = 27,704

Using Base64

import sys
from base64 import b64encode
print 'from base64 import b64decode;data=b64decode("'+b64encode(sys.stdin.read())+'")'

Score: (1416 + 2640) × loge(256) = 22,491

Data

The data is given here encoded in base64, these files should be decoded prior to passing to your program. The decoding is outside of the scope of this challenge.

Random

2moE8CGQCSSr8V4uHaeKMvtJUtStqVwvgn3xGgCWIlRWaEjVTLmF/fn0heXYh7GIPAUSZmEa2pR5
KTuLfLVtPMi8eUg5903zd/b57BIVJEPGVOyBhzxGTlzYEVG3uaUwnSOlpw9zP5SK/vjDNDCdF4IJ
ZBg8cHloaoxqw1Bm306GV6v6/Gzx+MHcCOsUyT0MyqOAgbuA4t568QTT82zd/nbZQ2BE9Q6bmIwo
tfBCTPwV0TShetCufxZJBbnVrNlDW8BDnKMGzqws/dJT+C03ndd64sszj/Quvri74Earfg6/07ea
Gh5KY8onMhpRiB/k/M6aHpA/glmaFNE5Gu0TsKVgNCHUCP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aHVydCB5b3UNCg0KTmV2ZXIgZ29u
bmEgZ2l2ZSB5b3UgdXANCk5ldmVyIGdvbm5hIGxldCB5b3UgZG93bg0KTmV2ZXIgZ29ubmEgcnVu
IGFyb3VuZCBhbmQgZGVzZXJ0IHlvdQ0KTmV2ZXIgZ29ubmEgbWFrZSB5b3UgY3J5DQpOZXZlciBn
b25uYSBzYXkgZ29vZGJ5ZQ0KTmV2ZXIgZ29ubmEgdGVsbCBhIGxpZSBhbmQgaHVydCB5b3UNCg0K
TmV2ZXIgZ29ubmEgZ2l2ZSB5b3UgdXANCk5ldmVyIGdvbm5hIGxldCB5b3UgZG93bg0KTmV2ZXIg
Z29ubmEgcnVuIGFyb3VuZCBhbmQgZGVzZXJ0IHlvdQ0KTmV2ZXIgZ29ubmEgbWFrZSB5b3UgY3J5
DQpOZXZlciBnb25uYSBzYXkgZ29vZGJ5ZQ0KTmV2ZXIgZ29ubmEgdGVsbCBhIGxpZSBhbmQgaHVy
dCB5b3U=
\$\endgroup\$
  • \$\begingroup\$ @PeterTaylor Excellent point, I'll update it. \$\endgroup\$ – tecywiz121 Jan 7 '14 at 19:38
1
\$\begingroup\$

PHP 8986

<?php
$data = file_get_contents('php://stdin');
printf('$var=gzuncompress(base64_decode("%s"));', base64_encode(gzcompress($data)));

Binary 1417

$var=gzuncompress(base64_decode("eJwBAAT/+9pqBPAhkAkkq/FeLh2nijL7SVLUralcL4J98RoAliJUVmhI1Uy5hf359IXl2IexiDwFEmZhGtqUeSk7i3y1bTzIvHlIOfdN83f2+ewSFSRDxlTsgYc8Rk5c2BFRt7mlMJ0jpacPcz+Uiv74wzQwnReCCWQYPHB5aGqMasNQZt9Ohler+vxs8fjB3AjrFMk9DMqjgIG7gOLeevEE0/Ns3f522UNgRPUOm5iMKLXwQkz8FdE0oXrQrn8WSQW51azZQ1vAQ5yjBs6sLP3SU/gtN53XeuLLM4/0Lr64u+BGq34Ov9O3mhoeSmPKJzIaUYgf5PzOmh6QP4JZmhTRORrtE7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vgM3N0AvEaBA4KUdg46yRVtuZDLQTwzfvpSctHGDbu0QtJuhRz195JO8x8ohEIMPTlGPzkNgsHzCsnjlCAfiCYJBcNGiB0UaPINmqMtiC120FloexyGMSKHyMBkVoK5G2TPQYaAeuFucJo5WUxWgx5BUD53jURxnYDnAQR/nsqjhLfoAkyJSEFDsZ9lrBCKZdohWPMtixyEoGNUBsx5JC3kQurIoi2c8JTtOFv1wOpcc14IVirSueaK14iKBciyeuV2NHl2uXZd9HN66ea14NcsX62bGtZD7kaMHzM4mXnzLYo+VgKZpE6BqzXkmHbs0b8upaZmiA4PKpUE3iILZmjTiJoaspigDS5ZmTikmjkGUmvygESaERlyXSFNOF5eec06UKD4D79W4YE75iiwdHO3ZcyvC0OX+lT+AlN5k9MQp7dzzmCd1wWtE1xkQ8V7zukfmhxc2j9e0m+/XNrctPgJ9WZHS3Q/tvH4Bd/EA/u6v6p/595nfAV47k5c="));

Z 94

Assumed the same character set as given for C++ in the question.

There's not really much wiggle room on how to accomplish this aside from compression and base64 encoding. The only "step up" I can think of would be finagling base128 encoding, but there aren't enough "safe" characters to accomplish this given the variability of extended-ASCII.

\$\endgroup\$
  • \$\begingroup\$ is the output in php? then it is not valid source without at least <? \$\endgroup\$ – Einacio Jan 8 '14 at 16:57
0
\$\begingroup\$

Ruby

Long version:

require 'tempfile'
tf=Tempfile.new('r')
while $stdin.gets{tf.puts $_}
tf.rewind
print "data='"
print IO.read(tf).gsub(/'/,"\\'")
print "'"

Golfed-up version

require 'tempfile'
t=Tempfile.new('r');while gets{t.puts $_};t.rewind;p "data='"+IO.read(t).gsub(/'/,"\\'")+"'"
\$\endgroup\$
  • \$\begingroup\$ When I run this on either input, I just get 'data=' followed by the raw binary, which doesn't work in Ruby. \$\endgroup\$ – tecywiz121 Jan 7 '14 at 20:26
  • \$\begingroup\$ @tecywiz121 you're right, modified to put the binary between "'" \$\endgroup\$ – user11485 Jan 7 '14 at 20:54
  • \$\begingroup\$ What if the data contains a single quote? \$\endgroup\$ – tecywiz121 Jan 7 '14 at 21:02
  • \$\begingroup\$ @tecywiz121 I suppose it matters (though none of the test data, as binary, should contain a plaintext single quote), so I added a gsub(/'/,"\\'") \$\endgroup\$ – user11485 Jan 8 '14 at 0:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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