-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day4.ps1
152 lines (144 loc) · 5.54 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
#
# https://adventofcode.com/2020
#
#
Start-Transcript .\Day4.log
$InputData = Get-Content .\Day4.data
Write-Host "$($InputData.Count) values to check !"
<#
byr (Birth Year)
iyr (Issue Year)
eyr (Expiration Year)
hgt (Height)
hcl (Hair Color)
ecl (Eye Color)
pid (Passport ID)
cid (Country ID)
#>
Write-Host "#Part1"
$PassportFields = @("byr", "iyr", "eyr", "hgt", "hcl", "ecl", "pid", "cid")
$LineIndex = 0
$NumberOfValidPassports = 0
$NumberOfPassports = 0
$PassportControl = "00000000".ToCharArray()
while ($LineIndex -le $InputData.Count) {
$InputDataLine = $InputData[$LineIndex]
if (($InputDataLine -ne "") -and ($null -ne $InputDataLine)) {
# Let's control the Passports field
foreach ($Field in $InputDataLine.Split(' ')) {
$Field = $Field.Split(':')[0]
$FieldIndex = [array]::IndexOf($PassportFields, $Field)
if ( $FieldIndex -ne -1 ) {
$PassportControl[$FieldIndex] = "1"
}
}
} else {
# We are on a Passport "splitter" (empty line) or at the end of the "file"
$NumberOfPassports += 1
if ( ((-join $PassportControl) -eq "11111111") -or ((-join $PassportControl) -eq "11111110")) {
$NumberOfValidPassports += 1
}
Write-Host "Controlling Passport $($NumberOfPassports) / Password Control = $(-join $PassportControl) / Number of valid Passports = $NumberOfValidPassports"
$PassportControl = "00000000".ToCharArray()
}
$LineIndex += 1
}
Write-Host "The number of valid Passports is $NumberOfValidPassports out of $NumberOfPassports"
Write-Host "#Part2"
# Part 2
$LineIndex = 0
$NumberOfValidPassports = 0
$NumberOfPassports = 0
$PassportControl = "00000000".ToCharArray()
$InvalidReason = ""
while ($LineIndex -le $InputData.Count) {
$InputDataLine = $InputData[$LineIndex]
if (($InputDataLine -ne "") -and ($null -ne $InputDataLine)) {
# Let's control the Passports field
foreach ($Field in $InputDataLine.Split(' ')) {
$FieldValue = $Field.Split(':')[1]
$Field = $Field.Split(':')[0]
$FieldIndex = [array]::IndexOf($PassportFields, $Field)
if ( $FieldIndex -ne -1 ) {
$FieldValid = $true
# Birth year
if ($Field -eq "byr") {
if (([int]$FieldValue -lt 1920) -or ([int]$FieldValue -gt 2002) ) {
$FieldValid = $false
$InvalidReason += " ($Field)"
}
}
# Issue year
if ($Field -eq "iyr") {
if (([int]$FieldValue -lt 2010) -or ([int]$FieldValue -gt 2020) ) {
$FieldValid = $false
$InvalidReason += " ($Field)"
}
}
# Expiration year
if ($Field -eq "eyr") {
if (([int]$FieldValue -lt 2020) -or ([int]$FieldValue -gt 2030) ) {
$FieldValid = $false
$InvalidReason += " ($Field)"
}
}
# Height
if ($Field -eq "hgt") {
if ($FieldValue -match '^(\d*)(cm|in)') {
if ($matches[2] -eq "cm") {
if (([int]$matches[1] -lt 150) -or ([int]$matches[1] -gt 193) ) {
$FieldValid = $false
$InvalidReason += " ($Field)"
}
} else {
if (([int]$matches[1] -lt 59) -or ([int]$matches[1] -gt 76) ) {
$FieldValid = $false
$InvalidReason += " ($Field)"
}
}
} else {
$FieldValid = $false
$InvalidReason += " ($Field)"
}
}
# Hair color
if ($Field -eq "hcl") {
if (-not ($FieldValue -match '^#[0-9a-f]{6}$')) {
$FieldValid = $false
$InvalidReason += " ($Field)"
}
}
# Eye color
if ($Field -eq "ecl") {
if (-not ($FieldValue -match '^(amb|blu|brn|gry|grn|hzl|oth)$')) {
$FieldValid = $false
$InvalidReason += " ($Field)"
}
}
# Passport ID
if ($Field -eq "pid") {
if (-not ($FieldValue -match '^\d{9}$')) {
$FieldValid = $false
$InvalidReason += " ($Field)"
}
}
if ($FieldValid) {
$PassportControl[$FieldIndex] = "1"
}
}
}
}
else {
# We are on a Passport "splitter" (empty line) or at the end of the "file"
$NumberOfPassports += 1
if ( ((-join $PassportControl) -eq "11111111") -or ((-join $PassportControl) -eq "11111110")) {
$NumberOfValidPassports += 1
}
Write-Host "Controlling Passport $($NumberOfPassports) / Password Control = $(-join $PassportControl) $InvalidReason / Number of valid Passports = $NumberOfValidPassports"
$InvalidReason = ""
$PassportControl = "00000000".ToCharArray()
}
$LineIndex += 1
}
Write-Host "The number of valid Passports is $NumberOfValidPassports"
Stop-Transcript