-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day8Part2.ps1
94 lines (75 loc) · 1.79 KB
/
Day8Part2.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
function Gcd {
param (
$Num1,
$Num2
)
while ($Num2) {
$temp = $Num2
$Num2 = $Num1 % $Num2
$Num1 = $temp
}
$Num1
}
function Gcd-Range {
param (
[Int[]]$Numbers
)
$Gcd = $Numbers[0]
for ($ndx = 1; $ndx -lt $Numbers.Count; $ndx++) {
$Gcd = Gcd $Gcd $Numbers[$ndx]
}
$Gcd
}
function Get-Gcm {
param (
$Numbers
)
$Gcd = Gcd-Range $Numbers
$Gcm = 1
foreach ($Number in $Numbers) {
$Gcm *= $Number / $Gcd
}
$Gcm * $Gcd
}
$Map = Get-Clipboard | ?{$_ -ne ""}
$Instructions = $Map[0] -split "" | ?{$_ -ne ""}
$Nodes = @{}
for ($ndx = 1; $ndx -lt $Map.Count; $ndx++) {
$Node, $Directions = $Map[$ndx] -split "="
$Left, $Right = $Directions -split ","
$Nodes[$Node.Trim()] = "" | select @{n="L";e={$Left.Trim() -replace "\("}},@{n="R";e={$Right.Trim() -replace "\)"}}
}
$Paths = @{}
foreach ($Node in $Nodes.Keys) {
if ($Node.Substring(2,1) -eq "A") {
$Paths[$Node] = "" | select @{n="Node";e={$Node}},Done,Step
}
}
$Node = "AAA"
$Step = 0
$Instruction = 0
$AllZ = $false
while (-not $AllZ) {
$AllZ = $true
foreach ($Path in $($Paths.Keys)) {
if ($Paths[$Path].Done -ne $true) {
$Paths[$Path].Node = $Nodes[$Paths[$Path].Node].($Instructions[$Instruction])
if ($Paths[$Path].Node.Substring(2,1) -eq "Z") {
$Paths[$Path].Done = $true
$Paths[$Path].Step = $Step + 1
} else {
$AllZ = $false
}
}
}
$Instruction++
if ($Instruction -ge $Instructions.Count) {
$Instruction = 0
}
$Step++
}
$Steps = @()
foreach ($Path in $Paths.Keys) {
$Steps += $Paths[$Path].Step
}
Get-Gcm $Steps