commit ce2777308aac33df83c0371bed66a57b772174a1
parent 9f04e689b1bea48107ab1139ed55a92981a1e28b
Author: Eamon Caddigan <eamon.caddigan@gmail.com>
Date: Wed, 1 Dec 2021 19:04:20 -0500
Cleaning things up and making them more functional
Diffstat:
2 files changed, 22 insertions(+), 22 deletions(-)
diff --git a/day01_part1.py b/day01_part1.py
@@ -30,12 +30,11 @@ def count_depth_increases(depth_diff_series):
increases"""
return (depth_diff_series > 0).sum()
-if __name__ == "__main__":
- """Download the data, find the answer, and print it to the terminal"""
- depth_data = download_input_data()
-
- depth_data['depth_diff'] = calculate_depth_diffs(depth_data['depth'])
+def solve_puzzle():
+ """Return the numeric solution to the puzzle"""
+ return count_depth_increases(
+ calculate_depth_diffs(download_input_data()['depth'])
+ )
- number_of_increases = count_depth_increases(depth_data['depth_diff'])
-
- print(f'{number_of_increases = }')
+if __name__ == "__main__":
+ print("Number of depth increases:", solve_puzzle())
diff --git a/day01_part2.py b/day01_part2.py
@@ -7,25 +7,26 @@ before calculating diffs as before."""
# 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
+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"""
+ # It's not actually necessary to calculate the rolling sum to solve the
+ # puzzle, but this is great pandas practice for me
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'])
+def solve_puzzle():
+ """Return the numeric solution to the puzzle"""
+ return count_depth_increases(
+ calculate_depth_diffs(
+ calculate_sliding_sum(download_input_data()['depth'])
+ )
+ )
- number_of_increases_p2 = \
- count_depth_increases(depth_data['depth_rolling_sum_diff'])
-
- print(f'{number_of_increases_p2 = }')
+if __name__ == "__main__":
+ print("Number of depth increases (using a sliding window):",
+ solve_puzzle())