I will make two more posts in this series: one with Day 24 plus the finish of Day profitable site 20, and one with Day 25.
Which, played with the usual deck of 52 cards in four suits, we knew as War when I was a kid ?
Welcome back to another two days’ worth of the ongoing chronicle of me trying to teach myself the Rust programming language by doing programming puzzles from Advent of Code 2020.
Day 20, Part 1
Unlike in the past puzzles, I have no idea how to tackle this problem, so I start just by reading in the data. I don’t have to store the actual image data, just the borders, so I can compare them to the borders of the other tiles.
There are eight borders – one on each of the four sides, plus the tiles e borders again but reversed. I’ll store each border as a u16 bit pattern for easy comparing, in an array of length 8.
I do start out with the vague idea that I should use tuple_combinations() for this. But anyway, I’ll read in the data first.
I write the slice things ( s![0, ..;-1] ) after reading a bit more of the documentation about slicing ndarray s. I think this notation is an improvement on the Conway’s Game of Life code that I wrote a few days ago.
Maybe if there is only ever one possibility for each edge to match another tile’s edge, we can iterate over tuple_combinations() and check that there are four tiles that have only two possible connections to other tiles?
If I understand the error message correctly, this is because tuple_combinations() makes copies of the elements, and I don’t think I can copy the tiles – for one thing, I want the number of connections to be updated in the same copy of the tile, I don’t want different copies of the same tile with different connections!
This I don’t understand. I don’t want to borrow the whole array twice, I just want to modify elements in it.
This works, but running it on the example input I get ‘Tile 2311 and 1951 can connect in more than one way’! Then I realize that yes, they could both be connected, but if you flip both of them, then they can still be connected. So we should expect that tiles connect in either 0 or 2 possible ways in connects_to() .
For the array of connections in the example input, I get 4 tiles with 2 connections, 4 tiles with 3 connections, and one tile with 4 connections, which is consistent with the tiles being arranged in a 3?3 square.
Running it on the real input I get a panic, because a zero-length string is being passed to read_tile() . Sure enough, there is an extra newline at the end of the file. I wonder if instead of splitting on “\n\n” I can split on “\n<2,>” , but that requires adding a dependency on the regex package which I’m not using anywhere else, so I add a .filter(|s| s.len() > 0) to read_input() .
I get an overflow in the call to product() so I change the type of the IDs to u64 . This gives me the right answer.
Day 20, Part 2
Next we have to remove the borders of the tiles, and arrange them in the correct configuration, and then search for “sea monsters” that look like this:
From experience at a job a long time ago, I know how to search for the sea monster, by using cross-correlation, which I would be willing to bet that ndarray is capable of doing. Before I get to that part, however, I’m a bit at a loss for how to stitch the pictures together. I feel that I could do it, but without a good plan it would probably be very messy and take me a lot of time to finish.