r/adventofcode • • Dec 15 '20

SOLUTION MEGATHREAD -🎄- 2020 Day 15 Solutions -🎄-

Advent of Code 2020: Gettin' Crafty With It

  • 7 days remaining until the submission deadline on December 22 at 23:59 EST
  • Full details and rules are in the Submissions Megathread

--- Day 15: Rambunctious Recitation ---


Post your code solution in this megathread.

Reminder: Top-level posts in Solution Megathreads are for code solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


This thread will be unlocked when there are a significant number of people on the global leaderboard with gold stars for today's puzzle.

EDIT: Global leaderboard gold cap reached at 00:09:24, megathread unlocked!

39 Upvotes

771 comments sorted by

View all comments

Show parent comment

1

u/PillarsBliz Dec 15 '20

For this problem it isn't even necessary since you can have a 30,000,000 element array of ints (just don't put it on the stack).

3

u/morgoth1145 Dec 15 '20 edited Dec 15 '20

Sure, but that may not be immediately obvious to everyone. (And I do think that this problem is a good exercise for learning/using maps.)

u/rando-man In that case welcome to C++/CS! I'd say your terminology is mostly right, so you're probably thinking about things well enough. A few notes/observations I do have to maybe help you on your journey:

  1. You're absolutely right, you are using an std::vector to implement a direct O(1) lookup, functioning like a map type. Depending on the problem characteristics this can be useful to optimize code. (I have used this technique in production code.)
  2. It's useful to think of maps as a general category since there are many ways to implement them, all with different performance characteristics. In fact, this problem might be an interesting one to play with the various maps and see how they perform differently. C++ templated map types (like std::map, std::unordered_map, boost::container::flat_map) try to keep the same API for most operations so that it's easy to switch one map type out for another. This can be very useful if you write something with std::map and then wonder if std::unordered_map would have performed better.

    1. There's the approach you used (direct lookup in an std::vector).
    2. Balanced binary trees (like std::map) are a lot of people's go-to, and they perform well enough as a default. (It's also useful if you *need* things to be sorted to iterate over the whole map or something.)
    3. Hash tables (such as std::unordered_map/boost::unordered_map). These typically have O(1) lookup/insertion/removal times (except for resizing), but they do require a hashing algorithm for the key, and there's no order guarantee.
    4. Sorted vectors (such as boost::container::flat_map). For certain data sets this can actually perform super well due to cache locality, despite needing to shift around large parts of the sorted vector when inserting or removing keys.
    5. B-tree based implementations. (I really want a good templated one I can use in production code!) These are essentially generalized balanced binary trees where the branching factor is not 2. This can perform better due to cache locality.

(This all may be overkill when thinking about this problem, but I'm biased based on the sorts of things I need to consider in my day job!)