Advertisement
Guest User

Code Golf Program

a guest
Jun 6th, 2012
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.13 KB | None | 0 0
  1. <?php
  2. $map = array(
  3. "ever 6" => 'L',
  4. "
  5. I just wanna J9Z
  6. Gotta U9understand
  7. B" => 'C',
  8. "279up2let 9down2run arouHaHdesert you2U9cry2say goodbye2Ja lie aHhurt you"=>'B',
  9. "
  10. Never 6" => '2',
  11. "how I'm feelX"=>'Z',
  12. "
  13. We'vePn eachRfor so longQr hearTachX butQ'reKshy to say it
  14. Inside we bothP whaTgoX on
  15. WePSgaVaHwe're 6play it"=>'3',
  16. "8, 79up)"=>'4',
  17. "8)
  18. N1, n1
  19. (Give 9up)"=>'5',
  20. "ever 6give" => '1',
  21. 'gonna ' => '6',
  22. 'give ' => '7',
  23. "
  24. (Ooh" => '8',
  25. "you " => "9",
  26. "nd "=> 'H',
  27. 'tell '=>'J',
  28. ' too '=>'K',
  29. ' know'=>'P',
  30. "
  31. You"=>"Q",
  32. ' other ' => 'R',
  33. ' the ' => "S",
  34. "t's been " => "T",
  35. "make "=>'U',
  36. "me "=>'V',
  37. 'ing' => "X"
  38. );
  39.  
  40. $c = str_split(implode(array_values($map)));
  41. $r = explode(M,implode('M',array_keys($map)));
  42.  
  43.  
  44. //Flip the arrays
  45. $c = array_reverse($c);
  46. $r = array_reverse($r);
  47.  
  48. print_r($c);
  49.  
  50. function encode($input){
  51. global $c,$r;
  52.  
  53. $input=str_replace($r,$c,$input);
  54.  
  55.  
  56. return $input;
  57. }
  58. function decode($input){
  59. global $c,$r;
  60.  
  61. $input=str_replace($c,$r,$input);
  62. return $input;
  63. }
  64.  
  65.  
  66.  
  67.  
  68. function verify_match($real,$decoded){
  69. return ($real == $decoded);
  70. }
  71.  
  72. $real_lyrics = file_get_contents('lyrics.txt');
  73. $encoded = encode($real_lyrics);
  74.  
  75. print "\r\nENCODE------\r\n";
  76. print substr($encoded,0,150);
  77.  
  78. $fh = fopen('compressed.txt','w');
  79. fwrite($fh,$encoded);
  80. fclose($fh);
  81.  
  82. //Flip the arrays
  83. $c = array_reverse($c);
  84. $r = array_reverse($r);
  85.  
  86. $compressed = file_get_contents('compressed.txt');
  87. $program = '<?=str_replace(str_split('.implode(array_values($map)).'),explode(M,"'.implode('M',array_keys($map)).'"),"'.$compressed.'");';
  88.  
  89. $fh = fopen('program.php','w');
  90. fwrite($fh,$program);
  91. fclose($fh);
  92.  
  93.  
  94.  
  95.  
  96. print "\r\nDECODE------\r\n";
  97. $decoded = decode($encoded);
  98. print $decoded;
  99.  
  100. print "\r\nByte count?\r\n";
  101. print strlen($program);
  102.  
  103.  
  104. print "\r\nIs decoded the same as original?\r\n";
  105. var_dump(verify_match($real_lyrics,$decoded));
  106.  
  107. /*
  108. print "\r\nR sequence ----\r\n------\r\n";
  109. print implode('M',array_reverse(array_keys($map)));
  110.  
  111. print "\r\nC sequence ----\r\n------\r\n";
  112. print implode(array_reverse(array_values($map)));
  113. */
  114.  
  115. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement