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

commit 4a8670dc62efc98f30b853c6c4c3753bd83e3e1b
parent 04d38c9e24dc4c40c3668400ea86389961920c3f
Author: Eamon Caddigan <eamon.caddigan@gmail.com>
Date:   Sat, 18 Dec 2021 20:42:35 -0500

Solution to day 18, part 2

Diffstat:
Aday18_part2.py | 37+++++++++++++++++++++++++++++++++++++
1 file changed, 37 insertions(+), 0 deletions(-)

diff --git a/day18_part2.py b/day18_part2.py @@ -0,0 +1,37 @@ +#!/usr/bin/env python +"""Advent of Code 2021, day 18 (part 2): Snailfish math +Snilfish math!""" + + +from typing import List +from itertools import permutations +from day18_part1 import (EXAMPLE_INPUT, + MathNode, + parse_input, + add_nodes, + find_magnitude) +from utils import get_puzzle_input + +def find_pairwise_sums(input_nodes: List[MathNode]) -> List[int]: + """Find the list of every pair of nodes in the list (in both orderings)""" + pairwise_sums = [] + for node_1, node_2 in permutations(input_nodes, 2): + pairwise_sums.append( + find_magnitude( + add_nodes(node_1, node_2) + ) + ) + return pairwise_sums + +def solve_puzzle(input_string: str) -> int: + """Return the numeric solution to the puzzle""" + return max(find_pairwise_sums(parse_input(input_string))) + +def main() -> None: + """Run when the file is called as a script""" + assert solve_puzzle(EXAMPLE_INPUT) == 3993 + print("Magnitude of the highest possible sum of pairs:", + solve_puzzle(get_puzzle_input(18))) + +if __name__ == "__main__": + main()