I finished day 25, and thus the whole AoC 2021. The last day is a sort of modified game of life simulation. It’s not really very tricky, except that you do need to process each round in two stages.
Here’s my SeaCucumberSimulation class:
final class SeaCucumberSimulation
{
private final int width;
private final int height;
private char[][] seaCucumbers;
public SeaCucumberSimulation(final String[] initialConfiguration)
{
width = initialConfiguration[0].length();
height = initialConfiguration.length;
// System.out.format("Width=%d Height=%d%n", width, height);
seaCucumbers = new char[width][height];
for (int y=0; y<height; y++)
{
for (int x=0; x<width; x++)
{
seaCucumbers[x][y] = initialConfiguration[y].charAt(x);
}
}
}
public long runUntilNoneMove()
{
int numberRounds = 1;
boolean someMoved = true;
while (someMoved)
{
someMoved = oneRound();
if (someMoved)
{
numberRounds++;
// System.out.println("after round " + numberRounds);
// outputOceanFloor();
}
}
return numberRounds;
}
boolean oneRound()
{
char[][] newSeaCucumbers = new char[width][height];
boolean someMoved = false;
for (int y=0; y<height; y++)
{
for (int x=0; x<width; x++)
{
newSeaCucumbers[x][y] = '.';
}
}
for (int y=0; y<height; y++)
{
for (int x=0; x<width; x++)
{
final int nextX;
if (x == width-1)
nextX=0;
else
nextX=x+1;
if (seaCucumbers[x][y] == 'v')
newSeaCucumbers[x][y] = 'v';
if (seaCucumbers[x][y] == '>')
{
if (seaCucumbers[nextX][y] == '.')
{
someMoved = true;
newSeaCucumbers[nextX][y] = '>';
}
else
{
newSeaCucumbers[x][y] = '>';
}
}
}
}
seaCucumbers = newSeaCucumbers;
newSeaCucumbers = new char[width][height];
for (int y=0; y<height; y++)
{
for (int x=0; x<width; x++)
{
newSeaCucumbers[x][y] = '.';
}
}
boolean someMoved2 = false;
for (int x=0; x<width; x++)
{
for (int y=0; y<height; y++)
{
final int nextY;
if (y == height-1)
nextY=0;
else
nextY=y+1;
if (seaCucumbers[x][y] == '>')
newSeaCucumbers[x][y] = '>';
if (seaCucumbers[x][y] == 'v')
{
if (seaCucumbers[x][nextY] == '.')
{
someMoved2 = true;
newSeaCucumbers[x][nextY] = 'v';
}
else
{
newSeaCucumbers[x][y] = 'v';
}
}
}
}
seaCucumbers = newSeaCucumbers;
return someMoved || someMoved2;
}
void outputOceanFloor()
{
System.out.println(this.toString());
}
@Override
public String toString()
{
final StringBuilder sb = new StringBuilder();
for (int y=0; y<height; y++)
{
for (int x=0; x<width; x++)
{
sb.append(seaCucumbers[x][y]);
}
sb.append('\n');
}
return sb.toString();
}
}
Which then gets exercised by
public static long getPart1Answer(final String day25Input, final String[] day25InputLines)
{
final SeaCucumberSimulation scs = new SeaCucumberSimulation(day25InputLines);
return scs.runUntilNoneMove();
}
As appears to be the tradition, there is no part two for day 25… it’s just a matter of having all 49 stars to that point to complete it.