-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay18.cs
More file actions
125 lines (105 loc) · 3.98 KB
/
Day18.cs
File metadata and controls
125 lines (105 loc) · 3.98 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
using System.Collections.Immutable;
using System.Runtime.InteropServices.ComTypes;
using _2024.Utils;
namespace _2024._18;
public class Day18 : Base
{
public Day18(bool example) : base(example)
{
Day = "18";
}
private int GetShortestPath(List<char[]> memoryArea, ImmutableSortedSet<ValueTuple<int, int>> corruptingBytes, ValueTuple<int, int> start, ValueTuple<int, int> end)
{
PriorityQueue<ValueTuple<int, int>, int> path = new();
HashSet<ValueTuple<int, int>> visited = [];
path.Enqueue(start, 0);
while (path.TryDequeue(out ValueTuple<int, int> pos, out int stepsTaken))
{
if (pos.Item1+1 == end.Item1 && pos.Item2+1 == end.Item2)
{
return stepsTaken;
}
if (!visited.Add(pos))
{
continue;
}
foreach (ValueTuple<int, int> dir in new[] { (0, 1), (0, -1), (1, 0), (-1, 0) })
{
ValueTuple<int, int> newPos = (pos.Item1+dir.Item1, pos.Item2+dir.Item2);
if(!memoryArea.TryGetValue(newPos, out char _) || corruptingBytes.Contains(newPos))
{
continue;
}
path.Enqueue(newPos, stepsTaken+1);
}
}
return -1;
}
private string FindBlockingPosition(List<char[]> memoryArea, List<ValueTuple<int, int>> corruptingBytes, ValueTuple<int, int> start, ValueTuple<int, int> end)
{
int low = 0;
int high = corruptingBytes.Count - 1;
int mid = 0;
while (low <= high)
{
mid = low + (high - low) / 2;
int shortestPathMid =
GetShortestPath(memoryArea, corruptingBytes[..(mid+1)].ToImmutableSortedSet(), start, end);
int shortestPathOnePrior = GetShortestPath(memoryArea, corruptingBytes[..mid].ToImmutableSortedSet(),
start, end);
if (shortestPathOnePrior != -1 && shortestPathMid == -1)
{
break;
}
if (shortestPathMid != -1)
{
low = mid + 1;
}
else
{
high = mid - 1;
}
}
return $"{corruptingBytes[mid].Item2},{corruptingBytes[mid].Item1}";
}
public override object PartOne()
{
string[] input = ReadInput();
ValueTuple<int, int> start = (0, 0);
ValueTuple<int, int> end = Example ? (7, 7) : (71, 71);
List<char[]> memoryArea = new(end.Item1);
SortedSet<ValueTuple<int, int>> corruptingBytes = [];
for (int i = 0; i < end.Item1; i++)
{
memoryArea.Add(new char[end.Item2]);
}
foreach ((string bytePos, int idx) in input.Enumerate())
{
if (idx == (Example ? 12 : 1024))
{
break;
}
string[] split = bytePos.Split(',');
ValueTuple<int, int> currByte = (int.Parse(split[1]), int.Parse(split[0]));
corruptingBytes.Add(currByte);
}
return GetShortestPath(memoryArea, corruptingBytes.ToImmutableSortedSet(), start, end);
}
public override object PartTwo()
{
string[] input = ReadInput();
ValueTuple<int, int> start = (0, 0);
ValueTuple<int, int> end = Example ? (7, 7) : (71, 71);
List<char[]> memoryArea = new(end.Item1);
for (int i = 0; i < end.Item1; i++)
{
memoryArea.Add(new char[end.Item2]);
}
List<ValueTuple<int, int>> corruptingBytes = [];
corruptingBytes.AddRange(input.Select(bytePos => bytePos.Split(',')).Select(split => (int.Parse(split[1]), int.Parse(split[0]))));
return FindBlockingPosition(memoryArea, corruptingBytes, start, end);
}
public override void Reset()
{
}
}