SHARE
TWEET

TransitiveMoV

a guest Jul 15th, 2019 113 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4.  
  5. my $file = $ARGV[0] or die "No file specified";
  6. my %scores = ();
  7. my %power = ();
  8.  
  9. sub read_csv($) {
  10.     my $file = shift;
  11.     open(DATA, '<', $file) or die "could not open $file";
  12.     my $header = <DATA>;
  13.     chomp $header;
  14.     my ($homeTeam, $homeScore, $awayTeam, $awayScore) = (0, 0, 0, 0);
  15.     my @fields = split(/,\s*/, $header);
  16.     for my $i (0 .. $#fields) {
  17.         if ($fields[$i] =~ /Home team/i) {
  18.             $homeTeam = $i;
  19.         }
  20.         if ($fields[$i] =~ /Vis team/i) {
  21.             $awayTeam = $i;
  22.         }
  23.         if ($fields[$i] =~ /score/i) {
  24.             ($homeTeam > $awayTeam) ? $homeScore = $i : $awayScore = $i;
  25.         }
  26.     }
  27.     my $i = 0;
  28.     while (my $line = <DATA>){
  29.         chomp $line;
  30.         @fields = split(/,\s*/, $line);
  31.         $scores{$i}{'t1'} = $fields[$homeTeam];
  32.         $scores{$i}{'t1s'} = $fields[$homeScore];
  33.         $scores{$i}{'t2'} = $fields[$awayTeam];
  34.         $scores{$i}{'t2s'} = $fields[$awayScore];
  35.         $i++;
  36.     }
  37. }
  38.  
  39. sub calculate_deltas($$$$){
  40.     my ($t1Power, $t2Power, $scoreDiff, $velocity) = @_;
  41.     my $t1Target = $t2Power + $scoreDiff;
  42.     my $t2Target = $t1Power - $scoreDiff;
  43.     return ((($t1Target-$t1Power)*$velocity),(($t2Target-$t2Power)*$velocity));
  44. }
  45.  
  46. sub init_power() {
  47.     foreach my $i (keys %scores) {
  48.         $power{$scores{$i}{'t1'}} = 100;
  49.         $power{$scores{$i}{'t2'}} = 100;
  50.     }
  51. }
  52.  
  53. sub run_round($) {
  54.     my ($velocity) = @_;
  55.     my %new_power = %power;
  56.     foreach my $i (keys %scores) {
  57.         my $t1p = $power{$scores{$i}{'t1'}};
  58.         my $t2p = $power{$scores{$i}{'t2'}};
  59.         my $diff = $scores{$i}{'t1s'} - $scores{$i}{'t2s'};
  60.         my ($d1, $d2) = calculate_deltas($t1p, $t2p, $diff, $velocity);
  61.         $new_power{$scores{$i}{'t1'}} += $d1;
  62.         $new_power{$scores{$i}{'t2'}} += $d2;
  63.     }
  64.     %power = %new_power;
  65. }
  66.  
  67. sub print_sorted_power(%){
  68.     my %pwr = @_;
  69.     my @sorted = sort {$pwr{$a} <=> $pwr{$b}} (keys %pwr);
  70.     foreach my $team (@sorted) {
  71.         print "$team: $pwr{$team}\n";
  72.     }
  73. }
  74.  
  75. read_csv($file);
  76. init_power();
  77. my $roundMax = 10000;
  78. for (my $r = 0; $r < $roundMax; $r++) {
  79.     my $velocity = .05 - .05*($r/$roundMax);
  80.     run_round($velocity);
  81. }
  82. print_sorted_power(%power);
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top