Who had thought that even "die" could be made shorter ;-)
#!perl -lap
@t{@q{@F}=@F}.=" $F[0] "x!/^\Q$F[1] /}for(keys%q){1/(($_)=grep!$t{$_},%q);$t{$w=$_}+=map{s/ \Q$w //g}%t
|
I tried using grep a few times already. But suddenly it gave a gain %)
#!perl -lap
@t{@F}.=" $F[0] "if$F[0]ne$F[1];@q{@F}=@F}for(keys%q){(($_)=grep!$t{$_},%q)||die;$t{$,=$_}+=map{s/ \Q$, //g}%t
|
My last solution is actually invalid according to the rules (see seperate mail for a test case). Fix it.
#!perl -lan
@t{@F}.=" $F[0] "if$F[0]ne$F[1];@q{@F}=@F}for(keys%q){map$t{$_}||do{$t{$,=$_}+=print;map{s/ \Q$, //g}%t;next},%q;die;
|
My last submission got rejected. Have to try again...
#!perl -lan
@t{@F}.=" $F[0] "if$F[0]ne$F[1];@q{@F}=@F}for(keys%q){$t{$_}||$,or$t{$,=$_}+=print for%q;$,=$,?!map{s/ \Q$, //g}%t:die;
|
Creative use of ?: and loosing a space shoul put me in the top 10 for now. - Time for bed!
#!perl -lan
$t{$F[1]}.=" $F[0] "if$F[0]ne$F[1];@q{@F}=@F}for(keys%q){$t{$_}||$,or$t{$,=$_}+=print for%q;$,=$,?!map{s/ \Q$, //g}%t:die;
|
Use that nice 5.6.1 feature to modify hash values
#!perl -lan
$t{$F[1]}.=" $F[0] "if$F[0]ne$F[1];@q{@F}=@F}for(keys%q){$t{$_}||$d or$t{$d=$_}+=print for%q;$d||die;map{s/ \Q$d //g}%t;$d="";
|
Yet another way to write the if/for.
#!perl -lan
$t{$F[1]}.=" $F[0] "if$F[0]ne$F[1];@q{@F}=@F}for(keys%q){$t{$_}||$d||($t{$d=$_}+=print)for%q;$d||die;map{$t{$_}=~s/ $d //g}%t;$d="";
|
A new testcase steals two more characters from me.
#!perl -lan
$t{$F[1]}.=" $F[0] "if$F[0]ne$F[1];@q{@F}=@F}for(keys%q){$t{$_}||$d||($t{$d=$_}+=print)for%q;$d||die;map{$t{$_}=~s/ \Q$d //g}%t;$d="";
|
Reformatting the for/if gives a few more chars
#!perl -lan
$t{$F[1]}.=" $F[0] "if$F[0]ne$F[1];@q{@F}=@F}for(keys%q){$t{$_}||($t{$d=$_}+=print)and last for%q;$d||die;map{$t{$_}=~s/ $d //g}%t;$d="";
|
New try: we store strings inside those hashes. (So much work for only three points gain)
#!perl -lan
$t{$F[1]}.=" $F[0] "if$F[0]ne$F[1];@q{@F}=@F}for(keys%q){for(%q){if(!$t{$_}){print;$t{$d=$_}=1;last}}$d||die;map{$t{$_}=~s/ $d //g}%t;$d="";
|
Some more strokes shaved with s/values//;
#!perl -lan
$t{$F[1]}{$F[0]}=1if$F[0]ne$F[1];$t{$F[0]}{a}}for(keys%t){for(keys%t){!%{$t{$_}}&&print&&delete$t{$d=$_}&&last}map{delete$$_{$d||die}}%t;$d="";
|
I had completely forgotten about 'values'
#!perl -lan
$t{$F[1]}{$F[0]}=1if$F[0]ne$F[1];$t{$F[0]}{a}}for(keys%t){for(keys%t){!%{$t{$_}}&&print&&delete$t{$d=$_}&&last}map{delete$$_{$d||die}}values%t;$d="";
|
loose some "if". &&,|| and "and" are shorter
#!perl -lan
$F[0]ne$F[1]and$t{$F[1]}{$F[0]}=1;$t{$F[0]}{a}}for(keys%t){for(keys%t){!%{$t{$_}}&&print&&delete$t{$d=$_}&&last}map{delete$t{$_}{$d||die}}keys%t;$d="";
|
A few more easy optimisations: for->map, omit 'scalar' or 'keys' here and there.
#!perl -lan
$t{$F[1]}{$F[0]}=1if($F[0]ne$F[1]);$t{$F[0]}{a}}for(keys%t){for(keys%t){if(!%{$t{$_}}){print;delete$t{$d=$_};last}}die if!$d;map{delete$t{$_}{$d}}keys%t;$d="";
|
Try my file obfuscator for obvious gains in whitespace.
#!perl -lan
$t{$F[1]}{$F[0]}=1if($F[0]ne$F[1]);$t{$F[0]}{a}}for(1..scalar keys%t){for(keys%t){if(!scalar keys%{$t{$_}}){print;delete$t{$_};$d=$_;last}}die if!$d;for(keys%t){delete$t{$_}{$d}}$d="";
|
Try the file upload box so formatting is kept sane.
#!perl -lan
$t{$F[1]}{$F[0]}=1 if($F[0] ne$F[1]);
$t{$F[0]}{a};
}
for(1..scalar keys%t){
for(keys %t){
if(!scalar keys%{$t{$_}}){
print;
delete $t{$_};
$d=$_;
last;
};
};
die if!$d;
for(keys %t){
delete $t{$_}{$d};
};
$d="";
|
My first working solution *uff*
#!perl -lan
$t{$F[1]}{$F[0]}=1 if($F[0] ne$F[1]);;
$t{$F[0]}{""};
}
{
for(1..scalar keys%t){
for$a(keys %t){
# print "Looking at $a: ",scalar keys(%{$t{$a}});
if(!scalar keys%{$t{$a}}){
print $a;
delete $t{$a};
$d=$a;
last;
};
};
die if!$d;
for$a(keys %t){
# print "Del $a,$d";
delete $t{$a}{$d};
};
$d="";
};
|