#!/usr/bin/perl -w # Attempt dictionary replacements on ikaruga32.rb to see if that would be # sufficient to compress it below size threshold. use strict; use constant TOP_SAVING_THRESHOLD => 10; # Load input. open my $infile, "; close $infile; # Ignore header line. $lines[0] =~ /^#!/ or die; splice @lines, 0, 1; chomp foreach @lines; my $text = join ";", @lines; my @candidates = ( "if ", ";end", ";next", ";print ", " ? ", " : ", 'K+"', ".scan(/../)", ".map", ".product(", ".size", ".times", "f.call", "console", "b[y][x]", "=Time::now;", "STDIN.", # Insufficient savings ".abs", ".upcase", ".min", ".all?", ".ord", ".each", "rand", ); my @savings = (); foreach my $c (@candidates) { my $count = 0; my $offset = 0; for(my $i; ($i = index($text, $c, $offset)) >= 0; $count++) { $offset = $i + length($c); } my $s = (length($c) - 2) * $count - (length($c) + 2); print "[$c] = $count, saving = $s\n"; push @savings, $s; } @savings = sort {$b <=> $a} @savings; my $total_savings = 0; my $top_savings = 0; for(my $i = 0; $i < scalar @savings; $i++) { my $s = $savings[$i]; next if $s < 0; $total_savings += $s; if( $i < TOP_SAVING_THRESHOLD ) { $top_savings += $s; } } print "Total savings = $total_savings\n", "Top ", TOP_SAVING_THRESHOLD, " only = $top_savings\n";