r/adventofcode • • Dec 03 '18

SOLUTION MEGATHREAD -🎄- 2018 Day 3 Solutions -🎄-

--- Day 3: No Matter How You Slice It ---


Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).

Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


Advent of Code: The Party Game!

Click here for rules

ATTENTION: minor change request from the mods!

Please prefix your card submission with something like [Card] to make scanning the megathread easier. THANK YOU!

Card prompt: Day 3 image coming soon - imgur is being a dick, so I've contacted their support.

Transcript:

I'm ready for today's puzzle because I have the Savvy Programmer's Guide to ___.


This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.

edit: Leaderboard capped, thread unlocked!

41 Upvotes

426 comments sorted by

View all comments

Show parent comment

2

u/tehjimmeh Dec 04 '18

An int is 32 bits. $x -shl 16 will shift the bits in the int, $x, left 16 times. This means that the rightmost 16 bits are moved into the leftmost 16 bit area.

In hex, each digit nicely represents 4 bits. If you were to shift 0x00001234 left 16 times, it'd be 0x12340000.

-bor means a bitwise or operation. It does a logical or operation on each bit in an int with another. This has the effect of producing an int with every bit in each of the two ints set.

So let's say you have a grid position, (567,826). In hex, that's (0x00000237,0x0000033A). If you shift the x value 16 bits left, you get 0x02370000, and if you bitwise or that with the y value, you get 0x0237033A - so you have both the x any y values encoded within a singled 32-bit int, which can be used as a unique key in a hash table.

1

u/ka-splam Dec 05 '18

Ohh yeah I see it - that's clever. Faster than "$x $y" and other ways to get both into one hashtable key. Neat.