Skip to content

Commit 3edc9a6

Browse files
committed
Day 9 - part 2
1 parent 0778a4d commit 3edc9a6

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

2023/day09/solution.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,15 @@ def step_differences(histories:list) -> list[list[list]]:
2222
return differences
2323

2424

25-
def extrapolate_history(histories:str) -> list:
25+
def extrapolate_history(histories:str, reverse:bool = False) -> list:
2626
extrapolated = []
2727
for history in histories:
2828
value = 0
2929
for x in range(len(history)-2, -1, -1):
30-
value += history[x][-1]
30+
if reverse:
31+
value = history[x][0] - value
32+
else:
33+
value += history[x][-1]
3134
extrapolated.append(value)
3235

3336
return extrapolated
@@ -40,3 +43,7 @@ def extrapolate_history(histories:str) -> list:
4043
# Part 1
4144
extrapolated = extrapolate_history(differences)
4245
print(f"All the extrapolated values add up to {sum(extrapolated)}")
46+
47+
# Part 2
48+
extrapolated = extrapolate_history(differences, True)
49+
print(f"All the previous extrapolated values add up to {sum(extrapolated)}")

0 commit comments

Comments
 (0)