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

day18_part2.py (1179B)


      1 #!/usr/bin/env python
      2 """Advent of Code 2021, day 18 (part 2): Snailfish math
      3 Snilfish math!"""
      4 
      5 
      6 from typing import List
      7 from itertools import permutations
      8 from day18_part1 import (EXAMPLE_INPUT,
      9                          MathNode,
     10                          parse_input,
     11                          add_nodes,
     12                          find_magnitude)
     13 from utils import get_puzzle_input
     14 
     15 def find_pairwise_sums(input_nodes: List[MathNode]) -> List[int]:
     16     """Find the list of every pair of nodes in the list (in both orderings)"""
     17     pairwise_sums = []
     18     for node_1, node_2 in permutations(input_nodes, 2):
     19         pairwise_sums.append(
     20             find_magnitude(
     21                 add_nodes(node_1, node_2)
     22             )
     23         )
     24     return pairwise_sums
     25 
     26 def solve_puzzle(input_string: str) -> int:
     27     """Return the numeric solution to the puzzle"""
     28     return max(find_pairwise_sums(parse_input(input_string)))
     29 
     30 def main() -> None:
     31     """Run when the file is called as a script"""
     32     assert solve_puzzle(EXAMPLE_INPUT) == 3993
     33     print("Magnitude of the highest possible sum of pairs:",
     34           solve_puzzle(get_puzzle_input(18)))
     35 
     36 if __name__ == "__main__":
     37     main()