3 min read

Advent of Code 2020 Day 14

My biggest problem with part two was not realizing that whereas in part one, we were changing the value being stored, but in part two, we're storing the pre-defined VALUE to multiple address locations based on the criteria.
Advent of Code 2020 Day 14

To say today's puzzle wasn't completely obliterated my inability to code or anything else, but by my ability to actually have time to do it. I was able to get part one completed fairly quickly (though Java didn't really seem to do the binary to decimal conversions as easily as something like python would do them, because String -> char[] -> replacement -> String -> long is kind of annoying to write in Java compared to what I'd likely do in python.

In java, my workflow is as follows (to make one bit change in a String) from long to bit change to long in this problem is:

long value = 1534;
String binary = Long.toBinaryString(value);
//10111111110
final int length = 36 - binary.length(); 
//needs to be final to create array
char[] str = new char[length];
//"                         "
String fullBinary = new String(str).replace('\0','0') + binary;
//0000000000000000000000000010111111110
char[] binarySection = fullBinary.toCharArray();
//{'0', '0', '0', '0',...,'1', '1', '0'}
binarySection[2] = '1';
//{'0', '0', '1', '0',...,'1', '1', '0'}
long finalValue = Long.parseLong(new String(binarySection), 2);

This can be compressed a bit as follows:

long value = 1534;
String binary = Long.toBinaryString(value);
final int length = 36 - binary.length();
char[] str = new char[length];
char[] fullBinary = (new String(str).replace('\0','0') + binary).toCharArray();
fullBinary[2] = '1';
long finalValue = Long.parseLong(new String(fullBinary), 2);

In reality, it's okay to do, but for this problem, it really just seemed to need more focused work to do what I wanted.  Now, I could've put it all into a method, but since I was using it only once within a method anyway, I didn't see much value in that to begin with.  I had to take time off between part one and part to in order to actually attend to work things (I know, how dare I do my job, right).

My biggest problem with part two was not realizing that whereas in part one, we were changing the value being stored, but in part two, we're storing the pre-defined VALUE to multiple address locations based on the criteria.  Part two starts out reading as follows:

For some reason, the sea port's computer system still can't communicate with your ferry's docking program. It must be using version 2 of the decoder chip!

A version 2 decoder chip doesn't modify the values being written at all. Instead, it acts as a memory address decoder. Immediately before a value is written to memory, each bit in the bitmask modifies the corresponding bit of the destination memory address in the following way:

  • If the bitmask bit is 0, the corresponding memory address bit is unchanged.
  • If the bitmask bit is 1, the corresponding memory address bit is overwritten with 1.
  • If the bitmask bit is X, the corresponding memory address bit is floating.

As the example continued further down talked about how the sum of the values was 208, but I never saw an example of where the value was actually stored. It took quite a bit of re-reading the problem statement to realize that in part one, I was modifying the value stored in the defined memory address, but in part two, I was modifying ONLY the memory address of where to store the unmodified value. Once I figured that out, much of my code could be shared, but I dove again into recursion for the expanding out of the binary value with floating bits to the final available values. All-in-all, I enjoyed today's puzzle, mostly because it was using a lot of the previously learned systems ( Long.parseLong("0101", 2) for binary, Long.toBinaryString(25L) to get the binary string, using streams to do index checks by leveraging things like anyMatch in the streams).  I think my initial hope for Advent of Code in 2020 was to better learn streams, and I'd say I've succeeded in learning them even more now.

If you want to join my Advent of Code leaderboard, feel free to join with the code: 699615-aae0e8af.