<jupyter_start><jupyter_text>--- Day 6: Chronal Coordinates ---The device on your wrist beeps several times, and once again you feel like you're falling."Situation critical," the device announces. "Destination indeterminate. Chronal interference detected. Please specify new target coordinates."The device then produces a list of coordinates (your puzzle input). Are they places it thinks are safe or dangerous? It recommends you check manual page 729. The Elves did not give you a manual.If they're dangerous, maybe you can minimize the danger by finding the coordinate that gives the largest distance from the other points.Using only the Manhattan distance, determine the area around each coordinate by counting the number of integer X,Y locations that are closest to that coordinate (and aren't tied in distance to any other coordinate).Your goal is to find the size of the largest area that isn't infinite. For example, consider the following list of coordinates:1, 11, 68, 33, 45, 58, 9If we name these coordinates A through F, we can draw them on a grid, putting 0,0 at the top left:...........A..........................C....D...........E.....B....................................F.This view is partial - the actual grid extends infinitely in all directions. Using the Manhattan distance, each location's closest coordinate can be determined, shown here in lowercase:aaaaa.ccccaAaaa.ccccaaaddeccccaadddeccCc..dDdeecccbb.deEeeccbBb.eeee..bbb.eeefffbbb.eeffffbbb.ffffFfLocations shown as . are equally far from two or more coordinates, and so they don't count as being closest to any.In this example, the areas of coordinates A, B, C, and F are infinite - while not shown here, their areas extend forever outside the visible grid. However, the areas of coordinates D and E are finite: D is closest to 9 locations, and E is closest to 17 (both including the coordinate's location itself). Therefore, in this example, the size of the largest area is 17.What is the size of the largest area that isn't infinite?<jupyter_code>example_input = """1, 1
1, 6
8, 3
3, 4
5, 5
8, 9"""

with open('input/day06.txt', 'r') as f:
    actual_input = f.read()
    
actual_input = actual_input.strip()
    
print(actual_input[0:10])
def get_coords(input):
    co = []
    for row in input.split('\n'):
        points = row.split(',')
        acoord = (int(points[0].strip()), int(points[1].strip()))
        co.append(acoord)
        
    return co

print(get_coords(example_input))
print(get_coords(actual_input))
import numpy as np
from scipy.spatial.distance import cityblock

def get_dimensions(input):
    coords = get_coords(input)
    max_x = np.max([x[0] for x in coords])
    max_y = np.max([x[1] for x in coords])
    return max_x, max_y

def get_closest(point, coords):
    min_coord = [coords[0]]
    min_distance = abs(cityblock(min_coord, point))
    for acoord in coords[1:]:
        #print(point, acoord, min_coord, min_distance)
        if cityblock(acoord, point) < min_distance:
            min_distance = abs(cityblock(acoord, point))
            min_coord = [acoord]
        elif cityblock(acoord, point) == min_distance:
            min_coord.append(acoord)
            
    if len(min_coord) > 1:
        return None
    return min_coord


def get_grid(input):
    dimx, dimy = get_dimensions(input)
    coords = get_coords(input)
    #create grid
    grid = [[' '] * dimy for i in range(dimx)]
        
    #fill grid
    for x in range(dimx):
        for y in range(dimy):       
            #print(get_closest((x,y), coords))
            grid[x][y] = get_closest((x,y), coords)   
    return grid
    
print(get_grid(example_input))
    

from collections import Counter

def exclude_edges(grid):
    edge_list = []
    for x in range(len(grid)):
        for y in range(len(grid[0])):       
            if x == 0 or y == 0:
                edge_list.append(grid[x][y])
    return edge_list

def calculate_most(input):
    grid = get_grid(input)
    edges = exclude_edges(grid)
    counts = []
    
    for x in range(len(grid)):
        for y in range(len(grid[0])): 
            if grid[x][y] not in edges and grid[x][y] is not None:
                #count it
                counts.append(str(grid[x][y]))
                
    #print(counts)
    counter = Counter(counts)
    return counter.most_common()[0][1]
    
print(calculate_most(example_input))
print(calculate_most(actual_input))<jupyter_output>17
5365<jupyter_text>--- Part Two ---On the other hand, if the coordinates are safe, maybe the best you can do is try to find a region near as many coordinates as possible.For example, suppose you want the sum of the Manhattan distance to all of the coordinates to be less than 32. For each location, add up the distances to all of the given coordinates; if the total of those distances is less than 32, that location is within the desired region. Using the same coordinates as above, the resulting region looks like this:...........A.......................C...D.....E....B.................................F.In particular, consider the highlighted location 4,3 located at the top middle of the region. Its calculation is as follows, where abs() is the absolute value function:Distance to coordinate A: abs(4-1) + abs(3-1) =  5Distance to coordinate B: abs(4-1) + abs(3-6) =  6Distance to coordinate C: abs(4-8) + abs(3-3) =  4Distance to coordinate D: abs(4-3) + abs(3-4) =  2Distance to coordinate E: abs(4-5) + abs(3-5) =  3Distance to coordinate F: abs(4-8) + abs(3-9) = 10Total distance: 5 + 6 + 4 + 2 + 3 + 10 = 30Because the total distance to all coordinates (30) is less than 32, the location is within the region.This region, which also includes coordinates D and E, has a total size of 16.Your actual region will need to be much larger than this example, though, instead including all locations with a total distance of less than 10000.What is the size of the region containing all locations which have a total distance to all given coordinates of less than 10000?<jupyter_code>def get_closest2(point, coords):
    min_coord = [coords[0]]
    min_distance = abs(cityblock(min_coord, point))
    total_distance = min_distance
    for acoord in coords[1:]:
        adistance = abs(cityblock(acoord, point))
        total_distance = total_distance + adistance
            
    return total_distance
def get_grid2(input):
    dimx, dimy = get_dimensions(input)
    coords = get_coords(input)
    #create grid
    grid = [[' '] * dimy for i in range(dimx)]
        
    #fill grid
    for x in range(dimx):
        for y in range(dimy):       
            #print(get_closest((x,y), coords))
            grid[x][y] = get_closest2((x,y), coords)   
    return grid

def calculate_most2(input, threshold=32):
    grid = get_grid2(input)
    counts = 0
    
    for x in range(len(grid)):
        for y in range(len(grid[0])): 
            if grid[x][y] < threshold:
                counts = counts + 1               

    return counts
    
print(calculate_most2(example_input, 32))
print(calculate_most2(actual_input, 10000))<jupyter_output>16
42513
{'fraction_non_alphanumeric': 0.11243105642766228, 'fraction_numerical': 0.021071984160656202, 'mean_word_length': 2.9708029197080292, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '498422', 'license': 'MIT', 'n_tokens_mistral': 2167, 'n_tokens_neox': 2064, 'n_words': 806, 'path': 'day06.ipynb', 'repo_name': 'drkatnz/adventofcode2018', 'size': '7071'}