Module advent_of_code_2021::day_25[−][src]

Expand description

This is my solution for Advent of Code - Day 25 - Sea Cucumber

After the last two days, today was a refreshing change back to a more ‘normal’ Advent of Code puzzle. We were given a grid representing herds of Sea Cucumbers wanting to move in a certain direction and had to iterate those moves until the grid stabilised with all the sea cucumbers blocked by another one. A slight twist was some ‘strong currents’ that caused the grid to wrap around in both x and y.

Cell represents the three possibilities for any cell in the grid: Empty, Rightwards moving cucumber, downwards moving cucumber. Grid stores the whole grid similar to crate::util::grid, but different enough that it was easier to just re-implement it. Grid::from parses the puzzle input with help from Cell::try_from. Grid::fmt and Cell::fmt go the other way for ease of testing. Grid::get, Grid::pos_of, Grid::swap, and Grid::can_move are all utilities that help with iterating the grid. Grid::iterate completes a single iteration step of each herd trying to move. This is where the one efficiency trick of the day is apparent. The grid has a caches of the cucumbers that might be able to move. Starting with all the cucumbers assigned to a cache for their direction, only 1) the sea cucumbers that have moved, and 2) the sea cucumbers positioned so they could move into the vacated cell, are added to the active set for the next iteration. This achieves two things: It limits the cells we need to check each iteration, and it is a handy indicator of if the grid has stabilised. As only moving cucumbers cause additions to the active sets, the grid is stable if and only if the two caches are empty.

That solves part one, and part two was the traditional “finish all the tasks and click the button to resolve the plot” task. I was able to complete each task on the day this year (just - day 24 was finally done at 2am on 25th UTC, so 3 hours before the cutoff), so this was already complete for me.

      --------Part 1--------   --------Part 2--------
Day       Time   Rank  Score       Time   Rank  Score
 25   15:06:43   8974      0   15:07:35   5460      0
 24   21:15:47   5841      0   21:16:30   5687      0
 23   08:47:15   5672      0   19:27:15   6218      0
 22   07:00:04   9762      0   07:01:31   3769      0
 21   05:37:25  11193      0   06:36:50   6331      0
 20   07:51:38   8898      0   07:58:09   8586      0
 19   13:14:31   5465      0   13:37:20   5294      0
 18   19:49:26  12383      0   20:00:08  12192      0
 17   04:47:21  10448      0   05:52:20  10205      0
 16   07:07:32   9928      0   07:16:09   8599      0
 15   05:47:19  12724      0   07:05:51  10514      0
 14   05:39:34  20863      0   07:04:42  14630      0
 13   05:19:33  17344      0   05:40:01  16739      0
 12   14:30:26  26961      0   14:55:24  24961      0
 11   08:27:10  21478      0   08:32:38  21179      0
 10   07:15:30  27171      0   08:06:12  26391      0
  9   08:32:37  32148      0   11:48:09  27638      0
  8   07:01:34  32052      0   08:06:36  19511      0
  7   04:14:51  28137      0   04:40:14  27200      0
  6   04:50:17  27578      0   04:53:08  20498      0
  5   07:05:00  26127      0   07:56:49  24779      0
  4   14:56:36  42423      0   15:45:56  40155      0
  3   06:10:06  46408      0   08:06:50  34497      0
  2   04:22:10  39520      0   04:29:07  37024      0
  1   08:11:39  47103      0   09:01:48  43667      0

Structs

Represent a grid as a vector of cells, with a width and height to enable quick lookups from x/y co-ordinates, and to help with wrapping around logic. Also keep HashSets of the RIGHT and DOWN cells that may be able to move, to limit the cells we need to check when iterating the grid

Enums

Represent the current state of a cell in the grid

Functions

The entry point for running the solutions with the ‘real’ puzzle input.