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

day19_part2.py (1356B)


      1 #!/usr/bin/env python
      2 """Advent of Code 2021, day 19 (part 2): Beacon Scanner
      3 Given positions (but not consistent frame of reference), find the overlap
      4 between a set of beacons detected by several scanners"""
      5 
      6 #  One of those plesant situations where my solution to part 1 solves part 2
      7 
      8 from typing import List
      9 import numpy as np
     10 from scipy.spatial.distance import cdist
     11 from day19_part1 import (EXAMPLE_INPUT,
     12                          parse_input,
     13                          create_beacon_network)
     14 from utils import get_puzzle_input
     15 
     16 def find_beacon_distance(transformation_list: List[np.ndarray]) -> np.ndarray:
     17     """Return a distance matrix of the beacons by pulling out the translation
     18     component of the transformation matrices"""
     19     scanner_locations = np.array(
     20         [x[3, 0:3] for x in transformation_list]
     21     )
     22     return cdist(scanner_locations, scanner_locations, metric='cityblock')
     23 
     24 def solve_puzzle(input_string: str) -> int:
     25     """Return the numeric solution to the puzzle"""
     26     return find_beacon_distance(
     27         create_beacon_network(parse_input(input_string))[1]
     28     ).max()
     29 
     30 def main() -> None:
     31     """Run when the file is called as a script"""
     32     assert solve_puzzle(EXAMPLE_INPUT) == 3621
     33     print("Maximum distance between scanners:",
     34           solve_puzzle(get_puzzle_input(19)))
     35 
     36 if __name__ == "__main__":
     37     main()