advent_of_code_2021

My attempts to work through the 2021 Advent of Code problems.
git clone https://git.eamoncaddigan.net/advent_of_code_2021.git
Log | Files | Refs | README | LICENSE

day01_part2.py (1329B)


      1 #!/usr/bin/env python
      2 """For part 2, we use the same data but sum over a three-element sliding window
      3 before calculating diffs as before."""
      4 
      5 # This is my first Advent of Code, so I wasn't sure how closely related the two
      6 # parts would be, so I didn't know how much code reuse would be typical. I like
      7 # that I can just `import` a previous solution and get any useful definitions
      8 # from there (though I'll try to be mindful of what should go into `utils.py`.
      9 
     10 from day01_part1 import (download_input_data,
     11                          calculate_depth_diffs,
     12                          count_depth_increases)
     13 
     14 def calculate_sliding_sum(depth_series):
     15     """Given a pandas series of depths, returns a new series where each element
     16     has been replaced by the sum of that element and the preceding two
     17     elements"""
     18     # It's not actually necessary to calculate the rolling sum to solve the
     19     # puzzle, but this is great pandas practice for me
     20     return depth_series.rolling(window=3).sum()
     21 
     22 def solve_puzzle():
     23     """Return the numeric solution to the puzzle"""
     24     return count_depth_increases(
     25         calculate_depth_diffs(
     26             calculate_sliding_sum(download_input_data()['depth'])
     27         )
     28     )
     29 
     30 if __name__ == "__main__":
     31     print("Number of depth increases (using a sliding window):",
     32           solve_puzzle())