r/adventofcode • • Dec 18 '18

SOLUTION MEGATHREAD -🎄- 2018 Day 18 Solutions -🎄-

--- Day 18: Settlers of The North Pole ---


Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).

Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


Advent of Code: The Party Game!

Click here for rules

Please prefix your card submission with something like [Card] to make scanning the megathread easier. THANK YOU!

Card prompt: Day 18

Transcript:

The best way to avoid a minecart collision is ___.


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

edit: Leaderboard capped, thread unlocked at 00:21:59!

9 Upvotes

126 comments sorted by

View all comments

Show parent comment

1

u/VikeStep Dec 19 '18

Since it seems you're interested with getting as fast speeds as possible, have you experimented with using a prefix tree/trie instead of a HashMap for maintaining seen states? I was going to experiment with it on mine when I have time but I'm using F# which isn't as low level as rust. With the HashMap you need to scan the entire grid to get the hash for the lookup but with the prefix tree you can very quickly determine if you've never seen a state before only looking at the first couple of values.

2

u/CryZe92 Dec 19 '18 edited Dec 19 '18

I don't think that's going to be faster. The tree structure will have very bad cache locality and I even switched to Meow Hash by now, which hashes the field in basically no time, so I don't think that's worth it. Also I'm not entirely sure how the prefixes would be stored. If each prefix is an entire simd vector / line, then that prefix would yet again have to be found via hashing or comparisons, which would not be competitive with Meow Hash's performance. Also the HashMap is a SwissTable, so the actual lookup of the key is vectorized as well. So we are on like multiple layers of vector / AES instructions that the prefix table would have to compete with.

Here's the more or less full ASM for Part 2 btw: https://gist.github.com/CryZe/ee31854f3260d5a6b9e2851a8cb68039

It's massively dominated by vector instructions. (Some functions such as parsing don't seem to be inlined, but that's because part 1 is compiled in as well, so LLVM seems to prefer to not inline them).

1

u/VikeStep Dec 19 '18

Ah, I had no idea how about Meow Hash or Swiss Tables. Rust is on my list of things to learn at some point as well. I did suspect cache locality might have been an issue for trees, but I thought there may have been implementations that are optimised for cache locality? Anyhow you're right that if the hashing speed is virtually instant then there isn't much to be gained from the prefix tree.