My first Perl Golf code.
output isn't what's required. There are no newlines at the end, and the numbers aren't commafied when necessary (3000 should be 3,000). Apart from those things, it's excellent for a first ever golf.
#!perl
@a=qw/o tw(o|en) th(i|r) fo fi si se ei n te el twel/;
for(split/ /,<>){
$i=1;
foreach $d(@a){
if(/^$d/){$x+=$i;}
$i++;
}
if(/tee/){$x+=10;}
if(/ty/){$x*=10;}
if(/^h/){$y=$x*1e2;$x=0;}
if(/^tho/){$n+=($x+$y)*1e3;$x=0;$y=0;}
if(/^m/){$n+=($x+$y)*1e6;$x=0;$y=0;}
if(/^b/){$n+=($x+$y)*1e9;$x=0;$y=0;}
}
print $n+$x+$y;
|
Second try.
#!perl
for(split/ /,<>){
$i=1;
foreach $d(qw/o tw(o|en) th(i|r) fo fi si se ei n te el twel/){
if(/^$d/){$x+=$i;}
$i++;
}
if(/tee/){$x+=10;}
if(/ty/){$x*=10;}
if(/^h/){$y=$x*1e2;$x=0;}
if(/^tho/){$n+=($x+$y)*1e3;$x=$y=0;}
if(/^m/){$n+=($x+$y)*1e6;$x=$y=0;}
if(/^b/){$n+=($x+$y)*1e9;$x=$y=0;}
}
$_=$n+$x+$y;
s/\G(\d{1,3})(?=(?:\d\d\d)+(?!\d))/$1,/g;
print $_,"\n";
|