-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day7Part1.ps1
72 lines (57 loc) · 1.56 KB
/
Day7Part1.ps1
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
class CamelHand : System.IComparable {
[string] $Cards
[Int]$Bid
[Int]$Rank
hidden $CardRank = @{
"2" = "2"
"3" = "3"
"4" = "4"
"5" = "5"
"6" = "6"
"7" = "7"
"8" = "8"
"9" = "9"
"T" = "A"
"J" = "B"
"Q" = "C"
"K" = "D"
"A" = "E"
}
CamelHand ([string]$Cards, [Int]$Bid) {
$this.Cards = $Cards
$this.Bid = $Bid
$this.RankHand()
}
RankHand() {
$Sorted = ($this.Cards -split "" | sort) -join ""
$ThisMatches = $Sorted | Select-String -Pattern '(.)\1{1,}' -AllMatches
$MatchScore = 0
[string]$Score = ""
foreach ($Match in $ThisMatches.Matches) {
$MatchScore += [Math]::Pow($Match.Length, 2)
}
$Score = $MatchScore
foreach ($Card in $this.Cards -split "" | ?{$_ -ne ""}) {
$Score += $this.CardRank[$Card]
}
$this.Rank = [Convert]::ToInt64($Score, 16)
}
[int] CompareTo($OtherHand) {
if ($this.Rank -gt $OtherHand.Rank) {return 1}
if ($this.Rank -eq $OtherHand.Rank) {return 0}
if ($this.Rank -lt $OtherHand.Rank) {return -1}
return $null
}
}
$Hands = Get-Clipboard
$AllHands = @()
foreach ($Hand in $Hands) {
$Cards, $Bid = $Hand -split " "
$AllHands += New-Object CamelHand $Cards, $Bid
}
$SortedHands = $AllHands | sort
$TotalWinnings = 0
for ($ndx = 1; $ndx -le $SortedHands.Count; $ndx++) {
$TotalWinnings += $SortedHands[$ndx-1].Bid * $ndx
}
$TotalWinnings