I had considered this approach but the nature of AoC means it rewards real world time to solution rather than and kind of computation time, and a naive solution still runs fast enough that it more than makes up in time spent programming it. There's actually whole classes of problems which would be more interesting if the naive solution wasn't fast enough, for example even on day 7 (or was it 8?) naively exploring to…
A neat XOR trick
11–20 of 181 posts
Re: A neat XOR trick
#12Re: A neat XOR trick
#13I think the claim at the end is supposed to say `O(D)` running time rather than `O(N)` (or I can't see where `N` is defined). Does this technique catch letters duplicated more than once? In a Python 3 shell: >> 2 ^ 4 6 >> 2 ^ 4 ^ 2 4 >> 2 ^ 4 ^ 2 ^ 2 6 Yes I guess it does, because inputs are used up in order to turn bits on and off. Nice!
If you wanted to detect V unique characters from a window of W characters where V < W, this trick wouldn't work. But you could still have a rolling window, it would just be a map counting "how many of this character in the window", which you can increment the relevant character for as it enters the window, and decrement as it leaves.
Re: A neat XOR trick
#14I had considered this approach but the nature of AoC means it rewards real world time to solution rather than and kind of computation time, and a naive solution still runs fast enough that it more than makes up in time spent programming it. There's actually whole classes of problems which would be more interesting if the naive solution wasn't fast enough, for example even on day 7 (or was it 8?) naively exploring to…
Re: A neat XOR trick
#15https://graphics.stanford.edu/~seander/bithacks.html#CountBi...
Re: A neat XOR trick
#16One can improve this even further I think. First, using an iterator instead of indexing into an UTF-8 string. Second, using the codepoint value directly as a bitmask (why is author realigning the bit mask to "a" in the first place?)
Re: A neat XOR trick
#17Re: A neat XOR trick
#18count_ones() is a challenge on its own. https://graphics.stanford.edu/~seander/bithacks.html#CountBi...
Re: A neat XOR trick
#19I think the claim at the end is supposed to say `O(D)` running time rather than `O(N)` (or I can't see where `N` is defined). Does this technique catch letters duplicated more than once? In a Python 3 shell: >> 2 ^ 4 6 >> 2 ^ 4 ^ 2 4 >> 2 ^ 4 ^ 2 ^ 2 6 Yes I guess it does, because inputs are used up in order to turn bits on and off. Nice!
Good catch; this should be fixed (using N for input length and W for window size consistently in the writeup).
Re: A neat XOR trick
#20The moral is knowledge is king and no matter how much iron you throw at a problem a well-designed algorithm will beat it.