-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay12.cs
332 lines (315 loc) · 8.77 KB
/
Day12.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
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
using Draco18s.AoCLib;
using System;
using System.Collections.Generic;
using System.Linq;
namespace AdventofCode2022 {
internal static class DayTwelve {
internal static long Part1(string input) {
string[] lines = input.Split('\n');
int sum = 0;
Grid mountain = new Grid(input, true);
Grid distances = new Grid(mountain.Width, mountain.Height);
int x = 0, y = 0;
foreach(string l in lines)
{
if(l.Contains('S'))
{
x = l.IndexOf('S');
break;
}
y++;
}
distances[x, y] = 0;
if(lines[y-1][x] == 'a')
{
y--;
}
else if (lines[y + 1][x] == 'a')
{
y++;
}
/*if (lines[y][x - 1] == 'a')
{
x--;
}*/
else if (lines[y][x + 1] == 'a')
{
x++;
}
distances[x, y] = 1;
int r = CheckPath(mountain, new Vector2(x, y), distances);
//Console.WriteLine(distances.ToString("char"));
x = y = 0;
foreach (string l in lines)
{
if (l.Contains('E'))
{
x = l.IndexOf('E');
break;
}
y++;
}
//Console.WriteLine(distances.ToString());
return distances[x, y];
//return -1;
}
private static int BestChar = 'a';
private static int CheckPath(Grid mountain, Vector2 pos, Grid dist)
{
if (mountain[pos] == 'E')
{
//Console.WriteLine($"Final value: {dist[pos]}");
return dist[pos];
}
int h = mountain[pos];
if(mountain[pos] > BestChar)
{
char a = (char)h;
BestChar = a;
int v = dist[pos];
//if(a >= 'p')
//Console.WriteLine($"{a}:{dist[pos]}");
}
if(pos.x - 1 >= 0 && ((mountain[pos.x-1,pos.y] - h) <= 1 || mountain[pos.x - 1, pos.y] == 'E'))
{
if(dist[pos.x - 1, pos.y] == 0 || dist[pos.x - 1, pos.y] > dist[pos.x, pos.y]+1)
{
dist[pos.x - 1, pos.y] = dist[pos.x, pos.y] + 1;
CheckPath(mountain, new Vector2(pos.x - 1, pos.y), dist);
}
}
if (pos.y - 1 >= 0 && ((mountain[pos.x, pos.y - 1] - h) <= 1 || mountain[pos.x, pos.y - 1] == 'E'))
{
if (dist[pos.x, pos.y - 1] == 0 || dist[pos.x, pos.y - 1] > dist[pos.x, pos.y] + 1)
{
dist[pos.x, pos.y - 1] = dist[pos.x, pos.y] + 1;
CheckPath(mountain, new Vector2(pos.x, pos.y - 1), dist);
}
}
if (pos.x + 1 < mountain.Width && ((mountain[pos.x + 1, pos.y] - h) <= 1 || mountain[pos.x + 1, pos.y] == 'E'))
{
if (dist[pos.x + 1, pos.y] == 0 || dist[pos.x + 1, pos.y] > dist[pos.x, pos.y] + 1)
{
dist[pos.x + 1, pos.y] = dist[pos.x, pos.y] + 1;
CheckPath(mountain, new Vector2(pos.x + 1, pos.y), dist);
}
}
if (pos.y + 1 < mountain.Height && ((mountain[pos.x, pos.y + 1] - h) <= 1 || mountain[pos.x, pos.y + 1] == 'E'))
{
if (dist[pos.x, pos.y + 1] == 0 || dist[pos.x, pos.y + 1] > dist[pos.x, pos.y] + 1)
{
dist[pos.x, pos.y + 1] = dist[pos.x, pos.y] + 1;
CheckPath(mountain, new Vector2(pos.x, pos.y + 1), dist);
}
}
return -1;
}
private static int bestdist = int.MaxValue;
private static char last = 'z';
private static int CheckPath2(Grid mountain, Vector2 pos, Grid dist)
{
if (mountain[pos] == 'S')
{
return -1;
}
if (mountain[pos] == 'a')
{
int v = dist[pos];
dist[pos] = 0;
}
if (mountain[pos] == 'E')
{
//Console.WriteLine($"Final value: {dist[pos]}");
return dist[pos];
}
int h = mountain[pos];
if (mountain[pos] > BestChar)
{
char a = (char)h;
BestChar = a;
int v = dist[pos];
//if(a >= 'p')
//Console.WriteLine($"{a}:{dist[pos]}");
}
if (pos.x - 1 >= 0 && ((mountain[pos.x - 1, pos.y] - h) <= 1 || mountain[pos.x - 1, pos.y] == 'E'))
{
if (dist[pos.x - 1, pos.y] == -1 || dist[pos.x - 1, pos.y] > dist[pos.x, pos.y] + 1)
{
dist[pos.x - 1, pos.y] = dist[pos.x, pos.y] + 1;
CheckPath2(mountain, new Vector2(pos.x - 1, pos.y), dist);
}
}
if (pos.y - 1 >= 0 && ((mountain[pos.x, pos.y - 1] - h) <= 1 || mountain[pos.x, pos.y - 1] == 'E'))
{
if (dist[pos.x, pos.y - 1] == -1 || dist[pos.x, pos.y - 1] > dist[pos.x, pos.y] + 1)
{
dist[pos.x, pos.y - 1] = dist[pos.x, pos.y] + 1;
CheckPath2(mountain, new Vector2(pos.x, pos.y - 1), dist);
}
}
if (pos.x + 1 < mountain.Width && ((mountain[pos.x + 1, pos.y] - h) <= 1 || mountain[pos.x + 1, pos.y] == 'E'))
{
if (dist[pos.x + 1, pos.y] == -1 || dist[pos.x + 1, pos.y] > dist[pos.x, pos.y] + 1)
{
dist[pos.x + 1, pos.y] = dist[pos.x, pos.y] + 1;
CheckPath2(mountain, new Vector2(pos.x + 1, pos.y), dist);
}
}
if (pos.y + 1 < mountain.Height && ((mountain[pos.x, pos.y + 1] - h) <= 1 || mountain[pos.x, pos.y + 1] == 'E'))
{
if (dist[pos.x, pos.y + 1] == -1 || dist[pos.x, pos.y + 1] > dist[pos.x, pos.y] + 1)
{
dist[pos.x, pos.y + 1] = dist[pos.x, pos.y] + 1;
CheckPath2(mountain, new Vector2(pos.x, pos.y + 1), dist);
}
}
return -1;
}
internal static long Part2(string input) {
string[] lines = input.Split('\n');
int sum = 0;
Grid mountain = new Grid(input, true);
Grid distances = new Grid(mountain.Width, mountain.Height);
int x = 0, y = 0;
for(x=0;x<distances.Width;x++)
{
for (y=0; y < distances.Height; y++)
{
distances[x, y] = -1;
}
}
x = y = 0;
foreach (string l in lines)
{
if (l.Contains('S'))
{
x = l.IndexOf('S');
break;
}
y++;
}
distances[x, y] = 0;
if (lines[y - 1][x] == 'a')
{
y--;
}
else if (lines[y + 1][x] == 'a')
{
y++;
}
else if (lines[y][x + 1] == 'a')
{
x++;
}
distances[x, y] = 0;
int r = CheckPath2(mountain, new Vector2(x, y), distances);
//Console.WriteLine(distances.ToString("char"));
x = y = 0;
foreach (string l in lines)
{
if (l.Contains('E'))
{
x = l.IndexOf('E');
break;
}
y++;
}
//Console.WriteLine(distances.ToString());
return distances[x, y];
}
//This code doesn't work for my input.
//It should work, but it returns 352 which is LONGER than part 1.
//Which makes NO SENSE what so ever.
//Start at the end
//work backwards
//if we find an 'a' while at 'b', report that distance, get the best result.
//right?
//then why is it finding a path longer than part 1?
/*internal static long Part2(string input) {
string[] lines = input.Split('\n');
int sum = 0;
Grid mountain = new Grid(input, true);
Grid distances = new Grid(mountain.Width, mountain.Height);
int x = 0, y = 0;
foreach (string l in lines)
{
if (l.Contains('E'))
{
x = l.IndexOf('E'); //find the end
break;
}
y++;
}
distances[x, y] = 0;
if (lines[y - 1][x] == 'z') //get the next z (there's only one adj)
{
y--;
}
else if (lines[y + 1][x] == 'z')
{
y++;
}
if (lines[y][x - 1] == 'z')
{
x--;
}
else if (lines[y][x + 1] == 'z')
{
x++;
}
distances[x, y] = 1; //z is one step from E
BestChar = 'z';
int r = CheckPathBroken(mountain, new Vector2(x, y), distances);
return r;
}
private static int CheckPathBroken(Grid mountain, Vector2 pos, Grid dist)
{
if (mountain[pos] == 'E') return -1;
if (mountain[pos] == 'a') //if at a, return best distance
{
if(dist[pos] < bestdist)
{
bestdist = dist[pos];
}
return bestdist;
}
int h = mountain[pos];
char a = (char)h;
last = a;
//check one step forwards in each direction, if we find an 'a' and are on 'b', we found *a* path to *an* 'a'
if (pos.x - 1 >= 0 && ((h - mountain[pos.x - 1, pos.y]) <= 1 || (mountain[pos.x - 1, pos.y] == 'a' && last == 'b')))
{
if (dist[pos.x - 1, pos.y] == 0 || dist[pos.x - 1, pos.y] > dist[pos.x, pos.y] + 1) //use the better distance
{
dist[pos.x - 1, pos.y] = dist[pos.x, pos.y] + 1;
CheckPathBroken(mountain, new Vector2(pos.x - 1, pos.y), dist); //recurse
}
}
if (pos.y - 1 >= 0 && ((h - mountain[pos.x, pos.y - 1]) <= 1 || (mountain[pos.x, pos.y - 1] == 'a' && last == 'b')))
{
if (dist[pos.x, pos.y - 1] == 0 || dist[pos.x, pos.y - 1] > dist[pos.x, pos.y] + 1)
{
dist[pos.x, pos.y - 1] = dist[pos.x, pos.y] + 1;
CheckPathBroken(mountain, new Vector2(pos.x, pos.y - 1), dist);
}
}
if (pos.x + 1 < mountain.Width && ((h-mountain[pos.x + 1, pos.y]) <= 1 || (mountain[pos.x + 1, pos.y] == 'a' && last == 'b')))
{
if (dist[pos.x + 1, pos.y] == 0 || dist[pos.x + 1, pos.y] > dist[pos.x, pos.y] + 1)
{
dist[pos.x + 1, pos.y] = dist[pos.x, pos.y] + 1;
CheckPathBroken(mountain, new Vector2(pos.x + 1, pos.y), dist);
}
}
if (pos.y + 1 < mountain.Height && ((h-mountain[pos.x, pos.y + 1]) <= 1 || (mountain[pos.x, pos.y + 1] == 'a' && last == 'b')))
{
if (dist[pos.x, pos.y + 1] == 0 || dist[pos.x, pos.y + 1] > dist[pos.x, pos.y] + 1)
{
dist[pos.x, pos.y + 1] = dist[pos.x, pos.y] + 1;
CheckPathBroken(mountain, new Vector2(pos.x, pos.y + 1), dist);
}
}
return bestdist;
}*/
}
}