Monday, February 25, 2008

2008 Scripting Games - Advanced Event 3

 

Event 3: Instant (Runoff) Winner

$ballots = cat C:\Scripts\votes.txt | foreach {
  $tmp = $_.split(",")
  $ballot = New-Object psobject
  add-member -inp $ballot noteproperty candidat $tmp
  $ballot
}


$totalBallots = $ballots.length

for($i=1; $i -lt 4; $i++){
  $round = $ballots | group {$_.candidat[0]} | sort count -desc | select Name, @{n="Percent";e={"{0:N2}" -f (($_.count/$totalBallots)*100)}}
  
  $first = $round[0]
  if($first.Percent -ge 50){
    "The winner is {0} with {1}% of the vote." -f $first.Name,$first.Percent
    return
  }
  
  $last = $round[-1].Name
  $ballots | foreach {$_.candidat = $_.candidat | where {$_ -ne $last}}
}

The winner is Pilar Ackerman with 50.17% of the vote.

No comments: