2023 Day 9 Part 2
This commit is contained in:
parent
edc2a135c8
commit
bf38cbdc69
1 changed files with 22 additions and 0 deletions
|
|
@ -56,4 +56,26 @@ proc part1(): int =
|
|||
var hh = deduce(h)
|
||||
result.inc extrapolate(hh)[^1]
|
||||
|
||||
func extrapolateBack(hs: var Histories): seq[int] =
|
||||
assert(hs[^1][0] == 0)
|
||||
|
||||
var row = len(hs) - 1
|
||||
result.add 0
|
||||
hs[row].insert(0, 0)
|
||||
|
||||
while row > 0:
|
||||
let inferred = hs[row-1][0] - hs[row][0]
|
||||
result.add inferred
|
||||
hs[row-1].insert(inferred, 0)
|
||||
dec row
|
||||
|
||||
proc part2(): int =
|
||||
let text = readFile("input/day09.txt")
|
||||
var histories = parseHistories(text)
|
||||
|
||||
for h in histories:
|
||||
var hh = deduce(h)
|
||||
result.inc extrapolateBack(hh)[^1]
|
||||
|
||||
echo part1()
|
||||
echo part2()
|
||||
|
|
|
|||
Loading…
Reference in a new issue