-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAdventOfCode - Day 4.2.ps1
121 lines (98 loc) · 2.93 KB
/
AdventOfCode - Day 4.2.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
$passports = @()
$new = $true
$passport = $null
Get-Content -Path '.\passports day 4.txt' | % {
if ($new) {
if ($null -ne $passport) {
$passports += $passport
}
$passport = New-Object -TypeName PSObject
}
$new = [String]::IsNullOrEmpty($_)
$pairs = $_ -split ' '
foreach ($pair in $pairs) {
$key = ($pair -split ':')[0]
$value = ($pair -split ':')[1]
if ($key -ne '' -and $value -ne '') {
Add-Member -InputObject $passport -MemberType NoteProperty -Name $key -Value $value
}
}
}
$passports += $passport
$validPassports = 0
for ($i = 0; $i -lt $passports.length; $i++) {
$passport = $passports[$i]
if ($null -ne $passport.byr -and
$null -ne $passport.iyr -and
$null -ne $passport.eyr -and
$null -ne $passport.hgt -and
$null -ne $passport.hcl -and
$null -ne $passport.ecl -and
$null -ne $passport.pid) {
try {
$byr = [int]::Parse($passport.byr)
} catch {
continue
}
if ($passport.byr.length -ne 4 -or $byr -lt 1920 -or $byr -gt 2002) {
continue
}
try {
$iyr = [int]::Parse($passport.iyr)
} catch {
continue
}
if ($passport.iyr.length -ne 4 -or $iyr -lt 2010 -or $iyr -gt 2020) {
continue
}
try {
$eyr = [int]::Parse($passport.eyr)
} catch {
continue
}
if ($passport.eyr.length -ne 4 -or $eyr -lt 2020 -or $eyr -gt 2030) {
continue
}
$isValid = $passport.hgt -match "(\d+)(in|cm)"
if ($isValid -eq $false) {
continue
}
$isValid = $true
switch ($Matches[2]) {
"in" {
try {
$length = [int]::Parse($Matches[1])
} catch {
$isValid = $false
}
if ($length -lt 59 -or $length -gt 76) {
$isValid = $false
}
}
"cm" {
try {
$length = [int]::Parse($Matches[1])
} catch {
$isValid = $false
}
if ($length -lt 150 -or $length -gt 193) {
$isValid = $false
}
}
}
if (!$isValid) {
continue
}
if (($passport.hcl -match "#[0-9|a-f]{6}") -eq $false) {
continue
}
if (($passport.ecl -match "^amb|blu|brn|gry|grn|hzl|oth$") -eq $false) {
continue
}
if (($passport.pid.Length -eq 9 -and $passport.pid -match "^\d{9}$") -eq $false) {
continue
}
$validPassports++
}
}
$validPassports