AoC 2020 D23: Crab Cups
| Problem statement | Source code | Tags: Data structures
← Previous Back to AoC Index Next →
We need efficient deletion and insertion at arbitrary points in the list. but we don't need to know what cup is at an arbitrary position (we do want to know the position of an arbitrary cup though). A linked list is a natural fit for this, especially since it can also be made circular easily. Here is my single-linked list:
To find the destination cup efficiently, we can maintain a mapping from label to cup nodes, in addition to the linked list itself.
Now we can faithfully implement the rules. Code is self-explanatory. The only potentially messy part is the splicing of the picked cups out of the list and into their new position.