-
Notifications
You must be signed in to change notification settings - Fork 0
/
2015_18.cs
158 lines (134 loc) · 4.08 KB
/
2015_18.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
151
152
153
154
155
156
157
158
namespace AOC.CSharp;
public static class AOC2015_18
{
private const char Stuck = 'S';
private const char On = '#';
private const char Off = '.';
public static long Solve1(string[] lines, string extra)
{
var chars = Parse(lines, false);
int iterations = int.Parse(extra);
for (int i = 0; i < iterations; i++)
{
chars = Transform(chars);
}
long count = CountOn(chars);
return count;
}
public static long Solve2(string[] lines, string extra)
{
var chars = Parse(lines, true);
int iterations = int.Parse(extra);
Print(chars);
Console.WriteLine();
for (int i = 0; i < iterations; i++)
{
chars = Transform(chars);
Print(chars);
Console.WriteLine();
}
long count = CountOn(chars);
return count;
}
private static char[,] Parse(string[] lines, bool stuck)
{
char[,] chars = new char[lines.Length + 2, lines.Length + 2];
for (int i = 0; i <= lines.Length + 1; i++)
{
for (int j = 0; j <= lines.Length + 1; j++)
{
chars[i, j] = Off;
}
}
for (int i = 1; i <= lines.Length; i++)
{
char[] lineChars = lines[i - 1].ToCharArray();
for (int j = 1; j <= lineChars.Length; j++)
{
if (stuck)
{
if (i == 1 && j == 1)
{
chars[i, j] = Stuck;
}
else if (i == 1 && j == lineChars.Length)
{
chars[i, j] = Stuck;
}
else if (i == lineChars.Length && j == 1)
{
chars[i, j] = Stuck;
}
else if (i == lineChars.Length && j == lineChars.Length)
{
chars[i, j] = Stuck;
}
else
{
chars[i, j] = lineChars[j - 1];
}
}
else
{
chars[i, j] = lineChars[j - 1];
}
}
}
return chars;
}
private static char[,] Transform(char[,] chars)
{
char[,] newChars = new char[chars.GetLength(0), chars.GetLength(1)];
Array.Copy(chars, newChars, chars.Length);
for (int i = 1; i < chars.GetLength(0) - 1; i++)
{
for (int j = 1; j < chars.GetLength(1) - 1; j++)
{
char ch = chars[i, j];
int neighborsOn = 0;
for (int a = i - 1; a <= i + 1; a++)
{
for (int b = j - 1; b <= j + 1; b++)
{
if (!(a == i && b == j))
{
neighborsOn += (chars[a, b] == On || chars[a, b] == Stuck) ? 1 : 0;
}
}
}
if (ch == On)
{
newChars[i, j] = neighborsOn >= 2 && neighborsOn <= 3 ? On : Off;
}
else if (ch == Off)
{
newChars[i, j] = neighborsOn == 3 ? On : Off;
}
}
}
return newChars;
}
private static long CountOn(char[,] chars)
{
long count = 0L;
for (int i = 1; i < chars.GetLength(0) - 1; i++)
{
for (int j = 1; j < chars.GetLength(1) - 1; j++)
{
count += (chars[i, j] == On || chars[i, j] == Stuck) ? 1 : 0;
}
}
return count;
}
private static void Print(char[,] chars)
{
for (int i = 1; i < chars.GetLength(0) - 1; i++)
{
for (int j = 1; j < chars.GetLength(1) - 1; j++)
{
Console.Write(chars[i, j]);
}
Console.WriteLine();
}
}
}