I guess that would be no stranger than crab submarines, a single crab shuffling hundreds of thousands of cups around, or bugs spreading in folded space
(that is 2021 day 7, 2020 day 23 part 2, 2019 day 24 part 2)
to be fair to wastl, dijkstra is... a pretty commonly used and well known algorithm; it's not at all niche or obscure and it's like, probably the most versatile
and of course i need to marshal my complexes back into tuples because i have to encode the pairs as tuples because i probably am misunderstanding something about heapq
Dijkstra's Algorithm is a pathfinding algorithm that works for weighted graphs, both directed and not, and solves the Single Source Shortest Path (which gives the length of the shortest path to every node on the graph from a static starting point), which means it can be used to find the shortest path from A to B
the idea behind DA is very similar to BFS - at its core, you maintain a priority queue of (node, cost) points (bubbler said there's a niche data structure that's more optimal but just use priority queue)
at each step, just like with BFS where you pop from your queue, with DA you just pop from your pqueue, getting the smallest item ordered by cost
essentially, at each step you go to the next cheapest point that you haven't visited
and then you just look at each neighbor and add it to the pqueue if it's not visited, and eventually you either empty your pqueue and have visited every node so you have the SSSP, or you arrive at your destination point
note that whereas with BFS it's optimal to add nodes to the visited set when you queue them, do not do that with DA, that will give you suboptimal results
you need to mark nodes as visited when you pop them off the PQ, and you should perform a visited check both when you pop and before you push
idk if javascript has a pq/heap lol. python has heapq
I was thinking something like keep a linked list of the things where they're always in the right order, and to insert I just follow the list until I find the right spot to insert an item
Since I'd imagine most of it will be Infinitys for a while
bellman-ford is slower than dijkstra (but still fast enough for this) but its main use is that it works on negative edge weight graphs and can detect and report negative cycles
i didn;t update anything once it was on the heap and it worked fine; not sure if that can produce incorrect results because i stopped thinking about this the moment i got an answer but yeah
It just makes me so mad that I didn't do well today because I don't have any formal CS education. It's like I'm being punished for figuring out what I know the hard way for the last six years.
Even if I was the best problem solver doing AoC, I just don't know some fairly common algorithm, so I lose
It feels like one of those job interviews where you just implement a list of algorithms on a whiteboard or something
Instead of actual problem solving
Since the people who know the answer already do great, and the people who need to think it through suffer
Like...I know there's some amount of expectation that the people who will be competitive know various algorithms, but this adds nothing creative on top of that
I actually know of a competitive programming site where the algorithms you need to learn at each level are very nicely laid out with example problems, but it's in Korean :/
i'll continue to promote dmoj.ca; not affiliated with them in any way just has been really helpful for me cuz it has all sorts of CP problems from contests all over the world and other assorted problems
yeah having heapq feels like a huge advantage and javascript doesn't seem the best equipped for doing dijkstra. bellman ford should be fine tho but still :/
It's like the inventor of JS heard about the concept of OOP the day before it released, but only overheard a small snippet of the conversation, and pieced the rest together that night while incredibly sleep deprived
i'd be happy to explain what heaps are and how they work but this is a fine (i think) implementation of heaps (which are the underlying DS used for most priority queues)
oh another thing you could do for the bellman ford implementation