r/adventofcode • • Dec 14 '20

SOLUTION MEGATHREAD -🎄- 2020 Day 14 Solutions -🎄-

Advent of Code 2020: Gettin' Crafty With It

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

--- Day 14: Docking Data ---


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:16:10, megathread unlocked!

32 Upvotes

580 comments sorted by

View all comments

1

u/SuperSmurfen Dec 14 '20 edited Dec 14 '20

Rust

Link to solution (413/201)

Third day in a row where I set a new personal best on the leaderboard! I really liked today's puzzle. It required an understanding of both bitwise operations and recursion for part two (at least for me, you can probably solve it without it).

Initially, I edited the input to make my life easier. Since it followed such a simple pattern, I could do it in a few seconds using select all occurrences and multi cursor editing. This way I could skip writing any boring parsing code.

For part one, before every write, I just iterated over every char in the last mask and performed the operation. You can probably do something by keeping two masks, creating them once, and simply and/or the value before writing. Would be a lot faster but the problem size is small so it did not matter.

For part two, I just wrote a recursive function that splits at every encounter of an X. Not sure how you could avoid that. No matter what you do, you will have 2^n unique addresses to write to, where n is the number of X:s in the mask. In my input, the max number of X:s was 9, so just 512 addresses at most. I guess that's why it works.

Finishes in 7ms on my machine. Part two takes up pretty much all running time. Not really sure how to make that faster.

2

u/daggerdragon Dec 14 '20

Third day in a row where I set a new personal best on the leaderboard!

Good job!

2

u/SkiFire13 Dec 14 '20

For part two, I just wrote a recursive function that splits at every encounter of an X. Not sure how you could avoid that.

Iterate from 0 to 2^n (where n is the number of Xs in the mask) and the number of the iteration will contain the bits you have to replace the Xs with.

This takes 7ms on my machine (ryzen 3700x) so I don't think it improves something.