-
Notifications
You must be signed in to change notification settings - Fork 0
/
18.linq
41 lines (34 loc) · 1.21 KB
/
18.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
<Query Kind="Program" />
void Main()
{
int totalRows = 400000;
string input = "^.^^^.^..^....^^....^^^^.^^.^...^^.^.^^.^^.^^..^.^...^.^..^.^^.^..^.....^^^.^.^^^..^^...^^^...^...^.";
List<string> rows = new List<string> { input };
int remainingRows = totalRows - 1;
while (remainingRows > 0)
{
string previousRow = rows.Last();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < previousRow.Length; i++)
{
char space = '.';
bool l = false;
bool c = previousRow[i] == '^';
bool r = false;
if (i != 0) l = previousRow[i-1] == '^';
if (i != previousRow.Length - 1) r = previousRow[i+1] == '^';
if ((l && c && !r) ||
(!l && c && r) ||
(l && !c && !r) ||
(!l && !c && r))
{
space = '^';
}
sb.Append(space);
}
rows.Add(sb.ToString());
remainingRows--;
}
int safeTiles = rows.SelectMany(s => s.ToCharArray()).Count(c => c == '.');
safeTiles.Dump();
}