-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTask091.ps1
37 lines (37 loc) · 1.05 KB
/
Task091.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
[int]$totSum = 0
foreach($line in Get-Content input.txt) {
[System.Collections.ArrayList]$fields = @(@())
[System.Collections.ArrayList]$newRow = $line.Split(" ")
$fields.Add($newRow)
$hasNonZero = $false
do {
$hasNonZero = $false
$firstVal = $true
$prevVal = 0;
$newRow = @()
forEach ($val in $fields[$fields.Count-1]) {
if ($firstVal) {
$firstVal = $false
} else {
$diff = $val - $prevVal
$newRow.Add($diff)
if ($diff -ne 0) {
$hasNonZero = $true
}
}
$prevVal = $val
}
$fields.Add($newRow)
} while ($hasNonZero)
$fields[$fields.Count-1].Add(0)
for ($i = $fields.Count-1; $i -gt 0; $i--) {
$thisLast = [convert]::ToInt32($fields[$i][$fields[$i].Count-1])
$prevLast = [convert]::ToInt32($fields[$i-1][$fields[$i-1].Count-1])
$sum = $prevLast + $thisLast
$fields[$i-1].Add([string]($sum))
if ($i -eq 1) {
$totSum += $sum
}
}
}
Write-Output "Sum: $($totSum)"