-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTask092.ps1
37 lines (37 loc) · 1.03 KB
/
Task092.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].Insert(0, 0)
for ($i = $fields.Count-1; $i -gt 0; $i--) {
$thisFirst = [convert]::ToInt32($fields[$i][0])
$prevFirst = [convert]::ToInt32($fields[$i-1][0])
$diff = $prevFirst - $thisFirst
$fields[$i-1].Insert(0, [string]($diff))
if ($i -eq 1) {
$totSum += $diff
}
}
}
Write-Output "Sum: $($totSum)"