#!/usr/bin/perl -w

use strict;  # A little anal retention :-)
    # VERSION 1.04

# Change this if your script is at another location.
# Remember that your script when tested by the referees
# will be named interlin.pl
my $script = "interlin.pl";


#----------------------------------------------------------#
#          You should not modify after this line.          #
#----------------------------------------------------------#

# Check solution.
select(STDERR);$|=1; select(STDOUT);$|=1; # auto-flush

print "You shot a round of ", get_golf_score($script), " strokes (the decimal part is the tiebreaker score).\n";

# Make tests.
my @tests = ( 
# void input.
[ "", "" ],

# Empty line
[ "
", "" ],

# Lone letter in sentence.
[ "a. f. This is a long long long long long long long very very very very very very very long sentence. f. Another long long long sentence, really long, I mean longer than the previous. a. And finally a sentence.
a long word sdlfkjsdklfjsldfkjsldfkjsdlfkjsdlfkjsdlfkjsdflksdjflsdkjflsdkjfsldkfjslkfjslkdfjsldkfjsdlkfjsldkfjsldfkjsldkfjsdlfkj. A.
",
"1:\ta.
1/1:\t1/1
1:\tf.
0/1:\t0/1
19:\tThis is  a   long long long long long long long very very...
28/75:\t1/4  1/2 1/1 1/4  1/4  1/4  1/4  1/4  1/4  1/4  2/4  2/4
1:\tf.
0/1:\t0/1
13:\tAnother long long long sentence, really long, I   mean lo...
24/63:\t3/7     1/4  1/4  1/4  3/8       3/6    1/4   1/1 2/4  2/6
1:\ta.
1/1:\t1/1
4:\tAnd finally a   sentence.
8/19:\t1/3 3/7     1/1 3/8
4:\ta   long word sdlfkjsdklfjsldfkjsldfkjsdlfkjsdlfkjsdlfkjs...
3/125:\t1/1 1/4  1/4  0/116
1:\tA.
1/1:\t1/1
" ],

# Empty line before paragraph.
[ "
Empty line before paragraph.
",
"4:\tEmpty line before paragraph.
10/24:\t2/5   2/4  3/6    3/9
" ],

# Empty line after paragraph.
[ "Empty line after paragraph.

",
"4:\tEmpty line after paragraph.
9/23:\t2/5   2/4  2/5   3/9
" ],

# No vowels in this sentence.
[ "sjkdfh sksjdfhsjkd sdfjkshdfkj sdjkfh.
",
"4:\tsjkdfh sksjdfhsjkd sdfjkshdfkj sdjkfh.
0/34:\t0/6    0/11        0/11        0/6
" ],

# Short sentences, punctuation not at end.
[ "You see what happens to 'quoted words'. Or parens (if you're into
\"it\"). Use a lot of short sentences.
",
"7:\tYou see what happens to  'quoted words'.
13/30:\t3/3 2/3 1/4  2/7     1/2 3/6     1/5
6:\tOr  parens (if you're into \"it\").
11/21:\t1/2 2/6    1/2 4/5    2/4  1/2
6:\tUse a   lot of  short sentences.
9/23:\t2/3 1/1 1/3 1/2 1/5   3/9
" ],

# There must not be extra space after the dot printed.
[ "Does your output have an extra space after the dot b.
",
"11:\tDoes your output have an  extra space after the dot b.
19/42:\t2/4  3/4  3/6    2/4  1/2 2/5   2/5   2/5   1/3 1/3 0/1
" ],

# More than one space between words
[ "There can be  more  than
one space between     words.


And more than one line between paragraphs.
",
"9:\tThere can be  more than one space between words.
15/38:\t2/5   1/3 1/2 2/4  1/4  2/3 2/5   3/7     1/5
7:\tAnd more than one line between paragraphs.
14/35:\t1/3 2/4  1/4  2/3 2/4  3/7     3/10
" ],

# Systematic test, showing short last word in sentence:
# Sentence length 58 to 61, word length 2 to 4.
[ "Mind the gap; the sentence has 58 characters now: spli t.
Mind the gap; the sentence has 58 characters now: spl it.
Mind the gap; the sentence has 58 characters now: sp lit.
Mind the gap; the sentence has 59 characters now: sspli t.
Mind the gap; the sentence has 59 characters now: sspl it.
Mind the gap; the sentence has 59 characters now: ssp lit.
Mind the gap; the sentence has 60 characters now: ssspli t.
Mind the gap; the sentence has 60 characters now: ssspl it.
Mind the gap; the sentence has 60 characters now: sssp lit.
Mind the gap; the sentence has 61 characters now: sssspli t.
Mind the gap; the sentence has 61 characters now: sssspl it.
Mind the gap; the sentence has 61 characters now: ssssp lit.
",
"11:\tMind the gap; the sentence has 58  characters now: spli t.
13/44:\t1/4  1/3 1/3  1/3 3/8      1/3 0/2 3/10       1/3  1/4  0/1
11:\tMind the gap; the sentence has 58  characters now: spl it.
13/44:\t1/4  1/3 1/3  1/3 3/8      1/3 0/2 3/10       1/3  0/3 1/2
11:\tMind the gap; the sentence has 58  characters now: sp  lit.
13/44:\t1/4  1/3 1/3  1/3 3/8      1/3 0/2 3/10       1/3  0/2 1/3
11:\tMind the gap; the sentence has 59  characters now: sspli t.
13/45:\t1/4  1/3 1/3  1/3 3/8      1/3 0/2 3/10       1/3  1/5   0/1
11:\tMind the gap; the sentence has 59  characters now: sspl it.
13/45:\t1/4  1/3 1/3  1/3 3/8      1/3 0/2 3/10       1/3  0/4  1/2
11:\tMind the gap; the sentence has 59  characters now: ssp lit.
13/45:\t1/4  1/3 1/3  1/3 3/8      1/3 0/2 3/10       1/3  0/3 1/3
11:\tMind the gap; the sentence has 60  characters now: ssspli t.
13/46:\t1/4  1/3 1/3  1/3 3/8      1/3 0/2 3/10       1/3  1/6    0/1
11:\tMind the gap; the sentence has 60  characters now: ssspl it.
13/46:\t1/4  1/3 1/3  1/3 3/8      1/3 0/2 3/10       1/3  0/5   1/2
11:\tMind the gap; the sentence has 60  characters now: sssp lit.
13/46:\t1/4  1/3 1/3  1/3 3/8      1/3 0/2 3/10       1/3  0/4  1/3
11:\tMind the gap; the sentence has 61  characters now: sssspl...
13/47:\t1/4  1/3 1/3  1/3 3/8      1/3 0/2 3/10       1/3  1/7
11:\tMind the gap; the sentence has 61  characters now: sssspl...
13/47:\t1/4  1/3 1/3  1/3 3/8      1/3 0/2 3/10       1/3  0/6
11:\tMind the gap; the sentence has 61  characters now: ssssp ...
13/47:\t1/4  1/3 1/3  1/3 3/8      1/3 0/2 3/10       1/3  0/5
" ],

# Systematic test, leaving part of last word in sentence:
# Sentence length to 61 to 63, left part -1 to 1. (Length 60 covered above)
[ "Mind the gap; the sentence has 61 characters now: sssspl it.
Mind the gap; the sentence has 61 characters now: ssssp lit.
Mind the gap; the sentence has 61 characters now: ssss plit.
Mind the gap; the sentence has 62 characters now: sssssp lit.
Mind the gap; the sentence has 62 characters now: sssss plit.
Mind the gap; the sentence has 62 characters now: ssss split.
Mind the gap; the sentence has 63 characters now: ssssss plit.
Mind the gap; the sentence has 63 characters now: sssss split.
Mind the gap; the sentence has 63 characters now: ssss ssplit.
",
"11:\tMind the gap; the sentence has 61  characters now: sssspl...
13/47:\t1/4  1/3 1/3  1/3 3/8      1/3 0/2 3/10       1/3  0/6
11:\tMind the gap; the sentence has 61  characters now: ssssp ...
13/47:\t1/4  1/3 1/3  1/3 3/8      1/3 0/2 3/10       1/3  0/5
11:\tMind the gap; the sentence has 61  characters now: ssss p...
13/47:\t1/4  1/3 1/3  1/3 3/8      1/3 0/2 3/10       1/3  0/4  1/4
11:\tMind the gap; the sentence has 62  characters now: sssssp...
13/48:\t1/4  1/3 1/3  1/3 3/8      1/3 0/2 3/10       1/3  0/6
11:\tMind the gap; the sentence has 62  characters now: sssss ...
13/48:\t1/4  1/3 1/3  1/3 3/8      1/3 0/2 3/10       1/3  0/5
11:\tMind the gap; the sentence has 62  characters now: ssss s...
13/48:\t1/4  1/3 1/3  1/3 3/8      1/3 0/2 3/10       1/3  0/4  1/5
11:\tMind the gap; the sentence has 63  characters now: ssssss...
13/49:\t1/4  1/3 1/3  1/3 3/8      1/3 0/2 3/10       1/3  0/6
11:\tMind the gap; the sentence has 63  characters now: sssss ...
13/49:\t1/4  1/3 1/3  1/3 3/8      1/3 0/2 3/10       1/3  0/5
11:\tMind the gap; the sentence has 63  characters now: ssss s...
13/49:\t1/4  1/3 1/3  1/3 3/8      1/3 0/2 3/10       1/3  0/4  1/6
" ],

# Systematic test, truncating internal word in sentence:b
# Word length 1 to 4, left part -1 to 1.
[ "Mind the dots now; 56 characters before the next word: a word.
Mind the dots now; 57 characters before the next word:: a word.
Mind the dots now; 58 characters before the next word::: a word.
Mind the dots now; 56 characters before the next word: no word.
Mind the dots now; 57 characters before the next word:: no word.
Mind the dots now; 58 characters before the next word::: no word.
Mind the dots now; 56 characters before the next word: the word.
Mind the dots now; 57 characters before the next word:: the word.
Mind the dots now; 58 characters before the next word::: the word.
",
"12:\tMind the dots now; 56  characters before the next word: a...
15/48:\t1/4  1/3 1/4  1/3  0/2 3/10       3/6    1/3 1/4  1/4   1/1
12:\tMind the dots now; 57  characters before the next word:: ...
15/48:\t1/4  1/3 1/4  1/3  0/2 3/10       3/6    1/3 1/4  1/4
12:\tMind the dots now; 58  characters before the next word:::...
15/48:\t1/4  1/3 1/4  1/3  0/2 3/10       3/6    1/3 1/4  1/4
12:\tMind the dots now; 56  characters before the next word: n...
15/49:\t1/4  1/3 1/4  1/3  0/2 3/10       3/6    1/3 1/4  1/4   1/2
12:\tMind the dots now; 57  characters before the next word:: ...
15/49:\t1/4  1/3 1/4  1/3  0/2 3/10       3/6    1/3 1/4  1/4
12:\tMind the dots now; 58  characters before the next word:::...
15/49:\t1/4  1/3 1/4  1/3  0/2 3/10       3/6    1/3 1/4  1/4
12:\tMind the dots now; 56  characters before the next word: t...
15/50:\t1/4  1/3 1/4  1/3  0/2 3/10       3/6    1/3 1/4  1/4   1/3
12:\tMind the dots now; 57  characters before the next word:: ...
15/50:\t1/4  1/3 1/4  1/3  0/2 3/10       3/6    1/3 1/4  1/4
12:\tMind the dots now; 58  characters before the next word:::...
15/50:\t1/4  1/3 1/4  1/3  0/2 3/10       3/6    1/3 1/4  1/4
" ],

# Some long words... what happens if there's only one
[ "Antisupercalifragilisticexpialidisestablishmentarianistical.
Antisupercalifragilisticexpialidisestablishmentarianisticity.
Antisupercalifragilisticexpialidisestablishmentarianisticness.
This is filler to get even closer to 60 characters; 
Antisupercalifragilisticexpialidisestablishmentarianistical.
",

"1:\tAntisupercalifragilisticexpialidisestablishmentarianistical.
25/59:\t25/59
1:\tAntisupercalifragilisticexpialidisestablishmentarianistic...
26/60:\t26/60
1:\tAntisupercalifragilisticexpialidisestablishmentarianistic...
25/61:\t25/61
11:\tThis is  filler to  get even closer to  60  characters; A...
39/100:\t1/4  1/2 2/6    1/2 1/3 2/4  2/6    1/2 0/2 3/10        25/59
" ],

# Last: 99/199
[ "Internationalizational-internationalizational-internationalizational-internationalizational-internationalizational-internationalizational-internationalizational-internationalizational-internationalizationals.
",
"1:\tInternationalizational-internationalizational-internation...
99/199:	99/199
"]
);

# Catching STDERR.
my $IN  = "in.tmp";
my $ERR = "err.tmp";

foreach my $test ( @tests ) {
    # Prepare input file.
    open IN, ">$IN" or die $!;
    print IN $test->[0];
    close IN;

    # Prepare command.
    my $cmd = qq("$^X" $script $IN 2>$ERR);
    print "Running '$cmd':\t";
    my $out = `$cmd`;

    # Check STDERR.
    if ( -s $ERR ) {
        print "oops, you wrote to stderr.\n";
        open ERR, "<$ERR" or die $!;
        local $/;               # slurp mode
        my $err = <ERR>;        # dump error output.
        close ERR;
        unlink $IN;
        unlink $ERR;
        warn "STDERR output:\n";
        warn  "--\n".$err."--\n";
        die "Failed.\n";
    }

    # Check STDOUT.
    if ( $out ne $test->[1] ) {
        print "oops, wrong output.\n";
        warn "Expected:\n";
        warn "--\n".$test->[1]."--\n";
        warn "Got:\n";
        warn "--\n".$out."--\n";
        unlink $IN;
        unlink $ERR;
        die "Failed.\n";
    }
    print "done.\n";
}

# All test successful.
print "Hooray, you passed.\n";
print "You shot a round of ", get_golf_score($script), " strokes (the decimal part is the tiebreaker score).\n";
unlink $IN;
unlink $ERR;
print "You can submit your solution at: http://perlgolf.sourceforge.net/cgi-bin/PGAS/leader.cgi?course=4\n";
exit;

#
# Compute golf score.
sub get_golf_score {
    my $script = shift;

    my $code;
    open F, "<$script" or die $!;
    {
        local $/;
        $code = <F>;
    }
    close F;
    
    $code =~ s/\r/\n/g;
    $code =~ s/\n+/\n/g;
    $code =~ s/\n+$//;           # Trim trailing newline(s)
    $code =~ s/^#!\S*perl//;     # Shebang.
    $code =~ s/\n//;             # Free first newline.
    my $score = length $code;

    # Tiebreak
    my $vowels = $code =~ y/aeiouyAEIOUY//;
    my $alphan = $code =~ y/a-zA-Z0-9//;
    my $tie;
    if ( $alphan != 0 ) {
        $tie = $vowels / $alphan;
        $tie = .99 if $tie == 1;
    } else {
        $tie = .99;
    }

    $score += $tie;
    return sprintf "%.2f", $score;
}

__END__
