Add to Technorati Favorites
Welcome to ThePowerShellGuy.com Sign in | Join | Help

Scripting Games 2008 Advanced Windows PowerShell Event 8

And for event 8, I cheated a bit here ,

I started out with sorting the files descending, thinking to fix big song first and then look for small ones to fit in, this already gave me more as 75 minutes, but then when testing I sorted them normally ( Small to Big ):

PoSH> .\Event8.ps1                                                                                                      
                                                                                                                        
Artist                                  Title                                   Duration                                
------                                  -----                                   --------                                
Alice Cooper                            I'm Eighteen                            02:58                                   
Alice Cooper                            Be My Lover                             03:22                                   
Badfinger                               No Matter What                          02:59                                   
Badfinger                               Come and Get It                         02:22                                   
Credence Clearwater Revival             Have You Ever Seen the Rain?            02:39                                   
Credence Clearwater Revival             Bad Moon Rising                         02:20                                   
Donovan                                 Lalena                                  02:55                                   
George Harrison                         If Not For You                          03:33                                   
Jefferson Airplane                      White Rabbit                            02:34                                   
John Prine                              New Train                               03:24                                   
John Prine                              Same Thing Happened to Me               03:18                                   
Nick Cave and the Bad Seeds             Henry Lee                               03:56                                   
Nick Cave and the Bad Seeds             Deanna                                  03:46                                   
Nirvana                                 All Apologies                           03:50                                   
Paul Simon                              Mother and Child Reunion                02:48                                   
Paul Simon                              Me and Julio                            02:43                                   
Robert Palmer                           Bad Case of Loving You                  03:10                                   
The Bangles                             Walk Like an Egyptian                   03:24                                   
The Bangles                             Manic Monday                            03:06                                   
The Beatles                             Help                                    02:18                                   
The Beatles                             Yesterday                               02:05                                   
The Kinks                               Big Black Smoke                         02:36                                   
The Kinks                               Village Green Preservation Society      02:49                                   
The Ramones                             I Wanna Be Sedated                      02:29                                   
The Rolling Stones                      It's Only Rock and Roll                 04:10                                   
The Rolling Stones                      Anybody Seen My Baby?                   04:07                                   
                                                                                                                        
                                                                                                                        
Total music time: 79.41                                                                                                 
PoSH>                                                                                      

This gave me such a perfect fill ( 79.41 ) , and also this would be the most numbers on the CD, as I did know the same list was used to check this script it I kept it with that , as I could make it fancy as I wanted to but it would be to hard to improve upon this score with the same  list ( and it was getting late and I still had one event more to do ;-) ) .

So my script does just sort the the songs from small to big,

$songlist = gc c:\scripts\songlist.csv |% {$a,$t,$d = $_.split(',') ;1 | 
  select @{n='Artist';e={$a}},@{n='Title';e={$t}},@{n='Duration';e={[timespan]"00:$d"}}}

[timespan]$total = 0
$Burn = @()
$songlist | sort duration |% {
  $s=$_
  if (($burn |? {$_.Artist -eq $s.Artist}).count -lt 2 -and ($total + $_.duration).TotalMinutes -lt 80) {
    $burn += $_ ;$total += $_.duration}
  }
$Burn | sort Artist | ft artist,title, @{label='Duration';expression={([datetime]"$($_.duration)").tostring('mm:ss')}}
"Total music time: {0}:{1}" -f [math]::truncate($total.totalMinutes), $total.Seconds
Enjoy,
Greetings /\/\o\/\/
 
 

Published Thursday, February 28, 2008 2:59 PM by MoW

Comments

# re: Scripting Games 2008 Advanced Windows PowerShell Event 8

I've got to post all my solutions, if only to contrast them with yours. I'm amazed at the conciseness of your solutions, usually with the exact same logic!

Mine was similar, I definitely "helped myself out" by sorting from largest song->smallest song. Blah, I just realized I implemented my own "TimeSpan" class, that was a waste on my part.

I'll post all mine at the same time, when the games finish. -Peter

Thursday, February 28, 2008 3:27 PM by Peter

# Scripting Games: Advanced PowerShell - Event 8 « PowerShell ?? ???????????? ??????????????

Anonymous comments are disabled