-
Notifications
You must be signed in to change notification settings - Fork 10
/
Day25.cs
182 lines (151 loc) · 5.65 KB
/
Day25.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
using System;
using System.Collections.Generic;
using AdventOfCode.CSharp.Common;
namespace AdventOfCode.CSharp.Y2023.Solvers;
public class Day25 : ISolver
{
public static void Solve(ReadOnlySpan<byte> input, Solution solution)
{
// no part 2 today
solution.SubmitPart2(string.Empty);
var idLookup = new Dictionary<int, int>(2000);
var graph = new List<List<(int Destination, int EdgeId)>>(2000);
int edgeCount = 0;
while (!input.IsEmpty)
{
int lhsName = NameToId(input.Slice(0, 3));
List<(int Destination, int EdgeId)> lhsList;
if (!idLookup.TryGetValue(lhsName, out int lhsId))
{
lhsId = idLookup.Count;
lhsList = new(10);
graph.Add(lhsList);
idLookup[lhsName] = lhsId;
}
else
{
lhsList = graph[lhsId];
}
input = input.Slice(5);
while (true)
{
int name = NameToId(input.Slice(0, 3));
List<(int Destination, int EdgeId)> list;
if (!idLookup.TryGetValue(name, out int id))
{
id = idLookup.Count;
list = new(10);
graph.Add(list);
idLookup[name] = id;
}
else
{
list = graph[id];
}
lhsList.Add((id, edgeCount++));
list.Add((lhsId, edgeCount++));
bool hasNext = input[3] == ' ';
input = input.Slice(4);
if (!hasNext)
break;
}
}
// Reusable bitset used in FindFurthestNode, FordFulkersonIteration, and CountReachableNodes
ulong[] visited = new ulong[(graph.Count - 1) / 64 + 1];
// We assume that s and t are on different sides of the cut.
// It is possible to construct a graph that this is not true, but this is not the case for AoC inputs.
int s = FindFurthestNode(0);
int t = FindFurthestNode(s);
ulong[] edgeFlows = new ulong[(edgeCount - 1) / 64 + 1];
for (int i = 0; i < 3; i++)
FordFulkersonIteration();
int reachableFromSource = CountReachableNodes();
int part1 = reachableFromSource * (graph.Count - reachableFromSource);
solution.SubmitPart1(part1);
int FindFurthestNode(int node)
{
Array.Clear(visited);
visited[node / 64] = 1UL << node;
int[] queue = new int[graph.Count];
queue[0] = node;
int queuePtr = 0;
int queueLen = 1;
while (queueLen < graph.Count)
{
node = queue[queuePtr++];
foreach ((int destination, _) in graph[node])
{
ulong flag = 1UL << destination;
if ((visited[destination / 64] & flag) == 0)
{
visited[destination / 64] |= flag;
queue[queueLen++] = destination;
}
}
}
return queue[queueLen - 1];
}
void FordFulkersonIteration()
{
Array.Clear(visited);
visited[s / 64] = 1UL << s;
TryDFS(s);
bool TryDFS(int node)
{
if (node == t)
return true;
foreach ((int destination, int edgeId) in graph[node])
{
ulong flag = 1UL << destination;
if ((visited[destination / 64] & flag) != 0)
continue;
ulong edgeFlag = 1UL << edgeId;
if ((edgeFlows[edgeId / 64] & edgeFlag) != 0)
continue;
visited[destination / 64] |= flag;
if (TryDFS(destination))
{
int inverseEdgeId = edgeId ^ 1;
ulong inverseEdgeFlag = 1UL << inverseEdgeId;
// if the inverse is 1, then we need to set the inverse to 0
// if the inverse is 0, then we need to set the edge to 1
if ((edgeFlows[inverseEdgeId / 64] & inverseEdgeFlag) != 0)
{
edgeFlows[inverseEdgeId / 64] ^= inverseEdgeFlag;
}
else
{
edgeFlows[edgeId / 64] |= edgeFlag;
}
return true;
}
}
return false;
}
}
int CountReachableNodes()
{
Array.Clear(visited);
visited[s / 64] = 1UL << s;
int count = 1;
DFS(s);
return count;
void DFS(int node)
{
foreach ((int destination, int edgeId) in graph[node])
{
ulong flag = 1UL << destination;
if ((visited[destination / 64] & flag) != 0)
continue;
ulong edgeFlag = 1UL << edgeId;
if ((edgeFlows[edgeId / 64] & edgeFlag) != 0)
continue;
visited[destination / 64] |= flag;
count++;
DFS(destination);
}
}
}
}
private static int NameToId(ReadOnlySpan<byte> name) => (name[0] << 16) | (name[1] << 8) | (name[2]);
}