Expand description
§Planet of Discord
§Part One
The “biodiversity rating” is a very strong hint to store the 5x5 grid as bits in a integer. To make calculating the rating a no-op, we store the grid:
abcde
fghij
klmno
pqrst
uvwxy
packed into a u32
as yxwvutsrqponmlkjihgfedcba
.
Then for each position we create a bitmask for up to 4 potential neighbors.
For example the bitmask for position a
is 100010
and for position h
is 1000101000100
.
For each generation we bitwise AND
each position with its mask, then use the count_ones
intrinsic to to efficiently find the number of neighbors.
§Part Two
The multiple levels can be represented as an array, the outer layer as the previous element and the inner layer as the next. We add two more bitmasks for the outer and inner layers respectively, then calculate the total sum for the 16 tiles on the outer edges with the previous layer and the 9 inner tiles with the next layer. The center tile is a no-op and always zero.
Constants§
Functions§
- Parse the initial grid, placing the top left bug into the least significant bit of the result.