|
| 1 | +== Day 22: Monkey Map == |
| 2 | + |
| 3 | +This is my implementation for both rounds of today's puzzle. |
| 4 | +This year, I am using Rust to solve the challenges. |
| 5 | +My first priority is learning more about the language, while the creation of |
| 6 | +easily readable code is a second priority, this time. |
| 7 | +Wiriting fast code is a far-away third priority. |
| 8 | + |
| 9 | +=== Oveview === |
| 10 | + |
| 11 | +All source code lives in the `src` directory. |
| 12 | +This solution contains a `main.rs`, which defines the main executable. |
| 13 | +There is also a `data.rs`, which specifies data types important for this |
| 14 | +solution as well as associated functions, methods, and traits. |
| 15 | +There is also an `io.rs`, which contains helper functions related to input |
| 16 | +parsing and output processing. |
| 17 | + |
| 18 | +=== Solution === |
| 19 | + |
| 20 | +This one was arguably the hardest one of this AOC. |
| 21 | +For part 1, I solved the problem by building a list of neighbours for each tile |
| 22 | +that is either free or a wall. |
| 23 | +You then simply follow the instruction string given. |
| 24 | +The most important function for building th eneighbour map for part 1 searches |
| 25 | +the map from an edge along a line to find the first tile that is free or a wall. |
| 26 | + |
| 27 | +Part 2 coul dhave been solved by hard-coding the neighbour relations between |
| 28 | +edges, but I didn't want to do that, which is one of the reasons I finished this |
| 29 | +one last and rather late. |
| 30 | +Two main ideas come into play here: |
| 31 | + |
| 32 | +Regarding rotation: |
| 33 | +Assuming you have a correct neighbour map, you can then easily determine the new |
| 34 | +rotation after a move by looking at the previous location. |
| 35 | +If that location is "up" from your current one, then you are now facing down, |
| 36 | +irrespective of any previous heading (holds similarly for the other directions). |
| 37 | + |
| 38 | +Regarding the neighbour map: |
| 39 | +I was wondering how to find out which points are neighbours to which ones. |
| 40 | +As a human, I would fold up the map and construct a cube, which is what I did |
| 41 | +here, too. |
| 42 | +Because the map does not contain any cuts between neighbouring 50x50 patches |
| 43 | +(aka cube faces in the flat map), we can make do with a simple folding |
| 44 | +procedure. |
| 45 | + |
| 46 | +1. Identify points belonging to each cube face and convert to 3D. |
| 47 | + That conversion simply sets the z-coordinate to zero for now. |
| 48 | + Also remeber an "up" or "north" vector and a normal vector for each point. |
| 49 | +2. Identify which cube faces neighbour which other faces. |
| 50 | +3. For each face, determine which faces need to be folded when folding down the |
| 51 | + face to the right. |
| 52 | + This can be determined by a breadth-first search over all face neighbours that |
| 53 | + is blocked by the reference face. |
| 54 | + Do the same for faces that lie downwards. |
| 55 | +4. Construct a transformation consisting of rotation and translation that |
| 56 | + describes the folding operation. |
| 57 | +5. Fold all points in affected faces. |
| 58 | +6. Rinse repeat. |
| 59 | +7. Build the neighbour map considering that a folded neighbour's normal vector |
| 60 | + is identical to the difference vector expected to that neighbour if it were in |
| 61 | + the same plane. |
| 62 | + |
| 63 | +Once you have a neighbour map, you apply the very same algorithm as in part 2 |
| 64 | +with the addition of the aforementioned rotation fix. |
| 65 | +Also note that folded neighbours are identified by the very same location but |
| 66 | +with different normal vectors. |
| 67 | +Thus, the actual tile position could be found by moving a tile in the direction |
| 68 | +of its normal vector by half a unit. |
| 69 | + |
| 70 | +The code looks horrible because I wanted to get it done and didn't care about |
| 71 | +readability. |
| 72 | +Thus, I havent' included it here. |
| 73 | +Please feel free to check out the repo if you want to have a look. |
| 74 | + |
| 75 | +=== How to run === |
| 76 | + |
| 77 | +Please have a look at `src/main.rs` for expeced names of input files. |
| 78 | +Assuming the expected files are present, you only need to execute `cargo run` to |
| 79 | +run the solution. |
| 80 | +The expected input files are ususally called `sample.dat` and `stage_1.dat`. |
0 commit comments