Unable to load image

Day 12

Post code, nerds.

14
Jump in the discussion.

No email address required.

For this one, I started by treated the elevation as a graph. A location was said to be connected to another location in the graph if the other location could be reached from the current location. Then, I would start at the starting location and follow the graph until I reached the end. Along the way, I would keep track of the distance from the start - this doubled as a way to prevent me from having to deal with loops.

I don't really know much about traditional pathfinding algorithims but mine worked fine, so đź’…. IDK if "wavefront" is what people in the feild call what I did, but it makes sense to me to imagine a wave crashing against the rocks...

import numpy as np
lines = []
with open("12.input", "r") as f:
    lines = f.readlines()

START_INDICATOR = -1
END_INDICATOR = 900

landscape = np.array([['abcdefghijklmnopqrstuvwxyz'.index(letter) if letter not in ['S', 'E'] else (START_INDICATOR if letter == 'S' else END_INDICATOR) for letter in line.strip()] for line in lines])

class LocationNode:
    def __init__(self, coordinates: tuple[int, int], connecting_locations: list[tuple[int, int]]) -> None:
        self.coordinates = coordinates
        self.connecting_locations = connecting_locations

INDICATOR_TO_VALUE = {START_INDICATOR : 0, END_INDICATOR: 25} | {i:i for i in range(26)}

locations  = np.empty(shape=landscape.shape, dtype=LocationNode)
for x in range(landscape.shape[0]):
    for y in range(landscape.shape[1]):
        directions = []
        if (x != 0):                    directions.append((INDICATOR_TO_VALUE[landscape[x-1,y]], (x-1, y)))
        if (x != landscape.shape[0]-1): directions.append((INDICATOR_TO_VALUE[landscape[x+1,y]], (x+1, y)))
        if (y != 0):                    directions.append((INDICATOR_TO_VALUE[landscape[x,y-1]], (x, y-1)))
        if (y != landscape.shape[1]-1): directions.append((INDICATOR_TO_VALUE[landscape[x,y+1]], (x, y+1)))
        locations[x, y] = LocationNode((x, y), [a[1] for a in directions if a[0]-INDICATOR_TO_VALUE[landscape[x,y]]<=1])

starting_location_array = np.where(landscape == START_INDICATOR)
starting_location = (starting_location_array[0][0], starting_location_array[1][0])
ending_location_array = np.where(landscape == END_INDICATOR)
ending_location = (ending_location_array[0][0], ending_location_array[1][0])

def get_distances(start: tuple[int,int]):
    distances = np.full(shape=landscape.shape, fill_value=-1, dtype=int)
    wavefront = [start]
    distance = 0
    while len(wavefront) != 0:
        new_wavefront = []
        for location in wavefront:
            distances[location] = distance
            new_wavefront = new_wavefront + [a for a in locations[location].connecting_locations if distances[a] == -1]
        distance+=1
        wavefront = set(new_wavefront)
    
    return distances

print(f"PART 1: {get_distances(starting_location)[ending_location]}")
print("Finding bests...")
a_locations = np.where(landscape == 0)

distances = []
for i in range(len(a_locations[0])+1):
    if i != len(a_locations[0]):
        x = a_locations[0][i]
        y = a_locations[1][i]
    else:
        x = starting_location[0]
        y = starting_location[1]
    location_node : LocationNode = locations[x,y]
    result = get_distances((x,y))[ending_location]
    if result != -1:
        distances.append(result)
distances.sort()
print(F'PART 2: {distances[0]}')
Jump in the discussion.

No email address required.

This is a really long way of saying you don't frick.

Jump in the discussion.

No email address required.

Link copied to clipboard
Action successful!
Error, please refresh the page and try again.