Resolva a Dívida Global, da maneira do Code Golf


32

Os líderes do mundo se encontraram e finalmente admitiram que a melhor (e única) maneira de resolver os problemas econômicos globais é fazer um balanço do quanto eles devem um ao outro e pagar um ao outro com cheques enormes. Eles o contrataram (ironicamente, com a menor taxa de contrato possível) para descobrir os melhores meios de fazê-lo.

Depois de muita deliberação, e pedindo a alguém para desenhar um exemplo simples, eles apresentaram as seguintes especificações.

Cada país é representado pelo código ISO 3166-1 alfa-2 : USnos EUA, AUna Austrália, JPno Japão, CNna China e assim por diante ...

  1. Um razão é elaborado como uma série de entradas de países e os valores devidos a cada país.
  2. A entrada de cada país começa com seu ID de domínio em dois pontos e quanto eles têm excedente / déficit (em bilhões de euros), seguidos por ponto e vírgula, depois uma lista dos países separados por vírgula e quanto (em bilhões de Euro) eles devem.
  3. Se um país não deve nada a outro país, nenhuma menção a esse país é inserida após esse separador de ponto e vírgula.
  4. Os déficits são indicados como números negativos, o excedente é indicado como um número positivo.
  5. Os valores também podem ser flutuadores.
  6. O razão deve ser retirado do STDIN. O final do razão é indicado por um retorno de carro em uma linha em branco. A contagem deve ser entregue à STDOUT.

Um exemplo de um razão:

Input:
AU:8;US:10,CN:15,JP:3
US:14;AU:12,CN:27,JP:14
CN:12;AU:8,US:17,JP:4
JP:10;AU:6,US:7,CN:10

O sistema então calcula quanto cada país deve e deve e determina seu superávit / déficit, por exemplo, para a UA:

AU = 8 (superávit atual) -10 (para EUA) -15 (para CN) -3 (para JP) +12 (dos EUA) +8 (da CN) +6 (da JP) = 6

Quando toda a computação estiver concluída, uma contagem deve ser mostrada:

Output:
AU:6
US:-5
CN:35
JP:8

Seu trabalho é criar esse sistema, capaz de receber qualquer número de entradas do razão para qualquer número de países e capaz de determinar quanto cada país tem déficit / superávit quando tudo é pago.

O teste final é para você usar seu código para resolver a dívida entre os seguintes países no caso de teste abaixo. Esses números foram retirados da BBC News em junho de 2011. ( http://www.bbc.com/news/business-15748696 )

Para os fins do exercício, usei o respectivo PIB como excedente atual ... Lembre-se de que este é estritamente um exercício de garantia de qualidade do código ... não se fala em resolução econômica global aqui nesta pergunta ... Se você quer falar sobre economia, tenho certeza de que existe outro subdomínio no SE que lida com isso ...

US:10800;FR:440.2,ES:170.5,JP:835.2,DE:414.5,UK:834.5
FR:1800;IT:37.6,JP:79.8,DE:123.5,UK:227,US:202.1
ES:700;PT:19.7,IT:22.3,JP:20,DE:131.7,UK:74.9,US:49.6,FR:112
PT:200;IT:2.9,DE:26.6,UK:18.9,US:3.9,FR:19.1,ES:65.7
IT:1200;JP:32.8,DE:120,UK:54.7,US:34.8,FR:309,ES:29.5
IE:200;JP:15.4,DE:82,UK:104.5,US:39.8,FR:23.8
GR:200;DE:15.9,UK:9.4,US:6.2,FR:41.4,PT:7.5,IT:2.8
JP:4100;DE:42.5,UK:101.8,US:244.8,FR:107.7
DE:2400;UK:141.1,US:174.4,FR:205.8,IT:202.7,JP:108.3
UK:1700;US:578.6,FR:209.9,ES:316.6,IE:113.5,JP:122.7,DE:379.3

Agora, seja o salvador econômico do mundo!

Regras:

  1. O menor código vence ... afinal de contas, este é o código-golfe ...
  2. Forneça sua saída do principal caso de teste com sua resposta de código ...

1
In the "ultimate test", shouldn't there be a semicolon after JP:4100?
Mathieu Rodic

9
I can't help but wonder if this is a very clever way of having a homework assignment completed for you. If so, you deserve it.
mkingston

2
Yeah, if you did this will real numbers you'd notice an astonishing contradiction. The sum of all surpluses and deficits will be negative.
Cruncher

3
Actually, it's not homework in disguise... It was inspired from my weekly poker tournament with my friends... Trying to figure out a quicker way of working out the winnings for each player ;)
WallyWest

1
@WallyWest LOL ;) btw, apologies for the OT comments, but it's a matter I'm quite passionate about. Now let's get back to some fun coding and forget the woes of the world...
Tobia

Respostas:


11

K, 66

{(((!)."SF"$+":"\:'*+a)-+/'d)+/d:"F"$(!).'"S:,"0:/:last'a:";"\:'x}

.

k)input:0:`:ledg.txt
k){(((!)."SF"$+":"\:'*+a)-+/'d)+/d:"F"$(!).'"S:,"0:/:last'a:";"\:'x} input
US| 9439.3
FR| 2598.9
ES| 852.1
PT| 90.1
IT| 887.5
IE| 48
GR| 116.8
JP| 4817.4
DE| 2903.7
UK| 1546.2

Incredibly impressed with this one... any chance you can provide a link to the K programming paradigm?
WallyWest

@WallyWest code.kx.com provides lots of information on q, which is the syntactic sugar that sits on top of k. q, moreso thank k, is what you'll find in production systems but for golfing k has the edge. Also check out Kona (github.com/kevinlawler/kona) which is an open source implementation of an older version of k
tmartin

10

Perl, 139 137 134 119 112

Here's another working piece of code... I will document it later.

Golfed code

With dictionary (112):

for(<>){~/:(.+);/g;$d{$c=$`}+=$1;$l=$';$d{$1}+=$2,$d{$c}-=$2while$l=~/(..):([^,]+)/g}print"$_:$d{$_}
"for keys%d

Without dictionary (137):

for($T=$t.=$_ for<>;$t=~/(..:)(.+);(.+)/g;print"$c$s\n"){$c=$1;$l=$3;$s=$2;$s-=$&while$l=~/[\d.]+/g;$s+=$1while$T=~/$c([\d.]+)(?!;|\d)/g}

Output

US:9439.3
FR:2598.9
ES:852.1
PT:90.1
IT:887.5
IE:48
GR:116.8
JP:4817.4
DE:2903.7
UK:1546.2

See it in action!

http://ideone.com/4iwyEP


4
The definition of "short" should be judged by amount of tokens, not characters. Readability 4 life!
Domi

10
@Domi - you're new here, aren't you ;-)
jimbobmcgee

4
@jimbobmcgee: I also have a feeling this website is not much about readability...
Mathieu Rodic

4

Python, 211 185 183

import sys,re;t,R,F=sys.stdin.read(),re.findall,float;S=lambda e,s:sum(map(F,R(e,s)))
for m in R('(..:)(.+);(.+)',t):print m[0]+`F(m[1])+S(m[0]+'([\d.]+)(?!;|\d)',t)-S('[\d.]+',m[2])`

Output with major test case:

US:9439.300000000001
FR:2598.9
ES:852.0999999999999
PT:90.09999999999997
IT:887.5
IE:48.0
GR:116.8
JP:4817.4
DE:2903.7
UK:1546.2000000000003

(test it here: http://ideone.com/CjWG7v)


4

C - 257 253 if no CR at end of line

Depends on sizeof(short)==2.

No check for buffer overflow.

#define C(c) x[*(short*)c]
main(i){double x[23131]={0},d;char*q,b[99],*(*s)()=strtok;for(;gets(b);)for(s(b,":"),C(b)+=atof(s(0,";"));q=s(0,":");C(b)-=d=(atof(s(0,","))),C(q)+=d);for(i=b[2]=0;i<23131;memcpy(b,&i,2),x[i]?printf("%s:%f\n",b,x[i++]):++i);}

Output:

DE:2903.700000  
IE:48.000000    
UK:1546.200000  
JP:4817.400000  
FR:2598.900000  
GR:116.800000   
ES:852.100000   
US:9439.300000  
IT:887.500000   
PT:90.100000   

Less golfed:

#define C(c) x[*(short*)c]

main(i)
{
    double x[23131]={0}, d;
    char *q, b[99], *(*s)()=strtok;
    for(;gets(b);) 
        for(s(b, ":"),C(b)+=atof(s(0, ";")); 
            q=s(0, ":"); 
            C(b)-=d=(atof(s(0, ","))), C(q)+=d) ;

    for(i=b[2]=0; 
        i<23131; 
        memcpy(b, &i, 2), x[i]?printf("%s:%f\n", b, x[i++]):++i) ;
}

3

PHP - 338, 280

Should work with any version of PHP 5.

Golfed:

while(preg_match("#(..):(.+);(.*)#",fgets(STDIN),$m)){$l[$m[1]][0]=(float)$m[2];foreach(explode(",",$m[3])as$x){$_=explode(":",$x);$l[$m[1]][1][$_[0]]=(float)$_[1];}}foreach($l as$c=>$d)foreach($d[1]as$_=>$o){$l[$_][0]+=$o;$l[$c][0]-=$o;}foreach($l as$c=>$d)echo$c,":",$d[0],"\n";

Un-golfed:

<?php

while( preg_match( "#(..):(\d+);(.*)#", fgets( STDIN ), $m ) )
{
    $l[$m[1]][0] = (float)$m[2];

    foreach( explode( ",", $m[3] ) as $x )
    {
        $_ = explode( ":", $x );
        $l[$m[1]][1][$_[0]] = (float)$_[1];
    }
}

foreach( $l as $c => $d )
    foreach( $d[1] as $_ => $o )
    {
        $l[$_][0] += $o;
        $l[$c][0] -= $o;
    }

foreach( $l as $c => $d )
    echo $c, ":", $d[0], "\n";

Output:

US:9439.3
FR:2598.9
ES:852.1
PT:90.1
IT:887.5
IE:48
GR:116.8
JP:4817.4
DE:2903.7
UK:1546.2

Wouldn't it be shorter if you used preg_match_all() and then just looped once?
Damir Kasipovic

3

perl (184 characters)

Code

%c,%d,%e=();while(<>){$_=~/(..):(.+);(.*)/;$n=$1;$c{$1}=$2;for $i(split /,/,$3){$i=~/(..):(.+)/;$d{$1}+=$2;$e{$n}+=$2;}}for $i(keys %c){$c{$i}+=$d{$i}-$e{$i};print $i.":".$c{$i}."\n";}

Output

UK:1546.2
DE:2903.7
IT:887.5
FR:2598.9
PT:90.1
US:9439.3
JP:4817.4
ES:852.1
IE:48
GR:116.8

3

Perl - 116 114 112

for(<>){($n,$m,@l)=split/[:;,]/;$h{$n}+=$m;$h{$n}-=$p,$h{$o}+=$p while($o,$p,@l)=@l}print"$_:$h{$_}\n"for keys%h

Output:

GR:116.8
UK:1546.2
DE:2903.7
IE:48
IT:887.5
US:9439.3
PT:90.1
ES:852.1
FR:2598.9
JP:4817.4

Ungolfed:

for(<>) {
    ($n, $m, @l)=split(/[:;,]/);
    $h{$n}+=$m;

    $h{$n}-=$p, $h{$o}+=$p while ($o,$p,@l)=@l
}
print "$_:$h{$_}\n" for keys%h

Nice! I like your approach :)
Mathieu Rodic

3

C++ - 1254

#include<iostream>
#include<cstring>
#include<vector>
#include<sstream>
#include<cstdlib>
using namespace std;int main(){vector<string>input,countries,output;vector<double>results;string last_val;int j,k,i=0;cout<<"Input\n";do{getline(cin,last_val);if(last_val!=""){input.push_back(last_val);countries.push_back(last_val.substr(0,2));}}while(last_val!="");for(j=0;j<countries.size();j++){results.push_back(0);for(k=0;k<input.size();k++)input[k].substr(0, 2)==countries[j]?results[j]+=atof(input[k].substr((input[k].find(countries[j])+3),(input[k].find(',',input[k].find(countries[j]))-input[k].find(countries[j]))).c_str()):results[j]+=atof(input[k].substr((input[k].find(countries[j],3)+3),(input[k].find(',',input[k].find(countries[j]))-input[k].find(countries[j]))).c_str());}for(j=0;j<input.size();j++){for(k=0;k<countries.size();k++){if(input[j].substr(0,2)!=countries[k]){results[j]-=atof(input[j].substr((input[j].find(countries[k])+ 3),(input[j].find(',',input[k].find(countries[k]))-input[j].find(countries[j]))).c_str());}}}for(i=0;i<countries.size();i++){stringstream strstream;strstream<<countries[i]<<":"<<results[i];output.push_back(strstream.str().c_str());}cout<<"Output:\n";for(i=0;i<output.size();i++){cout<<output[i]<<'\n';}return 0;}

I realize the code is very long, but enjoyed the good fun. This is my first time code golfing, and I am new to C++, so suggestions for improving my code are much appreciated.

Final Challenge Results

Output:
US:9439.3
FR:2598.9
ES:852.1
PT:90.1
IT:887.5
IE:48
GR:116.8
JP:4817.4
DE:2903.7
UK:1546.2

Ungolfed Code

#include<iostream>
#include<cstring>
#include<vector>
#include<sstream>
#include<cstdlib>

using namespace std;

int main() {
  vector<string> input, countries, output;
  vector<double> results;
  string last_val;
  int i, j, k;

  cout << "Input\n";
  do {
    getline(cin, last_val);
    if(last_val != "") {
      input.push_back(last_val);
      countries.push_back(last_val.substr(0, 2));
    }
  } while(last_val != "");

  for(j = 0; j < countries.size(); j++) {
    results.push_back(0);
    for(k = 0; k < input.size(); k++) {
      if(input[k].substr(0, 2) == countries[j]) {
        results[j] += atof(input[k].substr((input[k].find(countries[j]) + 3),
                             (input[k].find(',', input[k].find(countries[j])) -
                              input[k].find(countries[j]))).c_str());
      } else {
        results[j] += atof(input[k].substr((input[k].find(countries[j], 3) + 3),
                             (input[k].find(',', input[k].find(countries[j])) -
                              input[k].find(countries[j]))).c_str());
      }
    }
  }

  for(j = 0; j < input.size(); j++) {
    for(k = 0; k < countries.size(); k++) {
      if(input[j].substr(0, 2) != countries[k]) {
        results[j] -= atof(input[j].substr((input[j].find(countries[k]) + 3),
                             (input[j].find(',', input[k].find(countries[k])) -
                              input[j].find(countries[j]))).c_str());
      }
    }
  }

  for(i = 0; i < countries.size(); i++) {
    stringstream strstream;
    strstream << countries[i] << ":" << results[i];
    output.push_back(strstream.str().c_str());
  }

  cout << "Output:\n";
  for(i = 0; i < output.size(); i++) {
    cout << output[i] << '\n';
  }

  return 0;
}

2
Hi, Nice to see an example in C++. You could reduce number of characters by using one-letter identifiers in stead of descriptive names, i.e. use i for input, c for countries and so on.
ahy1

Agreed with @ahy1 here... If you reduce your variables to 1 letter you can cut a fair bit of this... You may also find this of interest for future golf challenges: codegolf.stackexchange.com/questions/132/tips-for-golfing-in-c
WallyWest

Oh, and you won't need cout << "Output:\n"; either... That's a 20 byte saving there...
WallyWest

3

AWK - 138 120

{l=split($0,h,"[:,;]");t[h[1]]+=h[2];for(i=3;i<l;i+=2){t[h[1]]-=h[i+1];t[h[i]]+=h[i+1]}}END{for(v in t){print v":"t[v]}}

And the results

$ cat data.withoutInputHeadline |awk -f codegolf.awk
IT:887.5
UK:1546.2
DE:2903.7
PT:90.1
ES:852.1
FR:2598.9
GR:116.8
Input:0
JP:4817.4
IE:48
US:9439.3

Ungolfed

{
    l=split($0,h,"[:,;]");
    t[h[1]]+=h[2];
    for(i=3;i<l;i+=2){
        t[h[1]]-=h[i+1]
        t[h[i]]+=h[i+1]
    }
}
END{
    for(v in t){
        print v":"t[v]
    }
}

(test it here: http://ideone.com/pxqc07)


Why even put those headers in? You'll save more bytes without them... they weren't even part of the spec I set... ;)
WallyWest

@WallyWest: Ok, so I didn't understood that, because they are displayed in your first example of Input and Output e.g.: (..)a tally must be shown: Output: (..) No worry about that, I remove my first example right now.
Doomsday

2

Ruby - 225

First try in a challenge like this, sure it could be a lot better...

R=Hash.new(0)
def pd(s,o=nil);s.split(':').tap{|c,a|R[c]+=a.to_f;o&&R[o]-=a.to_f};end
STDIN.read.split("\n").each{|l|c,d=l.split(';');pd(c);d.split(',').each{|s|pd(s,c.split(':')[0])}}
puts R.map{|k,v|"#{k}: #{v}"}.join("\n")

And the results

$ cat data|ruby codegolf.rb
US: 9439.299999999997
FR: 2598.8999999999996
ES: 852.1
JP: 4817.4
DE: 2903.7
UK: 1546.2000000000003
IT: 887.5
PT: 90.09999999999998
IE: 48.0
GR: 116.8

2

JS, 254 240 245

z='replace';r={};p=eval(('[{'+prompt()+'}]')[z](/\n/g,'},{')[z](/;/g,','));for(i in p){l=p[i];c=0;for(k in l){if(!c){c=k;r[c]=0;}else{r[c]-=l[k];}};for(j in p){w=p[j][c];if(w!=null)r[c]+=w}};alert(JSON.stringify(r)[z](/"|{|}/g,'')[z](/,/g,'\n'))

Well..I know it is quite long but this is my second code golf.

Suggestions are welcome!

BTW, Interesting Javascript preserves the order of elements in hashmaps, so, even if p contains an array of dictionaries, I can iterate each dictionary as an array and I'm sure that the first element of a dict is the first inserted. (the name of the country referred to the current line)

Ungolfed:

z='replace';
r={};
p=eval(('[{'+prompt()+'}]')[z](/\n/g,'},{')[z](/;/g,',')); // make the string JSONable and then evaluate it in a structure
for(i in p){ 
    l=p[i];
    c=0;
    for(k in l){
            if(!c){ // if c is not still defined, this is the country we are parsing.
                    c=k;
                    r[c]=0;
            }
            else r[c]-=l[k];
    }; 
    for(j in p){
            w=p[j][c];
            if(!w)  r[c]+=w
    }
};
alert(JSON.stringify(r)[z](/"|{|}/g,'')[z](/,/g,'\n')) # Stringify the structure, makes it new-line separated.

Note: the input is a prompt() which should be a single line. But if you copy/paste a multi line text (like the proposed input) in a prompt() window then JS read it all.

Output:

US:9439.3
FR:2598.9
ES:852.1
PT:90.09999999999998
IT:887.5
IE:48
GR:116.8
JP:4817.4
DE:2903.7000000000003
UK:1546.2

1
You use the word "replace" four times in your code. How about shortening it like this: z='replace';r={};p=eval(('[{'+prompt()+'}]')[z](/\n/g,'},{')[z](/;/g,','));for(i in p){l=p[i];c=0;for(k in l){if(!c){c=k;r[c]=0;}else{r[c]-=l[k];}};for(j in p){w=p[j][c];if(w!=null)r[c]+=w}};alert(JSON.stringify(r)[z](/"|{|}/g,'')[z](/,/g,'\n'))?
user2428118

Woah this saved me 7*4-(3*4+11) characters! (I also put (w!=null) as (!w)
Antonio Ragagnin

@AntonioRagagnin Could you please show your output?
WallyWest

Thanks for your message @WallyWest. Turns out !w was not a good idea to check w!=null and the script wasn't working anymore :p. Now I'll update it with the results
Antonio Ragagnin

Try using: z="replace";r={};p=eval(("[{"+prompt()+"}]")[z](/\n/g,"},{")[z](/;/g,","));for(i in p){l=p[i];c=0;for(k in l)c?r[c]-=l[k]:(c=k,r[c]=0);for(j in p)w=p[j][c],null!=w&&(r[c]+=w)}alert(JSON.stringify(r)[z](/"|{|}/g,"")[z](/,/g,"\n")) for 229 bytes... What I've done here is reduced the if(!c) sequence to a single ternary operator, and I've also incorporated it in its parent for loop... I've also done something similar with the other for loop... comma operators can work wonderfully to join multiple statements within a loop...
WallyWest

2

JavaScript(ES6) 175,166, 161, 156, 153147

Golfed

R={};prompt().split(/\s/).map(l=>{a=l.split(/[;,:]/);c=b=a[0];a.map(v=>b=!+v?v:(R[b]=(R[b]||0)+ +v c==b?b:R[c]-=+v))});for(x in R)alert(x+':'+R[x])

Ungolfed

R = {};
prompt().split(/\s/).map(l => {
    a = l.split(/[;,:]/);       // Split them all!! 
                                // Now in a we have big array with Country/Value items
    c = b = a[0];               // c - is first country, b - current country
    a.map(v =>                
         b = !+v ? v                 // If v is country (not a number), simply update b to it's value          
                 : (R[b] = (R[b] ||0) + +v   // Safely Add value to current country
                   c == b ? c : R[c] -= +v)  // If current country is not first one, remove debth 
    )
});
for (x in R) alert(x + ':' + R[x])

Output

US:9439.299999999997
FR:2598.8999999999996
ES:852.1
JP:4817.4
DE:2903.7
UK:1546.2000000000003
IT:887.5
PT:90.09999999999998
IE:48
GR:116.8

Not sure that ungolfed variant will be working correctly because in golfed variant i'm using one-line operators
tt.Kilew

can you please show your output?
WallyWest

1
Replaced R[b] ? R[b] += +v : R[b] = +v to R[b]=R[b]||0+ +v
tt.Kilew

1
Removed index i=0;i++%2==0?b=v to b=isNaN(+v)?v:
tt.Kilew

1
isNaN(+v) -> !+v
tt.Kilew

1

Groovy 315

def f(i){t=[:];i.eachLine(){l=it.split(/;|,/);s=l[0].split(/:/);if(!z(s[0]))t.put(s[0],0);t.put(s[0],x(z(s[0]))+x(s[1]));(1..<l.size()).each(){n=l[it].split(/:/);t.put(s[0],x(z(s[0]))-x(n[1]));if(!z(n[0]))t.put(n[0],0);t.put(n[0],x(z(n[0]))+x(n[1]))}};t.each(){println it}};def x(j){j.toDouble()};def z(j){t.get(j)}

Output:
US=9439.299999999997
FR=2598.8999999999996
ES=852.1
JP=4817.4
DE=2903.7
UK=1546.2000000000003
IT=887.5
PT=90.09999999999998
IE=48.0
GR=116.8

Ungolfed:

input = """US:10800;FR:440.2,ES:170.5,JP:835.2,DE:414.5,UK:834.5
FR:1800;IT:37.6,JP:79.8,DE:123.5,UK:227,US:202.1
ES:700;PT:19.7,IT:22.3,JP:20,DE:131.7,UK:74.9,US:49.6,FR:112
PT:200;IT:2.9,DE:26.6,UK:18.9,US:3.9,FR:19.1,ES:65.7
IT:1200;JP:32.8,DE:120,UK:54.7,US:34.8,FR:309,ES:29.5
IE:200;JP:15.4,DE:82,UK:104.5,US:39.8,FR:23.8
GR:200;DE:15.9,UK:9.4,US:6.2,FR:41.4,PT:7.5,IT:2.8
JP:4100;DE:42.5,UK:101.8,US:244.8,FR:107.7
DE:2400;UK:141.1,US:174.4,FR:205.8,IT:202.7,JP:108.3
UK:1700;US:578.6,FR:209.9,ES:316.6,IE:113.5,JP:122.7,DE:379.3"""

ungolfed(input)

def ungolfed(i){
    def tallyMap = [:]
    i.eachLine(){ 
        def lineList = it.split(/;|,/)
        def target = lineList[0].split(/:/)

        if(!tallyMap.get(target[0])){tallyMap.put(target[0],0)}
        tallyMap.put(target[0],tallyMap.get(target[0]).toDouble() + target[1].toDouble())
        (1..lineList.size()-1).each(){ e ->
            def nextTarget = lineList[e].split(/:/)
            //subtract the debt
            tallyMap.put(target[0], (tallyMap.get(target[0]).toDouble() - nextTarget[1].toDouble()))
            //add the debt
            if(!tallyMap.get(nextTarget[0])){ tallyMap.put(nextTarget[0], 0) }
            tallyMap.put(nextTarget[0], (tallyMap.get(nextTarget[0]).toDouble() + nextTarget[1].toDouble()))  
        }
    }
    tallyMap.each(){
        println it
    }
}

Do you happen to have a link to where I can find more info on Groovy?
WallyWest

@WallyWest: I have this book, and learned tons from it. I feel that this is one of those languages that it is good to have a reference on the shelf. link, Also tons of info here: link
md_rasler

1

PHP, 333

$a='';while(($l=trim(fgets(STDIN)))!='')$a.=$l.'\n';$a=rtrim($a,'\n');$p=explode('\n',$a);foreach($p as $q){preg_match('/^([A-Z]+)/',$q,$b);preg_match_all('/'.$b[0].':(\d+(?:\.\d+)?)/',$a,$c);$e=ltrim(strstr($q,';'),';');preg_match_all('/([A-Z]+)\:(\d+(?:\.\d+)?)/',$e,$d);echo $b[0].':'.(array_sum($c[1])-array_sum($d[2])).PHP_EOL;}

Ungolfed version :

$a='';
while(($l=trim(fgets(STDIN)))!='')
    $a .= $l.'\n';
$a = rtrim($a,'\n');
$p = explode('\n',$a);
foreach($p as $q){
    preg_match('/^([A-Z]+)/',$q,$b);
    preg_match_all('/'.$b[0].':(\d+(?:\.\d+)?)/',$a,$c);
    $e = ltrim(strstr($q,';'),';');
    preg_match_all('/([A-Z]+)\:(\d+(?:\.\d+)?)/', $e, $d);
    echo $b[0].':'.(array_sum($c[1])-array_sum($d[2])).PHP_EOL;
}
Ao utilizar nosso site, você reconhece que leu e compreendeu nossa Política de Cookies e nossa Política de Privacidade.
Licensed under cc by-sa 3.0 with attribution required.