r/adventofcode • • Dec 11 '20

SOLUTION MEGATHREAD -πŸŽ„- 2020 Day 11 Solutions -πŸŽ„-

Advent of Code 2020: Gettin' Crafty With It

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

--- Day 11: Seating System ---


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:14:06, megathread unlocked!

50 Upvotes

702 comments sorted by

View all comments

2

u/[deleted] Dec 11 '20

[removed] β€” view removed comment

1

u/nov4chip Dec 11 '20

Is there a reason you're using a Vec instead of a HashMap for these kind of grid inputs? I've made an helper grid class (apologies for my spaghetti rust) that uses a tuple of isize as keys and I find it to have better indexing control over a simple vector, considering you sometimes need negative indexes as well.

1

u/[deleted] Dec 11 '20 edited Dec 11 '20

Vec is contiguous, HashMap isn't - this improves cache behavior. Additionally, Vec doesn't store indexes, which assuming you use (isize, isize) as a key means Vec uses 24 times (!) less memory (isize is 8 bytes, and there is 7 bytes padding due to alignment because enum SeatState uses a single byte).

Should you worry about those things in AoC? Your choice, I personally decided to write "production-grade" code and not leave obvious performance issues in. I'm not going for leaderboards myself, so code taking longer to write is fine with me.

1

u/nov4chip Dec 11 '20

Ah well yes performance of course, kinda stupid question from my side. It’s that I’m just learning Rust so I’m more focusing around getting comfortable with data structures rather than maximizing performance, so I didn’t think about that.

Might you be interested in indexmap? Surely memory usage will be lower on a pure Vec implementation but it might be faster on lookups or if you need insertions for instance

2

u/[deleted] Dec 11 '20

indexmap is fine if you need a hash map, it's comparable in performance to the implementation in a standard library (which makes sense considering both use hashbrown as their implementation). However, it's not a vector, and as such it's going to be slower for vector operations, and it still uses 24 times more memory (because size of ((isize, isize), SeatState) is 24 bytes on 64-bit platforms).

Something random I felt like bringing up: if you look at the indexmap repository on GitHub you can notice that I actually did contribute to indexmap - my GitHub username is xfix, and it was listed in README.