Advent of Code 2023 - Day 14Parabolic Reflector Dish
| Problem statement | Source code | Tags: Arcade gamePeriod finding
Part 1
The implementation of tilt is straightforward: we iterate each column from the "bottom" (the bottom is defined by the tilting direction; for example a north tilt means we iterate from the first row to the last row, while a east tilt means we iterate from the last column to the first column). We keep two pointers: expected_pos is the position where the next O should go, and j is the current position. If we see an O, we move it to expected_pos and update expected_pos. If we see a #, we update expected_pos to be the next position after the wall.
Part 2
Again, R has given me more headaches than it spares me π
Obviously, we want to find the period for the tilting cycles, which involves tracking the list of seen configurations, but R does not have hashing for matrices. I used the classic new.env() hack, which gives me a string map, but its keys can be at most 10,000 bytes long, and the matrix has exactly 10,000 elements, so I can't just naΓ―vely stringify it as a key. Instead, I had to compress the matrix to 2 bits per cell (since there are only 3 states) and serialize it as base64. Anyway, all of this is really tedious and nothing remarkable.
Once I have a hashmap implementation, the rest is standard period finding. If I see the same configuration twice, once at and once at , then the period is , and the configuration at is the same as . The time starts at 0, but again because in R the first index is 1, I had to add 1 when indexing into loads.