-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day16Part2.ps1
122 lines (104 loc) · 2.55 KB
/
Day16Part2.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
function Clone {
param (
$Map
)
$Clone = @()
for ($Row = 0; $Row -lt $Map.Count; $Row++) {
$Clone += ,$Map[$Row].Clone()
}
$Clone
}
function Energize {
param (
$x,
$y,
$XDir,
$YDir
)
$temp = 0
while ($x -ge 0 -and $x -lt $Map[0].Count -and $y -ge 0 -and $y -lt $Map.Count) {
$VisualMap[$y][$x] = "#"
if (-not ($Energized["$x,$y"] -eq "$XDir,$YDir")) {
$Energized["$x,$y"] = "$XDir,$YDir"
} else {
break
}
switch ($Map[$y][$x]) {
"." {
$x += $XDir
$y += $YDir
break
}
"-" {
if ($XDir -ne 0) {
$x += $XDir
} else {
Energize ($x-1) $y -1 0
$XDir = 1
$YDir = 0
$x++
}
break
}
"|" {
if ($XDir -ne 0) {
Energize $x ($y-1) 0 -1
$XDir = 0
$YDir = 1
$y++
} else {
$y += $YDir
}
break
}
"/" {
if ($XDir -ne 0) {
$YDir -= $XDir
$XDir = 0
$y += $YDir
} else {
$XDir -= $YDir
$YDir = 0
$x += $XDir
}
break
}
"\" {
if ($XDir -ne 0) {
$YDir = $XDir
$XDir = 0
$y += $YDir
} else {
$XDir = $YDir
$YDir = 0
$x += $XDir
}
break
}
}
}
}
$MapText = Get-Clipboard
$Map = @()
foreach ($Row in $MapText) {
$Map += ,$Row.ToCharArray()
}
$VisualMap = Clone $Map
$Max = 0
for ($Row = 0; $Row -lt $Map.Count; $Row++) {
$Energized = @{}
Energize 0 $Row 1 0
$Max = [Math]::Max($Max, $Energized.Count)
$Energized = @{}
Energize ($Map[0].Count-1) $Row -1 0
$Max = [Math]::Max($Max, $Energized.Count)
}
for ($Col = 0; $Col -lt $Map[0].Count; $Col++) {
$Energized = @{}
Energize $Col 0 0 1
$Max = [Math]::Max($Max, $Energized.Count)
$Energized = @{}
Energize $Col ($Map.Count-1) 0 -1
$Max = [Math]::Max($Max, $Energized.Count)
}
$Max