day14_part2.py (1101B)
1 #!/usr/bin/env python 2 """Advent of Code 2021, day 14 (part 2): Extended Polymerization 3 Perform 'pair insertions' on a 'polymer' string even more times""" 4 5 # Ah-ha, it _is_ lanternfish all over again; I'm glad I went with a compact 6 # representation. I was worried that I'd need to find 4-grams or something 7 # which would have required a complete retooling, and I'm glad I didn't program 8 # for it. 9 10 from day14_part1 import (EXAMPLE_INPUT, 11 parse_input, 12 apply_insertions) 13 from utils import get_puzzle_input 14 15 def solve_puzzle(input_string: str) -> int: 16 """Return the numeric solution to the puzzle""" 17 element_count = apply_insertions(*(parse_input(input_string) + (40,))) 18 return int(element_count.max() - element_count.min()) 19 20 def main() -> None: 21 """Run when the file is called as a script""" 22 assert solve_puzzle(EXAMPLE_INPUT) == 2188189693529 23 print("Difference between most and least common elements", 24 "(after many more insertions):", 25 solve_puzzle(get_puzzle_input(14))) 26 27 if __name__ == "__main__": 28 main()