r/adventofcode • • 3d ago

Visualization [2024 Day 18] [Kotlin] Visualization with GraphMateKT

Around 2 years ago, Advent of code inspired me to add vizualization capabillities to my GraphLibrary: GraphMateKT. Now, I'm starting a youtube series, showcaseing it's capabilities, by solving old AoC puzzles, and hopefully, new ones. I'm looking forward to AoC 2026! For now, here's a vizualization of a breath first search on a partially blocked and fully blocked grid!

38 Upvotes

4 comments sorted by

4

u/RazarTuk 3d ago

As it would happen, I actually just reimplemented my solution for 2024 Day 18 in Kotlin, albeit in the context of working through past years and hitting 2016 Day 13. The algorithm's overkill for basically anything except 2024 Day 18 Part 2, but I actually used a fancy one called LPA*, which is tailor-made for Part 2. Basically, it's an adaptation of A* which adds some metadata, so that if you add a new wall, it only has to update things beyond it

If you're interested, I can get you pseudocode, so you can add it to your library

1

u/Prestigious-Mood1711 3d ago

Oh wow! Thank you for providing that! I'm curious though, why did you decide to use LPA* instead of rerunning BFS on gradually more deleted grids?

1

u/RazarTuk 3d ago

I actually did just rerun Dijkstra for my actual solution. But it also felt like the sort of problem that would have a specialized algorithm already, so afterwards, I did more research and discovered LPA*. (And then used it on Day 20, despite it being total overkill, because it was already implemented)

3

u/RazarTuk 3d ago

Pseudocode, if you want to add LPA* to your library:

Each node has both an expected distance and a reported distance. The reported distance corresponds to the "regular" distance in Dijkstra's algorithm, while the expected distance is the lowest possible reported distance based on any of its neighbors. The score for the priority queue is the lower of these values. If you want to add a heuristic, compare min(expected, reported) + heuristic, but break ties with min(expected, reported), as opposed to how you'd break ties with the heuristic in normal A*. queue.add_or_update is shorthand for "Add this to the queue if it wasn't already there, or update its priority if it is". And instead of checking every node again, this will only re-process any nodes that are "behind" the new wall

initialize():
    for node in graph:
        node.expected <- infinity
        node.reported <- infinity
    start.expected <- 0
    queue.add(start)
    update()

# You can adapt this for removing an edge by running update_expected on both endpoints
add_wall(node):
    walls.add(node)
    for neighbor in node.neighbors:
        update_expected(neighbor)
    update()

update():
    until queue.empty || (end.expected == end.reported && end.score < queue.peek.score):
        curr <- queue.poll
        if curr.expected < curr.reported:
            set_reported(curr, curr.expected)
        else if curr.expected > curr.reported:
            set_reported(curr, infinity)

set_reported(node, reported):
    if node.reported != reported:
        node.reported <- reported
        if node.reported != node.expected:
            queue.add_or_update(node)
        for neighbor in node.neighbors:
            update_expected(neighbor)

update_expected(node):
    # node.expected is definitionally 0 for the starting node
    if node != start:
        min_distance <- infinity
        for neighbor in node.neighbors:
            if neighbor.reported + dist(node, neighbor) < min_distance:
                min_distance <- neighbor.reported + dist(node, neighbor)
        if node.expected != min_distance:
            node.expected <- min_distance
            queue.add_or_update(node)