<jupyter_start><jupyter_text>Day 5 - Part OneFor this problem, I took the NumPy approach again. I used a few familiar techniques, and also learned a few new ones including:* `np.add.at`* `np.pad`I solved Part One in a way that will help me out with Part Two, since I could tell that it would involve accounting for diagonals based on the description.This was a fun and engaging exercise - looking forward to Part Two!<jupyter_code>from aocd import get_data
import numpy as np
raw_data = get_data(day=5, year=2021)
# helper function to expand map in x or y direction with zeroes if needed
def expand_map(overlap_map, xs, ys):
    # get shape of current map
    num_rows, num_cols = overlap_map.shape
    curr_x_max, curr_y_max = max(xs), max(ys)
    
    # check/expand x direction
    if num_rows <= curr_x_max:
        x_padding = curr_x_max - num_rows + 1
        overlap_map = np.pad(overlap_map, ((0, x_padding), (0, 0)), 'constant')
    # check/expand y direction
    if num_cols <= curr_y_max:
        y_padding = curr_y_max - num_cols + 1
        overlap_map = np.pad(overlap_map, ((0, 0), (0, y_padding)), 'constant')
    
    return overlap_map
overlap_map = np.zeros((1,1)).astype('int')
vent_lines = raw_data.split('\n')
for line in vent_lines:
    pair1, pair2 = [l.strip() for l in line.split('->')]
    (x1, y1), (x2, y2) = map(int, pair1.split(',')), map(int, pair2.split(','))
    
    overlap_map = expand_map(overlap_map, (x1, x2), (y1, y2))
    
    # if x1 == x2 or y1 == y2, add to overlap map where necessary
    if x1 == x2:
        vent_line_range_y = range(y1, y2+1) if y1 < y2 else range(y1, y2-1, -1)
        np.add.at(overlap_map, (x1, vent_line_range_y), 1)
    elif y1 == y2:
        vent_line_range_x = range(x1, x2+1) if x1 < x2 else range(x1, x2-1, -1)
        np.add.at(overlap_map, (vent_line_range_x, y1), 1)
count = (overlap_map >= 2).sum()
count<jupyter_output><empty_output><jupyter_text>Day 5 - Part TwoMy implementation in Part One proved to be useful in solving part two. I just had to add one additional segment to the code to account for diagonals and handle negative range situations in both directions.<jupyter_code>from aocd import get_data
import numpy as np
raw_data = get_data(day=5, year=2021)
# helper function to expand map in x or y direction with zeroes if needed
def expand_map(overlap_map, xs, ys):
    # get shape of current map
    num_rows, num_cols = overlap_map.shape
    curr_x_max, curr_y_max = max(xs), max(ys)
    
    # check/expand x direction
    if num_rows <= curr_x_max:
        x_padding = curr_x_max - num_rows + 1
        overlap_map = np.pad(overlap_map, ((0, x_padding), (0, 0)), 'constant')
    # check/expand y direction
    if num_cols <= curr_y_max:
        y_padding = curr_y_max - num_cols + 1
        overlap_map = np.pad(overlap_map, ((0, 0), (0, y_padding)), 'constant')
    
    return overlap_map
overlap_map = np.zeros((1,1)).astype('int')
vent_lines = raw_data.split('\n')
for line in vent_lines:
    pair1, pair2 = [l.strip() for l in line.split('->')]
    (x1, y1), (x2, y2) = map(int, pair1.split(',')), map(int, pair2.split(','))
    
    overlap_map = expand_map(overlap_map, (x1, x2), (y1, y2))
    
    # if x1 == x2 or y1 == y2, add to overlap map where necessary
    if x1 == x2:
        vent_line_range_y = range(y1, y2+1) if y1 < y2 else range(y1, y2-1, -1)
        np.add.at(overlap_map, (x1, vent_line_range_y), 1)
    elif y1 == y2:
        vent_line_range_x = range(x1, x2+1) if x1 < x2 else range(x1, x2-1, -1)
        np.add.at(overlap_map, (vent_line_range_x, y1), 1)
    else:
        vent_line_range_x = range(x1, x2+1) if x1 < x2 else range(x1, x2-1, -1)
        vent_line_range_y = range(y1, y2+1) if y1 < y2 else range(y1, y2-1, -1)
        np.add.at(overlap_map, (vent_line_range_x, vent_line_range_y), 1)
count = (overlap_map >= 2).sum()
count<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12525720164609053, 'fraction_numerical': 0.03523662551440329, 'mean_word_length': 3.3795045045045047, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 13, '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': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '468223', 'license': 'MIT', 'n_tokens_mistral': 1502, 'n_tokens_neox': 1496, 'n_words': 441, 'path': '2021/Day05.ipynb', 'repo_name': 'landoncayia/advent-of-code-2021', 'size': '3888'}