-
Notifications
You must be signed in to change notification settings - Fork 0
/
advent-2016-day3-part2.ps1
39 lines (32 loc) · 1.16 KB
/
advent-2016-day3-part2.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
$input = Get-Content -Path "day3.txt"
$count = 0
$lineNo = 0
$triangles = New-Object System.Collections.Generic.List[System.Object]
foreach ($line in $input.Split("`n")) {
$line = $line.Trim()
$lineNo++
$sides = $line.Split(" ", [System.StringSplitOptions]::RemoveEmptyEntries)
$pos = 0
foreach ($side in $sides) {
if ($triangles.Count -le $pos) {
$o = New-Object System.Collections.Generic.List[System.Object]
$triangles.Add($o)
}
$triangles[$pos++].Add($side)
}
# Check if we have 3 sides to compare
if (($triangles.Count -gt 0) -and ($triangles[0].Count -eq 3)) {
foreach ($triangle in $triangles) {
$side1 = [int]$triangle[0]
$side2 = [int]$triangle[1]
$side3 = [int]$triangle[2]
# Remove the processed sides
$triangle.Clear()
# Check that each pair of sides is greater than the remaining side
if ((($side1 + $side2) -gt $side3) -and (($side1 + $side3) -gt $side2) -and (($side2 + $side3) -gt $side1)) {
$count++
}
}
}
}
Write-Output "Valid Triangle Count: $count"