Nim
Part 1:
The extrapolated value to the right is just the sum of all last values in the diff pyramid. 45 + 15 + 6 + 2 + 0 = 68
Part 2:
The extrapolated value to the left is just a right-folded difference (right-associated subtraction) between all first values in the pyramid. e.g. 10 - (3 - (0 - (2 - 0))) = 5
So, extending the pyramid is totally unneccessary.
Total runtime: 0.9 ms
Puzzle rating: Easy, but interesting 6.5/10
Full Code: day_09/solution.nim
Snippet:
proc solve(lines: seq[string]): AOCSolution[int] =
for line in lines:
var current = line.splitWhitespace().mapIt(it.parseInt())
var firstValues: seq[int]
while not current.allIt(it == 0):
firstValues.add current[0]
block p1:
result.part1 += current[^1]
var nextIter = newSeq[int](current.high)
for i, v in current[1..^1]:
nextIter[i] = v - current[i]
current = nextIter
block p2:
result.part2 += firstValues.foldr(a-b)