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 bbe48acdc50b4999423ed00bd15b2c0d23d9e1cf
parent 40ae9ac2d3f794b51685a439516e4b220b8b703e
Author: Eamon Caddigan <eamon.caddigan@gmail.com>
Date:   Sun, 19 Dec 2021 20:38:05 -0500

Solution to day 19, part 2

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

diff --git a/day19_part2.py b/day19_part2.py @@ -0,0 +1,37 @@ +#!/usr/bin/env python +"""Advent of Code 2021, day 19 (part 2): Beacon Scanner +Given positions (but not consistent frame of reference), find the overlap +between a set of beacons detected by several scanners""" + +# One of those plesant situations where my solution to part 1 solves part 2 + +from typing import List +import numpy as np +from scipy.spatial.distance import cdist +from day19_part1 import (EXAMPLE_INPUT, + parse_input, + create_beacon_network) +from utils import get_puzzle_input + +def find_beacon_distance(transformation_list: List[np.ndarray]) -> np.ndarray: + """Return a distance matrix of the beacons by pulling out the translation + component of the transformation matrices""" + scanner_locations = np.array( + [x[3, 0:3] for x in transformation_list] + ) + return cdist(scanner_locations, scanner_locations, metric='cityblock') + +def solve_puzzle(input_string: str) -> int: + """Return the numeric solution to the puzzle""" + return find_beacon_distance( + create_beacon_network(parse_input(input_string))[1] + ).max() + +def main() -> None: + """Run when the file is called as a script""" + assert solve_puzzle(EXAMPLE_INPUT) == 3621 + print("Maximum distance between scanners:", + solve_puzzle(get_puzzle_input(19))) + +if __name__ == "__main__": + main()