2021aoc/Day25.cs

64 lines
1.9 KiB
C#

// #define Day25
#if Day25
var linesSplit = File.ReadAllLines("day25/input");
int rowMax = linesSplit.Length;
int colMax = linesSplit[0].Length;
var map = new char[linesSplit.Length, linesSplit[0].Length];
var easts = new List<Point>();
var souths = new List<Point>();
for (int row = 0; row < linesSplit.Length; row++) {
for (int col = 0; col < linesSplit[0].Length; col++) {
if (linesSplit[row][col] == '>') {
map[row, col] = linesSplit[row][col];
easts.Add(new Point(row, col));
} else if (linesSplit[row][col] == 'v') {
map[row, col] = linesSplit[row][col];
souths.Add(new Point(row, col));
}
}
}
long step = 0;
bool moved = true;
while (moved) {
moved = false;
var nextMap = new char[linesSplit.Length, linesSplit[0].Length];
var nextEasts = new List<Point>();
var nextSouths = new List<Point>();
foreach (var east in easts) {
int checkCol = (east.col + 1) % colMax;
if (map[east.row, checkCol] == '\0') {
nextMap[east.row, checkCol] = '>';
nextEasts.Add(new Point(east.row, checkCol));
moved = true;
} else {
nextMap[east.row, east.col] = '>';
nextEasts.Add(new Point(east.row, east.col));
}
}
foreach (var south in souths) {
int checkRow = (south.row + 1) % rowMax;
if (nextMap[checkRow, south.col] == '\0' && map[checkRow, south.col] != 'v') {
nextMap[checkRow, south.col] = 'v';
nextSouths.Add(new Point(checkRow, south.col));
moved = true;
} else {
nextMap[south.row, south.col] = 'v';
nextSouths.Add(new Point(south.row, south.col));
}
}
map = nextMap;
easts = nextEasts;
souths = nextSouths;
step++;
}
Console.WriteLine(step);
record Point(int row, int col);
#endif