AoC 2020 D14: Docking Data
| Problem statement | Source code | Tags: Bitwise operations
← Previous Back to AoC Index Next →
Part 1
The mask can do one of three things to a bit: set to 0, set to 1, or leave unchanged. We know that "set to 0" is done with & 0 (whereas & 1 leaves unchanged), and "set to 1" is done with | 1 (whereas | 0 leaves unchanged). So we can maintain two bitmasks, one for setting to 0 and one for setting to 1; bits that are set by neither remain unchanged. I construct the masks by first reversing the mask string, so that the character index corresponds with the shift amount. ^ 1 is used to toggle bits in the masks.
Memory update is straightforward:
Part 2
In part 2, we still have three possible actions on each bit: set to 0, unchanged, or floating. Floating is nothing more than enumerating all possible combinations of 0 and 1 for those bits. I made a little generator function to enumerate these combinations:
Once we have concrete mask_0 and mask_1, the masking is done exactly like part 1, this time to memory addresses.