-
Notifications
You must be signed in to change notification settings - Fork 0
/
adventOfCode2020day11part2.linq
195 lines (157 loc) · 3.8 KB
/
adventOfCode2020day11part2.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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<Query Kind="Program" />
const char EMPTY = 'L';
const char OCCUPIED = '#';
const char FLOOR = '.';
const char UNKNOWN='z';
bool live = true;
bool skipFloor = true;
int iterations = 100000;
int adjacentToLeave = 5;
List<string> numbers;
void Main()
{
var input = ReadMyFile(@"sample11.txt");
if (live)
{
input = ReadMyFile(@"input11.txt");
}
numbers = ParseInput(input);
char[,] map = new char[numbers.First().Length, numbers.Count()];
for (int i = 0; i < numbers.First().Length; i++)
{
for (int j = 0; j < numbers.Count(); j++)
{
try{
map[i, j] = numbers[j][i];
}catch(Exception ex){
break;
}
}
}
//map.Dump();
var simulatedMap = RunSimulation(map);
CountOccupiedSeats(simulatedMap).Dump();
}
private int CountOccupiedSeats(char[,] map)
{
int counter = 0;
for (int k = 0; k < map.GetLength(0); k++)
for (int l = 0; l < map.GetLength(1); l++)
{
if (map[k, l] == OCCUPIED)
counter++;
}
return counter;
}
private char[,] RunSimulation(char[,] map)
{
for (int i = 0; i < iterations; i++)
{
bool hasChanged = false;
char[,] currentState = map.Clone() as char[,];
for (int k = 0; k < map.GetLength(0); k++)
for (int l = 0; l < map.GetLength(1); l++)
{
var change = TrySittingAtPosition(currentState, k, l);
if (change != map[k, l])
{
hasChanged = true;
map[k, l] = change;
}
}
//map.Dump();
if (!hasChanged)
return map;
}
throw new InvalidDataException("Something is wrong, we never reached equilibrium");
}
private char TrySittingAtPosition(char[,] map, int x, int y)
{
if (map[x, y] == EMPTY && IsSeatAlone(map, x, y))
{
return OCCUPIED;
}
else if (map[x, y] == OCCUPIED && ShouldVacate(map, x, y))
{
return EMPTY;
}
return map[x, y];
}
private bool ShouldVacate(char[,] map, int x, int y)
{
if (OccupiedSeats(map, x, y) >= adjacentToLeave)
{
return true;
}
return false;
}
private bool IsSeatAlone(char[,] map, int x, int y)
{
if (OccupiedSeats(map, x, y) > 0)
return false;
return true;
}
private int OccupiedSeats(char[,] map, int x, int y)
{
int occupied = 0;
if (CheckStepsUntilFirstChair(map, x, y, -1) == OCCUPIED)
occupied++;
if (CheckStepsUntilFirstChair(map, x, y, 1) == OCCUPIED)
occupied++;
if (CheckStepsUntilFirstChair(map, x, y,0, -1) == OCCUPIED)
occupied++;
if (CheckStepsUntilFirstChair(map, x, y,0, 1) == OCCUPIED)
occupied++;
if (CheckStepsUntilFirstChair(map, x, y,-1, -1) == OCCUPIED)
occupied++;
if (CheckStepsUntilFirstChair(map, x, y,1, 1) == OCCUPIED)
occupied++;
if ( CheckStepsUntilFirstChair(map, x, y,1, -1) == OCCUPIED)
occupied++;
if (CheckStepsUntilFirstChair(map, x, y,-1, 1)== OCCUPIED)
occupied++;
return occupied;
}
private char CheckStepsUntilFirstChair(char[,] map, int x, int y, int xDirection = 0, int yDirection = 0)
{
int steps = 1;
while (skipFloor && IsInBounds(map, x + steps * xDirection, y + steps * yDirection) && map[x + steps * xDirection, y+steps * yDirection] == FLOOR)
{
steps++;
}
if (IsInBounds(map, x + steps * xDirection, y + steps * yDirection))
return map[x + steps * xDirection, y + steps * yDirection];
return UNKNOWN;
}
private bool IsInBounds(char[,] map, int x, int y)
{
return IsXInsideBounds(map, x) && IsYInsideBounds(map, y);
}
private bool IsXInsideBounds(char[,] map, int x)
{
return x >= 0 && x <= map.GetLength(0) - 1;
}
private bool IsYInsideBounds(char[,] map, int y)
{
return y >= 0 && y <= map.GetLength(1) - 1;
}
private List<string> ParseInput(List<string> inputs)
{
var list = new List<string>();
foreach (var input in inputs)
{
list.Add(input);
}
return list;
}
private List<string> ReadMyFile(string uri)
{
var newList = new List<string>();
string line;
System.IO.StreamReader reader = new StreamReader(uri);
while ((line = reader.ReadLine()) != null)
{
newList.Add(line);
}
return newList;
}