<jupyter_start><jupyter_text>AoC Day 5<NAME>5 December 2021 Prompt--- Day 5: Hydrothermal Venture ---You come across a field of hydrothermal vents on the ocean floor! These vents constantly produce large, opaque clouds, so it would be best to avoid them if possible.They tend to form in lines; the submarine helpfully produces a list of nearby lines of vents (your puzzle input) for you to review. For example:```0,9 -> 5,98,0 -> 0,89,4 -> 3,42,2 -> 2,17,0 -> 7,46,4 -> 2,00,9 -> 2,93,4 -> 1,40,0 -> 8,85,5 -> 8,2```Each line of vents is given as a line segment in the format x1,y1 -> x2,y2 where x1,y1 are the coordinates of one end the line segment and x2,y2 are the coordinates of the other end. These line segments include the points at both ends. In other words:An entry like 1,1 -> 1,3 covers points 1,1, 1,2, and 1,3.An entry like 9,7 -> 7,7 covers points 9,7, 8,7, and 7,7.For now, only consider horizontal and vertical lines: lines where either x1 = x2 or y1 = y2.So, the horizontal and vertical lines from the above list would produce the following diagram:```.......1....1....1....1....1.........1...112111211........................................222111....```In this diagram, the top left corner is 0,0 and the bottom right corner is 9,9. Each position is shown as the number of lines which cover that point or . if no line covers that point. The top-left pair of 1s, for example, comes from 2,2 -> 2,1; the very bottom row is formed by the overlapping lines 0,9 -> 5,9 and 0,9 -> 2,9.To avoid the most dangerous areas, you need to determine the number of points where at least two lines overlap. In the above example, this is anywhere in the diagram with a 2 or larger - a total of 5 points.Consider only horizontal and vertical lines. At how many points do at least two lines overlap?<jupyter_code>import copy
# get input

with open("inputs/day5.txt") as file:
    inputs = file.read().splitlines()<jupyter_output><empty_output><jupyter_text>create functions<jupyter_code>def get_coordinates(inputs, drop_diagonals = True):
    coordinates = []
    for i in inputs:
        coords = i.split(" -> ")
        coords1 = tuple([int(x) for x in coords[0].split(",")])
        coords2 = tuple([int(x) for x in coords[1].split(",")])
        if drop_diagonals:
            x1, y1 = coords1
            x2, y2 = coords2
            if (x1 == x2) or (y1 == y2):
                coordinates.append((coords1, coords2))
        else:
            coordinates.append((coords1, coords2))
    return coordinates
def create_grid(x, y):
    grid = [[0] * x for i in range(y)]
    return grid
def mark_grid_horizontal_vertical(grid_input, coordinates):
    grid = copy.deepcopy(grid_input)
    for c in coordinates:
        (x1,y1),(x2,y2) = c
        # draw horizontal line
        if y1 == y2:
            if x2 > x1:
                for i in range(x1,x2+1):
                    grid[y1][i] += 1
            else:
                for i in range(x2,x1+1):
                    grid[y1][i] += 1  
        # draw vertical line
        elif x1 == x2:
            if y2 > y1:
                for i in range(y1,y2+1):
                    grid[i][x1] += 1
            else:
                for i in range(y2,y1+1):
                    grid[i][x1] += 1   
    return grid
def count_dangerous_areas(grid, threshold=2):
    danger_count = 0
    for row in grid:
        for spot in row:
            if spot >= threshold:
                danger_count += 1
    return danger_count<jupyter_output><empty_output><jupyter_text>Part 1 Tests<jupyter_code>test_inputs = ["0,9 -> 5,9", "8,0 -> 0,8", "9,4 -> 3,4", "2,2 -> 2,1", "7,0 -> 7,4", "6,4 -> 2,0", "0,9 -> 2,9", "3,4 -> 1,4", "0,0 -> 8,8", "5,5 -> 8,2"]
test_coordinates = get_coordinates(test_inputs, drop_diagonals = True)
test_grid = create_grid(10,10)
marked_test_grid = mark_grid_horizontal_vertical(test_grid, test_coordinates)
print(test_coordinates)
display(marked_test_grid)<jupyter_output>[((0, 9), (5, 9)), ((9, 4), (3, 4)), ((2, 2), (2, 1)), ((7, 0), (7, 4)), ((0, 9), (2, 9)), ((3, 4), (1, 4))]<jupyter_text>```.......1....1....1....1....1.........1...112111211........................................222111....```<jupyter_code>count_dangerous_areas(marked_test_grid)<jupyter_output><empty_output><jupyter_text>Part 1 with real inputs<jupyter_code>coordinates_part1 = get_coordinates(inputs, drop_diagonals = True)
all_x = []
all_y = []
for coord in coordinates_part1:
    c1, c2 = coord
    x1, y1 = c1
    x2, y2 = c2
    all_x.append(x1)
    all_x.append(x2)
    all_y.append(y1)
    all_y.append(y2)
x_min = min(all_x)
x_max = max(all_x)
y_min = min(all_y)
y_max = max(all_y)
print(x_min, x_max)
print(y_min, y_max)
# just checking, but I want my grid to be 0 x 999
grid = create_grid(1000,1000)
marked_grid_part1 = mark_grid_horizontal_vertical(grid, coordinates_part1)
count_dangerous_areas(marked_grid_part1)<jupyter_output><empty_output><jupyter_text>Part 1 Solution8111 - CORRECT!I made a very silly mistake at first and got an answer of 0 because I kept a test limiter of first 10 elements in a function... d'oh --- Part Two ---Unfortunately, considering only horizontal and vertical lines doesn't give you the full picture; you need to also consider diagonal lines.Because of the limits of the hydrothermal vent mapping system, the lines in your list will only ever be horizontal, vertical, or a diagonal line at exactly 45 degrees. In other words:An entry like 1,1 -> 3,3 covers points 1,1, 2,2, and 3,3.An entry like 9,7 -> 7,9 covers points 9,7, 8,8, and 7,9.```0,9 -> 5,98,0 -> 0,89,4 -> 3,42,2 -> 2,17,0 -> 7,46,4 -> 2,00,9 -> 2,93,4 -> 1,40,0 -> 8,85,5 -> 8,2```Considering all lines from the above example would now produce the following diagram:```1.1....11..111...2....2.1.111....1.2.2...112313211...1.2......1...1....1.....1..1.......1.222111....```You still need to determine the number of points where at least two lines overlap. In the above example, this is still anywhere in the diagram with a 2 or larger - now a total of 12 points.Consider all of the lines. At how many points do at least two lines overlap?<jupyter_code>def mark_grid_diagonal(grid_input, coordinates):
    grid = copy.deepcopy(grid_input)
    for c in coordinates:
        (x1,y1),(x2,y2) = c
        # draw diagonal line
        if (y1 != y2) and (x1 != x2):
            # if right down direction
            if (x2 > x1) and (y2 > y1):
                distance = (x2 - x1) + 1
                for i in range(distance):
                    grid[y1+i][x1+i] += 1
            # if left down direction
            elif (x1 > x2) and (y2 > y1):
                distance = (x1 - x2) + 1
                for i in range(distance):
                    grid[y1+i][x1-i] += 1
            # if right up direction
            elif (x2 > x1) and (y1 > y2):
                distance = (x2 - x1) + 1
                for i in range(distance):
                    grid[y1-i][x1+i] += 1
            # if left up direction
            elif (x1 > x2) and (y1 > y2):
                distance = (x1 - x2) + 1
                for i in range(distance):
                    grid[y1-i][x1-i] += 1
    return grid<jupyter_output><empty_output><jupyter_text>part 2 tests<jupyter_code>test_coordinates = get_coordinates(test_inputs, drop_diagonals = False)
marked_test_grid1 = mark_grid_horizontal_vertical(test_grid, test_coordinates)
marked_test_grid2 = mark_grid_diagonal(marked_test_grid1, test_coordinates)
display(marked_test_grid2)<jupyter_output><empty_output><jupyter_text>```1.1....11..111...2....2.1.111....1.2.2...112313211...1.2......1...1....1.....1..1.......1.222111....```  part 2 real thing<jupyter_code>coordinates_part2 = get_coordinates(inputs, drop_diagonals = False)
marked_grid1_part2 = mark_grid_horizontal_vertical(grid, coordinates_part2)
marked_grid2_part2 = mark_grid_diagonal(marked_grid1_part2, coordinates_part2)
count_dangerous_areas(marked_grid2_part2)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14445985125425437, 'fraction_numerical': 0.06428841547964201, 'mean_word_length': 2.6816705336426914, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 83, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '657601', 'license': 'MIT', 'n_tokens_mistral': 2910, 'n_tokens_neox': 2652, 'n_words': 840, 'path': '2021/Day_05.ipynb', 'repo_name': 'jenna-jordan/Advent_of_Code', 'size': '7939'}