From 5f4017a2f9b75b64a182ad6513e118d4133a3558 Mon Sep 17 00:00:00 2001 From: Michael Jalkio Date: Thu, 28 Dec 2023 03:16:10 +0000 Subject: [PATCH] Solve day 9 part 2 --- year_2023/day09/mirage_maintenance.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/year_2023/day09/mirage_maintenance.py b/year_2023/day09/mirage_maintenance.py index 75515a6..4c62c91 100644 --- a/year_2023/day09/mirage_maintenance.py +++ b/year_2023/day09/mirage_maintenance.py @@ -14,9 +14,15 @@ def sum_extrapolated_values(puzzle_input, backwards=False): sequences[-1].append(0) for i in reversed(range(len(sequences) - 1)): - sequences[i].append(sequences[i][-1] + sequences[i + 1][-1]) + if backwards: + sequences[i].insert(0, (sequences[i + 1][0] - sequences[i][0]) * -1) + else: + sequences[i].append(sequences[i][-1] + sequences[i + 1][-1]) - extrapolated_sum += sequences[0][-1] + if backwards: + extrapolated_sum += sequences[0][0] + else: + extrapolated_sum += sequences[0][-1] return extrapolated_sum