46
SOLUTION MEGATHREAD-🎄- 2021 Day 16 Solutions -🎄-(self.adventofcode)
submitted 4 years, 1 month ago* (edited 27 minutes after) by daggerdragon to /r/adventofcode (134.8k)
NEW AND NOTEWORTHY
DO NOT POST SPOILERS IN THREAD TITLES!
- The only exception is for `Help...
since 4 years, 1 month ago
5 of 5
Tip Reveddit Real-Time can notify you when your content is removed.
your account history
Tip Check if your account has any removed comments.
view my removed comments you are viewing a single comment's thread.
view all comments


Pure Java
Recursive function with a global bit index. I used BiFunctions to list the mathematical operations so I didn't have to use an ugly switch statement. Be aware that this type of code is not thread-safe, I was going for conciseness.
For part 2 I got all the examples correct, but got a 'too low' answer on the real data. It turned out that you shouldn't use == on two Integer objects. I thought they would be auto-unboxed but I guess not? Anyway, lesson learned. Thank goodness IntelliJ warned me of the issue because I would have been searching forever.
https://github.com/arjanIng/advent2021/blob/main/src/advent/Day16.java
I'm curious. I am not well versed in all changes betwee modern Java versions, but for me,
... returns 'true', then 'false' (as expected) for both jdk 11.0.13 and 17.0.1.
Although IntelliJ does indeed warn about the == and !=, it still works as you would expect for integers apparently.
Which version are you on? Maybe it's old behaviour?
Maybe more experienced Javans could elaborate.
Try Integer.valueOf(99999999)
AFAIR, Java preallocates boxed small numbers, so Integer.valueOf(1) indeed always returns the same result, but for bigger numbers it allocates a new one instead. As a side effect, by some magic tricks it's possible to change the value of an integer, and Integer.valueOf(1) will now return 2.
So in general case, it's not safe to compare them with ==
I tried it, and you're right. It works only from -128 to 127 (so, signed byte).
That's quite the gotcha. Good guy IntelliJ I guess :)
Apparently it only works with small Integers due to caching, which is why the examples worked.
https://javarevisited.blogspot.com/2010/10/what-is-problem-while-using-in.html#axzz7FCcSsdE4
Oh, and pro-tip: learn to avoid using System.exit() unless you absolutely have no other choice. You can crash a server with that.