Scripting Games 2008 Advanced Windows PowerShell Event 4
In this event we had to make a console based month calendar :
PoSH> .\event4.ps1 03/03/08
March, 2008
Sun Mon Tue Wed Thu Fri Sat
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31
PoSH> .\event4.ps1 'feb 08'
February, 2008
Sun Mon Tue Wed Thu Fri Sat
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29
PoSH>
My solution did look like this :
Param ([datetime]$date)
$date.ToString('MMMM, yyyy')
$Month = [datetime]$date.ToString('MMMM, yyyy')
write-host -fore 'cyan' "Sun Mon Tue Wed Thu Fri Sat "
$Line = " " * ([int][DayOfWeek]$Month.DayOfWeek * 6 )
1 .. (( Get-Culture ).Calendar.GetDaysInMonth( $date.year,$date.Month)) |% {
$day = "$_".PadLeft(3)
if ($line.length -gt 41) {$line;$line =""}
$line += $day + " "
}
$line
After a comment from Chris Warwick hat he could do it in 327 Characters ;-) , he is also posting all his solutions onto his blog , his solution you can find here : Scripting Games, Advanced Event 4, Image is Everything I optimized obfuscated the original script, and removed the coloring and checking for given day to make it a bit shorter :
PoSH> gc .\event4_short.ps1 | Measure-Object -Word -Line -Character
Lines Words Characters Property
----- ----- ---------- --------
10 26 234
PoSH> .\event4_short.ps1 02/2003
February, 2003
Sun Mon Tue Wed Thu Fri Sat
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28
PoSH>
That version does look like this :
Param ([datetime]$d)
$d.ToString('MMMM, yyyy')
"Sun Mon Tue Wed Thu Fri Sat"
$L=" "*([int][DayOfWeek]$d.DayOfWeek*4)
1..([datetime]::DaysInMonth($d.year,$d.Month))|%{
$e = "$_".PadLeft(3)
if ($l.length -gt 27){$l;$l =""}
$l+=$e+" "
}
$l
Enjoy,
Greetings /\/\o\/\/