Wednesday, February 20, 2008

2008 Scripting Games, Solution 2

# Advanced Event 2: Skating on Thin Ice
# http://www.microsoft.com/technet/scriptcenter/funzone/games/games08/aevent2.mspx

# Construct a hash table for skater,score combinations
$skaters=@{}
type c:\scripts\skaters.txt | % {
# Split line into name and an array of results
$name,$results=$_.split(",")
# Calculate the sum, max and min
$measure=$results | measure-object -sum -max -min
# Get the result that counts by excluding max and min scores
$result=$measure.sum - $measure.maximum - $measure.minimum
# Calc the average score
$score=$result / ($results.count-2)
# Save in assoc array
$skaters.$name=$score
}
# Get the 3 highest values, GetEnumerator must be used to get name-value properties
$skaters.GetEnumerator() | sort -desc value | Select -first 3 | % {
# Metal values
$metal="Gold","Silver","Bronze"
# Index into metal
$c=-1
} {
# Increament metal index
$c++
# Write-out winner
"{0} medal: {1}, {2}" -f $metal[$c],$_.name,$_.value
}

No comments: