Repository navigation
Expand file tree
/
Copy pathDay11.cs
More file actions
177 lines (139 loc) · 4.61 KB
/
Copy pathDay11.cs
File metadata and controls
177 lines (139 loc) · 4.61 KB
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
using System.Text;
public static class Day11
{
public static int GetOccupied(string path)
{
char[][] lines = File.ReadAllLines(path).Select(x => x.ToArray()).ToArray();
var copy = lines.Select(x => x.ToArray()).ToArray();
var i = 0;
while (true)
{
i++;
for (int y = 0; y < lines.Length; y++)
{
var line = lines[y];
for (int x = 0; x < line.Length; x++)
{
var seat = line[x];
if (seat == '.')
{
continue;
}
if (seat == 'L' && NoOccupiedAdjacent(lines, y, x))
{
copy[y][x] = '#';
}
if (seat == '#' && AllAdjacentAreOccupided(lines, y, x))
{
copy[y][x] = 'L';
}
}
}
if (BoardsAreTheSame(lines, copy))
{
break;
}
// Dump(copy);
lines = copy.Select(x => x.ToArray()).ToArray();
}
Dump(lines);
Console.WriteLine(i);
return lines.Sum(x => x.Count(x => x == '#'));
}
private static bool BoardsAreTheSame(char[][] lines, char[][] copy)
{
for (int i = 0; i < lines.Length; i++)
{
for (int j = 0; j < lines[i].Length; j++)
{
if (lines[i][j] != copy[i][j])
return false;
}
}
return true;
}
private static void Dump(char[][] lines)
{
var stringBuilder = new StringBuilder();
foreach (var line in lines)
{
foreach (var c in line)
{
stringBuilder.Append(c);
}
stringBuilder.AppendLine();
}
Console.WriteLine(stringBuilder.ToString());
Console.WriteLine();
}
private static bool AllAdjacentAreOccupided(char[][] lines, int y, int x)
{
bool isOccupied(char[][] lines, Tuple<int, int, int, int> info)
{
var y = info.Item1 + info.Item3;
var x = info.Item2 + info.Item4;
while (true)
{
if (y < 0 || x < 0 || y > lines.Length - 1 || x > lines[y].Length - 1)
return false;
var c = lines[y][x];
if (c == '#')
{
return true;
}
if (c == 'L')
{
return false;
}
y += info.Item3;
x += info.Item4;
}
};
var locations = new List<Tuple<int, int, int, int>> {
new Tuple<int, int, int, int>(y, x, -1, 0),
new Tuple<int, int, int, int>(y, x, -1, -1),
new Tuple<int, int, int, int>(y, x, -1, 1),
new Tuple<int, int, int, int>(y, x, 1, 0),
new Tuple<int, int, int, int>(y, x, 1, -1),
new Tuple<int, int, int, int>(y, x, 1, 1),
new Tuple<int, int, int, int>(y, x, 0, -1),
new Tuple<int, int, int, int>(y, x, 0, 1),
};
return locations.Count(x => isOccupied(lines, x)) >= 5;
}
private static bool NoOccupiedAdjacent(char[][] lines, int y, int x)
{
bool isNotOccupied(char[][] lines, Tuple<int, int, int, int> info)
{
var y = info.Item1 + info.Item3;
var x = info.Item2 + info.Item4;
while (true)
{
if (y < 0 || x < 0 || y > lines.Length - 1 || x > lines[y].Length - 1)
return true;
var c = lines[y][x];
if (c == '#')
{
return false;
}
if (c == 'L')
{
return true;
}
y += info.Item3;
x += info.Item4;
}
};
var locations = new List<Tuple<int, int, int, int>> {
new Tuple<int, int, int, int>(y, x, -1, 0),
new Tuple<int, int, int, int>(y, x, -1, -1),
new Tuple<int, int, int, int>(y, x, -1, 1),
new Tuple<int, int, int, int>(y, x, 1, 0),
new Tuple<int, int, int, int>(y, x, 1, -1),
new Tuple<int, int, int, int>(y, x, 1, 1),
new Tuple<int, int, int, int>(y, x, 0, -1),
new Tuple<int, int, int, int>(y, x, 0, 1),
};
return locations.All(x => isNotOccupied(lines, x));
}
}