commit 43a6f9ac7410585c2eb9707ec05fef6105da4e37
parent d05ab6a516b87d46acbb0fcfb9126895620bdc44
Author: Eamon Caddigan <eamon.caddigan@gmail.com>
Date: Wed, 1 Dec 2021 05:29:02 -0500
My solution to part 2
Diffstat:
1 file changed, 31 insertions(+), 0 deletions(-)
diff --git a/day01_part2.py b/day01_part2.py
@@ -0,0 +1,31 @@
+#!/usr/bin/env python
+"""For part 2, we use the same data but sum over a three-element sliding window
+before calculating diffs as before."""
+
+# This is my first Advent of Code, so I wasn't sure how closely related the two
+# parts would be, so I didn't know how much code reuse would be typical. I like
+# that I can just `import` a previous solution and get any useful definitions
+# from there (though I'll try to be mindful of what should go into `utils.py`.
+
+from day01_part1 import download_input_data, calculate_depth_diffs, count_depth_increases
+
+def calculate_sliding_sum(depth_series):
+ """Given a pandas series of depths, returns a new series where each element
+ has been replaced by the sum of that element and the preceding two
+ elements"""
+ return depth_series.rolling(window=3).sum()
+
+if __name__ == "__main__":
+ """Download the data, find the answer, and print it to the terminal"""
+ depth_data = download_input_data()
+
+ depth_data['depth_rolling_sum'] = \
+ calculate_sliding_sum(depth_data['depth'])
+
+ depth_data['depth_rolling_sum_diff'] = \
+ calculate_depth_diffs(depth_data['depth_rolling_sum'])
+
+ number_of_increases_p2 = \
+ count_depth_increases(depth_data['depth_rolling_sum_diff'])
+
+ print(f'{number_of_increases_p2 = }')