Thursday, February 28, 2008

2008 Scripting Games - Advanced Event 8

 

Event 8: Making Beautiful Music

 

$TotalMusicTime = New-TimeSpan

$songList = cat C:\Scripts\songlist.csv | foreach {
    $item = $_.split(",")
    $time = $item[2].split(":")
    $song = new-object psobject
    add-member -inp $song noteproperty Artist $item[0]
    add-member -inp $song noteproperty SongName $item[1]
    add-member -inp $song noteproperty Minutes $time[0]
    add-member -inp $song noteproperty Seconds $time[1]
    $song    
}

$cd=@()

$singleArtist = $songList | group Artist | where {$_.count -eq 1}
$groupsArtist = $songList | group Artist | where {$_.count -gt 1}
$singleArtist | foreach {
    $song = new-object psobject
    add-member -inp $song noteproperty Artist $_.Group[0].Artist
    add-member -inp $song noteproperty SongName $_.Group[0].SongName
    add-member -inp $song noteproperty Minutes $_.Group[0].Minutes
    add-member -inp $song noteproperty Seconds $_.Group[0].Seconds
    $cd+=$song
    $TotalMusicTime = $TotalMusicTime.add((New-TimeSpan -Minutes $song.Minutes -Seconds $song.Seconds))
}

$groupsArtist | foreach {
    if(($TotalMusicTime.TotalMinutes -ge 75) -and ($TotalMusicTime.TotalMinutes -le 80)){return}
    $song = new-object psobject
    add-member -inp $song noteproperty Artist $_.Group[0].Artist
    add-member -inp $song noteproperty SongName $_.Group[0].SongName
    add-member -inp $song noteproperty Minutes $_.Group[0].Minutes
    add-member -inp $song noteproperty Seconds $_.Group[0].Seconds
    $cd+=$song
    $TotalMusicTime = $TotalMusicTime.add((New-TimeSpan -Minutes $song.Minutes -Seconds $song.Seconds))
}

write-host
$cd | select Artist,SongName,@{n="Time";e={"{0}:{1}" -f $_.Minutes,$_.Seconds}} | sort artist | ft -a

"Total music time: {0}:{1}" -f $TotalMusicTime.TotalMinutes.ToString("N0"),$TotalMusicTime.Seconds
write-host

 

Artist                      SongName                     Time
------                      --------                     ----
Alice Cooper                I'm Eighteen                 2:58
Badfinger                   No Matter What               2:59
Cracker                     Eurotrash Girl               8:03
Credence Clearwater Revival Have You Ever Seen the Rain? 2:39
Dire Straits                So Far Away                  5:12
Don McLean                  American Pie                 8:35
Donovan                     Catch the Wind               5:02
Jefferson Airplane          White Rabbit                 2:34
John Prine                  New Train                    3:24
Nick Cave and the Bad Seeds Deanna                       3:46
Nirvana                     Heart-Shaped Box             4:41
Paul Simon                  Mother and Child Reunion     2:48
REM                         Losing My Religion           4:28
Robert Palmer               Simply Irresistible          4:12
The Animals                 House of the Rising Sun      4:31
The Bangles                 Manic Monday                 3:06
The Beatles                 Help                         2:18
The Kinks                   Sunny Afternoon              3:36
The Ramones                 I Wanna Be Sedated           2:29


Total music time: 77:21

2008 Scripting Games - Advanced Event 7

 

$teams = "A","B","C","D","E","F"
$rounds=@()

for($i=0; $i -lt $teams.length; $i++){
   0..2 | foreach { $rounds += ("{0} vs. {1}" -f $teams[$_],$teams[-($_+1)]) }
   1..($teams.length-1) | foreach { $teams[1],$teams[$_] = $teams[$_],$teams[1] }
} 

# remove duplicates
$rounds = $rounds | select -unique

# random positions
$rnd = new-object random (get-date).millisecond 
$list = new-object System.Collections.ArrayList
$list.AddRange(0..($rounds.count-1))

1..$rounds.count | foreach {
   $n=$rnd.next($list.count)	
   $rounds[$list[$n]]
   [void]$list.removeAt($n)
}



#result

B vs. E
A vs. C
C vs. D
A vs. E
F vs. D
C vs. F
D vs. B
F vs. B
B vs. C
A vs. D
A vs. B
A vs. F
E vs. F
E vs. C
D vs. E

Sudden Death Challenge: Event 7

 

([math]::Sqrt(([math]::Pow(([math]::Truncate((100*2)/30)),5) *4)) /45).toString("N2")

 

#Result

3.92

Sudden Death Challenge: Event 6

 

$words = (cat c:\scripts\lettercase.txt).split(" ") | foreach {
    if($_ -match '\d+') {
        $_= [int]$_ -1111; $_
    } else {
        $_= $_[0].toString().toUpper()+$_.substring(1).toLower(); $_
    }
}

[string]::join(" ",$words)

 

# result

The 2008 Winter Scripting Games!

Sudden Death Challenge: Event 5

 

# one line

gwmi -list | ? {$_.name -like "WIN32_*"} | % {$_.psbase.properties} | ? {($_.name[0] -cmatch '[A-Y]') -and ($_.origin -like "WIN32_*")} | group {$_.name[0]} | sort name | select @{n="Name";e={$_.group[0].name}},@{n="Class";e={$_.group[0].origin}} | ft -auto


# results
Name                      Class                               
----                      -----                               
ArpAlwaysSourceRoute      Win32_NetworkAdapterConfiguration   
Bias                      Win32_TimeZone                      
ColorPlanes               Win32_DisplayControllerConfiguration
DriveName                 Win32_VolumeChangeEvent             
EventType                 Win32_PowerManagementEvent          
FileName                  Win32_ModuleLoadTrace               
GatewayCostMetric         Win32_NetworkAdapterConfiguration   
HorizontalResolution      Win32_DisplayControllerConfiguration
ImageBase                 Win32_ModuleLoadTrace               
JavaClass                 Win32_ClassicCOMClassSetting        
KeepAliveInterval         Win32_NetworkAdapterConfiguration   
LastDrive                 Win32_BootConfiguration             
MachineName               Win32_ComputerSystemEvent           
NumForwardPackets         Win32_NetworkAdapterConfiguration   
OEMEventCode              Win32_PowerManagementEvent          
ProcessID                 Win32_ModuleLoadTrace               
QuotaNonPagedPoolUsage    Win32_Process                       
RemainingEvaluationPeriod Win32_WindowsProductActivation      
StackBase                 Win32_ThreadStartTrace              
Type                      Win32_ComputerShutdownEvent         
UserStackBase             Win32_ThreadStartTrace              
VerticalResolution        Win32_DisplayControllerConfiguration
WaitMode                  Win32_ThreadStartTrace              
XOffCharacter             Win32_SerialPortConfiguration       
YResolution               Win32_PrinterConfiguration          


Sudden Death Challenge: Event 3

 

$presidents= cat C:\Scripts\presidents.txt | foreach {
    $tmp = $_.split(",")
    $p=New-Object psobject
    Add-Member -inp $p noteproperty firstName $tmp[1].trim()
    Add-Member -inp $p noteproperty lastName  $tmp[0].trim()
    Add-Member -inp $p noteproperty firstLength $p.firstName.length
    Add-Member -inp $p noteproperty vowels @(("{0}{1}" -f $p.firstName,$p.lastName).toCharArray() -match '[aeiou]').count
    $p
}


$longest = $presidents | sort firstLength -desc | select -first 1
"Longest first name: {0} {1}" -f $longest.firstName,$longest.lastName

"Total vowels used: {0}" -f ($presidents | measure-object vowels -sum).sum


$initials = ""
$alphabet = ([string](65..90 | foreach {[char]$_})).replace(" ","")

$presidents  | foreach {
    $initials+="{0}{1}" -f $_.firstName[0],$_.lastName[0]
}

$alphabet = $alphabet -replace "[$initials]"
"The following letters are not used as Presidential initials:"
$alphabet.ToCharArray()

 

 

# results

Longest first name: Rutherford Hayes
Total vowels used: 192
The following letters are not used as Presidential initials:
I
O
Q
S
X
Y

Sudden Death Challenge: Event 2

 
$vertical = @(cat C:\Scripts\vertical.txt) 
$text=""

for($i=0; $i -le 3; $i++){
    0..($vertical.length-1) | foreach { $text+=$vertical[$_][$i] } 
}

$text
 
# result
The 2008 Winter Scripting Games!