# Advanced Event 7: Play Ball!
# http://www.microsoft.com/technet/scriptcenter/funzone/games/games08/aevent7.mspx
# Teams - as character array
$teams="ABCDEF".toChararray()
# Create combinations, use $c to start each iteration one step futher in the array
$plays=$teams | % {$c=0} { $team=$_; $c++; $teams[$c..$teams.length] | % { "$team vs. $_" } }
# Randomize - the easy way
# $plays | sort {(new-object random).next()}
# Randomize - and make sure the plan is more realistic by adding two additional
# constraints: Make sure -
# - no team plays more than 3 matches in a row and
# - a team is not left idle for 40% of a tournement
[int] $idle=[math]::truncate($plays.count*0.4)
do{
$AllTeamsOk=$true
# Randomize / shuffle deck
$plays=$plays | sort {(new-object random).next()}
# Use foreach statement as break is used
foreach($team in $teams) {
# Build a list of Y/.'s for each game a use a regex to look for consecutive matches
$teamplays=$plays | % {$g=""} { if ($_ -match $team) { $g+="Y" } else {$g+="."} } {$g}
write-debug "$team $teamplays"
if ($teamplays -match "y{3}") {
write-verbose "bad $team plays 3 matches in a row - reshuffle"
$AllTeamsOk=$false
break
}
elseif ($teamplays -match "\.{$idle}") {
write-verbose "bad $team is too much idle in the tournement - reshuffle"
$AllTeamsOk=$false
break
}
}
}
until ($AllTeamsOk)
$plays
Monday, March 03, 2008
2008 Scripting Games, Solution 7
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment