-
Notifications
You must be signed in to change notification settings - Fork 0
/
2021_25.cs
150 lines (131 loc) · 3.58 KB
/
2021_25.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
namespace AOC.CSharp;
public static class AOC2021_25
{
public static long Solve1(string[] lines)
{
char[,] grid = Parse(lines);
int transformCount = 0;
while (true)
{
transformCount++;
char[,] newGrid = Transform(grid);
if (Same(grid, newGrid))
{
return transformCount;
}
grid = newGrid;
}
}
public static long Solve2(string[] lines)
{
return 0L;
}
private static char[,] Parse(string[] lines)
{
int height = lines.Length;
int width = lines[0].Length;
char[,] grid = new char[width, height];
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
grid[x, y] = lines[y][x];
}
}
return grid;
}
private static void Print(char[,] grid)
{
int height = grid.GetLength(1);
int width = grid.GetLength(0);
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
Console.Write(grid[x, y]);
}
Console.WriteLine();
}
}
private static char[,] Init(int width, int height)
{
char[,] newGrid = new char[width, height];
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
newGrid[x, y] = '.';
}
}
return newGrid;
}
private static char[,] Transform(char[,] grid)
{
int height = grid.GetLength(1);
int width = grid.GetLength(0);
char[,] newGrid = Init(width, height);
// Process the east to west moves first
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
char curr = grid[x, y];
if (curr == '>')
{
int checkY = y;
int checkX = x + 1;
checkX = checkX == width ? 0 : checkX;
if (grid[checkX, checkY] == '.')
{
newGrid[checkX, checkY] = '>';
}
else
{
newGrid[x, y] = '>';
}
}
}
}
//Now process north to south
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
char curr = grid[x, y];
if (curr == 'v')
{
int checkX = x;
int checkY = y + 1;
checkY = checkY == height ? 0 : checkY;
if (grid[checkX, checkY] == 'v')
{
newGrid[x, y] = 'v';
}
else if (newGrid[checkX, checkY] == '>')
{
newGrid[x, y] = 'v';
}
else
{
newGrid[checkX, checkY] = 'v';
}
}
}
}
return newGrid;
}
private static bool Same(char[,] grid1, char[,] grid2)
{
for (int y = 0; y < grid1.GetLength(1); y++)
{
for (int x = 0; x < grid1.GetLength(0); x++)
{
if (grid1[x, y] != grid2[x, y])
{
return false;
}
}
}
return true;
}
}