-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday4.ps1
203 lines (141 loc) · 4.78 KB
/
day4.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
$matrix = @"
MMMSXXMASM
MSAMXMSMSA
AMXSXMAAMM
MSAMASMSMX
XMASAMXAMM
XXAMMXXAMA
SMSMSASXSS
SAXAMASAAA
MAMMMXMMMM
MXMXAXMASX
"@
$matrix = Get-Content .\input_day4.txt
# Search forward and backward on each line
# Search up and down in each row
# Search diagonal up and down
# Instead of adapting to search directions. Search from left to right only. But rotate the matrix by 90 before each search.
#[search] -> 123 741 987 369 123
# 456 852 654 258 456
# 789 963 321 147 789
$q = $matrix -split "\r\n",""
$buffer1 = New-Object system.collections.arraylist
$buffer2 = New-Object System.Collections.ArrayList
foreach ($i in $matrix) {
[void]$buffer1.Add($i.tochararray())
[void]$buffer2.Add($i.tochararray())
}
$matches = 0
[char[]]$search = "XMAS"
for ($i=0; $i -lt $buffer1.count ; $i++) {
for ($j=0; $j -lt $buffer1.count ; $j++) {
[bool]$bfound = $true
0..3 | %{if ($buffer1[$i][$j + $_] -eq $search[$_]){ $bfound = $bfound -and $true}else{$bfound = $bfound -and $false} }
if ($bfound) {write-host $i $j; $matches+=1;}
}
}
[array]::Reverse($search)
for ($i=0; $i -lt $buffer1.count ; $i++) {
for ($j=0; $j -lt $buffer1.count ; $j++) {
[bool]$bfound = $true
0..3 | %{if ($buffer1[$i][$j + $_] -eq $search[$_]){ $bfound = $bfound -and $true}else{$bfound = $bfound -and $false} }
if ($bfound) {write-host $i $j; $matches+=1;}
}
}
[array]::Reverse($search)
# Transpose the array and reverse each row to get a 90 degree rotation, then search
for ($i=0; $i -lt $buffer1.count ; $i++) {
for ($j=0; $j -lt $buffer1.count ; $j++) {
$buffer2[$j][$i] = $buffer1[$i][$j]
}
}
for ($i=0; $i -lt $buffer1.count ; $i++) {
[array]::Reverse($buffer2[$i])
}
for ($i=0; $i -lt $buffer2.count ; $i++) {
for ($j=0; $j -lt $buffer2.count ; $j++) {
[bool]$bfound = $true
0..3 | %{if ($buffer2[$i][$j + $_] -eq $search[$_]){ $bfound = $bfound -and $true}else{$bfound = $bfound -and $false} }
if ($bfound) {write-host $i $j; $matches+=1;}
}
}
[array]::Reverse($search)
for ($i=0; $i -lt $buffer2.count ; $i++) {
for ($j=0; $j -lt $buffer2.count ; $j++) {
[bool]$bfound = $true
0..3 | %{if ($buffer2[$i][$j + $_] -eq $search[$_]){ $bfound = $bfound -and $true}else{$bfound = $bfound -and $false} }
if ($bfound) {write-host $i $j; $matches+=1;}
}
}
[array]::Reverse($search)
# Diagonal search
# Get the diagonals starting in the bottom left and working to top right
[int]$n = $buffer1.count -1
$rows = $buffer1.count
$cols = $buffer1.count
$diaglist = New-Object system.collections.arraylist
while ($n -gt -($buffer1.count)) {
[string]$line=""
#Set starting point, either moving up to 0, or to right of 0.
if ($n -ge 0) { [int]$r=$n; [int]$c=0}
if ($n -lt 0) { [int]$r=0; [int]$c=[math]::abs($n)}
while ([int]$r -lt $rows -and $c -lt $cols) {
$line+= $buffer1[$r][$c]
$r+=1
$c+=1
}
[void]$diaglist.add($line)
$n-=1
}
for ($i=0; $i -lt $diaglist.count ; $i++) {
for ($j=0; $j -lt $diaglist.count ; $j++) {
[bool]$bfound = $true
0..3 | %{if ($diaglist[$i][$j + $_] -eq $search[$_]){ $bfound = $bfound -and $true}else{$bfound = $bfound -and $false} }
if ($bfound) {write-host $i $j; $matches+=1;}
}
}
[array]::Reverse($search)
for ($i=0; $i -lt $diaglist.count ; $i++) {
for ($j=0; $j -lt $diaglist.count ; $j++) {
[bool]$bfound = $true
0..3 | %{if ($diaglist[$i][$j + $_] -eq $search[$_]){ $bfound = $bfound -and $true}else{$bfound = $bfound -and $false} }
if ($bfound) {write-host $i $j; $matches+=1;}
}
}
[array]::Reverse($search)
# diagonal search on matrix rotated 90.
[int]$n = $buffer2.count -1
$rows = $buffer2.count
$cols = $buffer2.count
$diaglist = New-Object system.collections.arraylist
while ($n -gt -($buffer2.count)) {
[string]$line=""
#Set starting point, either moving up to 0, or to right of 0.
if ($n -ge 0) { [int]$r=$n; [int]$c=0}
if ($n -lt 0) { [int]$r=0; [int]$c=[math]::abs($n)}
while ([int]$r -lt $rows -and $c -lt $cols) {
$line+= $buffer2[$r][$c]
$r+=1
$c+=1
}
[void]$diaglist.add($line)
$n-=1
}
for ($i=0; $i -lt $diaglist.count ; $i++) {
for ($j=0; $j -lt $diaglist.count ; $j++) {
[bool]$bfound = $true
0..3 | %{if ($diaglist[$i][$j + $_] -eq $search[$_]){ $bfound = $bfound -and $true}else{$bfound = $bfound -and $false} }
if ($bfound) {write-host $i $j; $matches+=1;}
}
}
[array]::Reverse($search)
for ($i=0; $i -lt $diaglist.count ; $i++) {
for ($j=0; $j -lt $diaglist.count ; $j++) {
[bool]$bfound = $true
0..3 | %{if ($diaglist[$i][$j + $_] -eq $search[$_]){ $bfound = $bfound -and $true}else{$bfound = $bfound -and $false} }
if ($bfound) {write-host $i $j; $matches+=1;}
}
}
[array]::Reverse($search)
#2323 too low