-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path24-1.linq
79 lines (72 loc) · 2.11 KB
/
24-1.linq
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
<Query Kind="Program" />
static void Main(string[] args)
{
Directory.SetCurrentDirectory(Path.GetDirectoryName(Util.CurrentQueryPath));
string[] inputLines = File.ReadAllLines("24.txt");
int xLength = inputLines[0].Length;
int yLength = inputLines.Length;
char[,] grid = new char[yLength, xLength];
for (int y = 0; y < yLength; y++)
{
for (int x = 0; x < xLength; x++)
{
char c = inputLines[y][x];
grid[x,y] = c;
}
}
HashSet<long> ratings = new HashSet<long>();
while (true)
{
char[,] newGrid = new char[yLength, xLength];
for (int y = 0; y < yLength; y++)
{
for (int x = 0; x < xLength; x++)
{
char c = grid[x,y];
char up = '.', down = '.', right = '.', left = '.';
if (y > 0) up = grid[x, y-1];
if (y < yLength-1) down = grid[x, y+1];
if (x > 0) left = grid[x-1, y];
if (x < xLength-1) right = grid[x+1, y];
int adjacentBugs = (new char[] { up, down, left, right }).Count(n => n == '#');
if (c == '#')
{
if (adjacentBugs != 1)
c = '.';
}
else
{
if (adjacentBugs >= 1 && adjacentBugs <= 2)
c = '#';
}
newGrid[x,y] = c;
}
}
grid = newGrid;
long diversity = CalculateBiodiversity();
if (ratings.Contains(diversity))
{
Console.WriteLine(diversity);
return;
}
else
{
ratings.Add(diversity);
}
}
long CalculateBiodiversity()
{
long biodiversity = 0;
int counter = 1;
for (int y = 0; y < yLength; y++)
{
for (int x = 0; x < xLength; x++)
{
char c = grid[x,y];
if (c == '#') biodiversity += counter;
counter *= 2;
}
}
return biodiversity;
}
}