-
Notifications
You must be signed in to change notification settings - Fork 0
/
advent-2016-day7.ps1
76 lines (63 loc) · 2.36 KB
/
advent-2016-day7.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
# abba[mnop]qrst supports TLS (abba outside square brackets).
# abcd[bddb]xyyx does not support TLS (bddb is within square brackets, even though xyyx is outside square brackets).
# aaaa[qwer]tyui does not support TLS (aaaa is invalid; the interior characters must be different).
# ioxxoj[asdfgh]zxcvbn supports TLS (oxxo is outside square brackets, even though it's within a larger string).
function Get-ABBA {
param( [array]$sequences )
$hasABBA = $false
foreach ($seq in $sequences) {
for ($i = 0; ($i + 4) -le $seq.Length; $i++) {
$pair1 = $seq.Substring($i, 2);
$pair2 = $seq.Substring($i+2, 2)
$reverseMatches = ($pair1[0] -eq $pair2[1]) -and ($pair1[1] -eq $pair2[0])
$pairsAreDifferent = $pair1 -ne $pair2
if ($reverseMatches -and $pairsAreDifferent) {
$hasABBA = $true
}
}
}
$hasABBA
}
$input = "abba[mnop]qrst
abcd[bddb]xyyx
aaaa[qwer]tyui
ioxxoj[asdfgh]zxcvbn"
$input = Get-Content -Path "day7.txt"
$tlsSupportCount = 0
foreach ($line in $input.Split("`n")) {
$line = $line.Trim()
$outerSequences = New-Object System.Collections.Generic.List[System.Object]
$hypernetSeqs = New-Object System.Collections.Generic.List[System.Object]
$outerSequence = ''
$hypernetSeq = ''
$hypernetSeqSection = 0
foreach ($character in $line.ToCharArray()) {
if ($character -eq '[') {
$hypernetSeqSection = 1
$outerSequences.Add($outerSequence)
$outerSequence = ''
} elseif ($character -eq ']') {
$hypernetSeqSection = 0
$hypernetSeqs.Add($hypernetSeq)
$hypernetSeq = ''
} else {
# Check if we are reading in the hypernet
if ($hypernetSeqSection) {
$hypernetSeq += $character
} else {
$outerSequence += $character
}
}
}
if ($outerSequence.Length -gt 0) {
$outerSequences.Add($outerSequence)
}
$outerSeqGood = Get-ABBA $outerSequences
$hyperSeqNotABBA = Get-ABBA $hypernetSeqs
#Write-Output "Line $line outerSeqGood: $outerSeqGood hyperSeqNotABBA: $hyperSeqNotABBA"
if ($outerSeqGood -and !$hyperSeqNotABBA) {
Write-Output "Line $line supports TLS"
$tlsSupportCount++
}
}
Write-Output "$tlsSupportCount IPs support TLS"