Alphanumeric balance
Perl, 32 + 32 = 64
The string is expected in STDIN. The output is written to STDOUT. White space is ignored. My interpretation of the task is that the program should be able to run on itself to get the score.
$/ = $,;
$_ = <>;
s x\sxxg;
$\ = length;
print s x[0-9a-z]xxgi,
' + ',
s x.xxg,
' = '
Ungolfed with comments
$/ = $,; # The input separator becomes undefined, because the default for $, is "undef"
$_ = <>; # now $_ takes the whole file (STDIN) instead of the first line
s x\sxxg; # $_ =~ s/\s//g;
# white space is removed from $_
$\ = length; # The number of the other characters are put into $\,
# which is automatically printed the end of "print".
print s x[0-9a-z]xxgi, # s/[0-9a-z]//gi
# Remove alphanumeric characters and return their count
' + ',
s x.xxg, # s/.//g
# Remove the remaining special characters and return their count.
# "." does not catch new lines, but we have already
# removed white spaces including new lines.
' = '
I found several variations with the same byte counts, e.g.:
$/ = $x;
$_ = <>, s x\sxxg;
$\ = split $x;
print s x[\da-z]xxgi,
" + ",
s x.xxg,
' = '
Examples
Example from the question:
echo 'http://stackexchange.com' | perl a.pl 20 + 4 = 24
Running on itself (
a.pl
):cat a.pl | perl a.pl 32 + 32 = 64
The file size is 104 bytes, thus 40 bytes are ignored as white space.
Perl, 29 + 29 = 58
$_=<>;s x\sxxg;$\=length;print s x[0-9a-z]xxgi,' + ',s/.//g,' = '
The string is expected at STDIN and it is limited to the first line. The result is printed to STDOUT. White space is ignored.
Ungolfed
$_ = <>;
s x\sxxg; # same as s/\s//gx; removes white space;
$\ = length($_); # sum is automatically appended at the end of print
print sx[0-9a-z]xxgi, # same as s/[0-9a-z]//gi;
# the number of alphanumeric characters
' + ',
s/.//g, # the number of the remaining special characters
' = '
Examples
File a.pl
contains the Perl script.
Example from the question:
echo 'http://stackexchange.com' | perl a.pl 20 + 4 = 24
Running on itself:
cat a.pl | perl a.pl 29 + 29 = 58
The file size of
a.pl
is 65 bytes, thus 7 bytes are ignored as white space.
C - 96 (48+48) characters
It's somewhat readable. There's room for improvement, though.
i,j;main(_){while((_=getchar())>=0)isspace(_)||(isalnum(_)?i++:j++);printf("%i + %i = %i",i,j
,i+j);}
Bash + coreutils, 72 (36 + 36) non-whitespace chars
a=`tr -dc [:alnum:]<<<$1|wc -c`
n=`tr -dt [:space:]<<<$1|wc -c`
echo $a + $[n-a] = $n
Output:
$ ./alnumbalance.sh http://stackexchange.com 20 + 4 = 24 $ ./alnumbalance.sh "$(cat alnumbalance.sh)" 36 + 36 = 72 $
Previous answer:
Pure Bash, 92 (46 + 46) non-whitespace chars
nosp=${1//[[:space:]]}
noaln=${nosp//[[:alnum:]]}
echo $[${#nosp}-${#noaln}] + ${#noaln} = ${#nosp}
Output:
$ ./alnumbalance.sh http://stackexchange.com 20 + 4 = 24 $ ./alnumbalance.sh "$(cat alnumbalance.sh)" 46 + 46 = 92 $