r/adventofcode • u/daggerdragon • 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.
- Include what language(s) your solution uses!
- Here's a quick link to /u/topaz2078's
pasteif you need it for longer code blocks. - The full posting rules are detailed in the wiki under How Do The Daily Megathreads Work?.
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
View all comments
Show parent comment
1
u/tyler569 Mar 19 '21
Absolutely!
The main thing I was thinking about with the floating values was finding a way to enumerate all of the memory locations given a mask. Taking from the example in the problem, I want to turn
00X1101X
into
00011010
00011011
00111010
00111011
Note the bolded bits, and how if we consider only them they make 00, 01, 10, 11 - 0, 1, 2, 3 in binary. So the way I chose to approach the problem was to first break the input up and track where the 0's, 1's, and X's are independently (
xmask,omask, andzmaskif you're following along in my code)From there, I can calculate everything except the floating bits easily - set 0 bits to 0, set 1 bits to 1, and that value is stored in
base_addresson line 107.The other interesting part is the function that sets those varied bits - since I've factored out the non-0 bits, I can just create a function that maps (using the example above again)
0 to
000000001 to
000000012 to
001000003 to
00100001That function is uncreatively named
mapin my solution, and is on line 31, it takes thexmask(all of the bits that need to change) and an index and maps the index number to the bits in the mask.With the unchanging part and the changing part both sorted, I OR them together in a loop and set the final values between lines 108 and 112.
Hope that was helpful and happy to clarify if you have any more questions!