-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day3Part2.ps1
43 lines (36 loc) · 1.05 KB
/
Day3Part2.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
$Engine = Get-Clipboard
$ToSum = @()
$GearDic = @{}
$PrevNumbers = @()
$PrevSymbols = @()
function GetAdjacent {
param (
$Numbers,
$Symbols
)
foreach ($Number in $Numbers.Matches) {
foreach ($Gear in $Symbols.Matches) {
if ($Gear.Index -ge $Number.Index - 1 -and $Gear.Index -le $Number.Index + $Number.Length) {
if (-not $GearDic.ContainsKey($Gear)) {
$GearDic[$Gear] = @()
}
$GearDic[$Gear] += [Int]$Number.Value
}
}
}
}
foreach ($Line in $Engine) {
$Numbers = $Line | Select-String -Pattern '\d+' -AllMatches
$Symbols = $Line | Select-String -Pattern '\*' -AllMatches
GetAdjacent $Numbers $Symbols
GetAdjacent $Numbers $PrevSymbols
GetAdjacent $PrevNumbers $Symbols
$PrevNumbers = $Numbers
$PrevSymbols = $Symbols
}
foreach ($Key in $GearDic.Keys) {
if ($GearDic[$Key].Count -eq 2) {
$ToSum += $GearDic[$Key][0] * $GearDic[$Key][1]
}
}
($ToSum | Measure-Object -Sum).Sum