-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay16.cs
198 lines (182 loc) · 5.66 KB
/
Day16.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
namespace AdventofCode2022 {
internal static class DaySixteen {
class ValveRoom
{
public string name;
public int pressure;
public string tunnels;
public List<ValveRoom> connections;
public ValveRoom(string n, int p)
{
name = n;
pressure = p;
connections = new List<ValveRoom>();
}
public void AddConnection(ValveRoom r)
{
if(!connections.Contains(r))
connections.Add(r);
if (!r.connections.Contains(this))
r.connections.Add(this);
}
}
class PathOption
{
public string roomName;
public ValveRoom thisRoom;
public int timeToHere;
public List<PathOption> next;
public long cachedReleaseTotal = -1;
List<string> cachedPathTaken;
List<string> cachedOpenValves;
public PathOption()
{
next = new List<PathOption>();
}
public long GetPressureReleased(ref List<string> pathTaken, ref List<string> openValves)
{
if (cachedReleaseTotal >= 0)
{
pathTaken = cachedPathTaken;
openValves = cachedOpenValves;
return cachedReleaseTotal;
}
long ourReleaseA = 0;
long ourReleaseB = 0;
pathTaken.Add(this.roomName);
long bestChild = 0;
List<string> bestChildValves = null;
List<string> bestChildPath = null;
foreach (PathOption pathOption in next)
{
List<string> valves = new List<string>();
List<string> path = new List<string>();
valves.AddRange(openValves);
path.AddRange(pathTaken);
long child = pathOption.GetPressureReleased(ref path, ref valves);
if (bestChild < child)
{
bestChildPath = path;
bestChildValves = valves;
bestChild = child;
}
}
if (!openValves.Contains(roomName) && thisRoom.pressure > 0 && (bestChildPath?.Count + openValves.Count) < 29)
{
ourReleaseA = (30 - (bestChildPath.Count + openValves.Count + 1)) * thisRoom.pressure;
}
if (!openValves.Contains(roomName) && thisRoom.pressure > 0 && (pathTaken.Count + openValves.Count) < 29)
{
ourReleaseB = (30 - (pathTaken.Count + openValves.Count + 1)) * thisRoom.pressure;
}
long bestChild2 = 0;
List<string> bestChildValves2 = null;
List<string> bestChildPath2 = null;
foreach (PathOption pathOption in next)
{
List<string> valves = new List<string>();
List<string> path = new List<string>();
valves.AddRange(openValves);
if(ourReleaseB > 0) valves.Add(this.roomName);
path.AddRange(pathTaken);
long child = pathOption.GetPressureReleased(ref path, ref valves);
if (bestChild2 < child)
{
bestChildPath2 = path;
bestChildValves2 = valves;
bestChild2 = child;
}
}
cachedReleaseTotal = 0;
if (bestChild + ourReleaseA > bestChild2 + ourReleaseB)
{
cachedReleaseTotal = bestChild + ourReleaseA;
openValves.AddRange(bestChildValves);
pathTaken = bestChildPath;
if(ourReleaseA > 0) openValves.Add(this.roomName);
}
else if(bestChild2 > 0)
{
cachedReleaseTotal = bestChild2 + ourReleaseB;
openValves.AddRange(bestChildValves2);
pathTaken = bestChildPath2;
if (ourReleaseB > 0) openValves.Add(this.roomName);
}
else if(thisRoom.pressure > 0 && (pathTaken.Count + openValves.Count + 1) < 30)
{
ourReleaseB = (30 - (pathTaken.Count + openValves.Count + 1)) * thisRoom.pressure;
cachedReleaseTotal = ourReleaseB;
if (ourReleaseB > 0) openValves.Add(this.roomName);
}
openValves = openValves.Distinct().ToList();
cachedPathTaken = new List<string>();
cachedPathTaken.AddRange(pathTaken);
cachedOpenValves = new List<string>();
cachedOpenValves.AddRange(openValves);
return cachedReleaseTotal;
}
}
internal static long Part1(string input)
{
string[] lines = input.Split('\n');
long sum = 0;
Regex pressureMatch = new Regex("rate=(\\d+);");
Regex tunnelMatch = new Regex("valves( ([A-Z]+),?)+");
Dictionary<string, ValveRoom> valves = new Dictionary<string, ValveRoom>();
foreach (string lin in lines)
{
Match preMatch = pressureMatch.Match(lin);
int pressure = int.Parse(preMatch.Groups[1].Value);
string name = lin.Substring(6, 2);
ValveRoom room = new ValveRoom(name, pressure);
preMatch = tunnelMatch.Match(lin);
room.tunnels = preMatch.Value.Replace("valves","");
valves.Add(name, room);
}
foreach (ValveRoom r in valves.Values)
{
string[] conns = r.tunnels.Split(',');
foreach (string c in conns)
{
if (string.IsNullOrWhiteSpace(c)) continue;
var c2 = c.Replace(" ", "");
r.AddConnection(valves[c2]);
}
}
PathOption currentroom = new PathOption();
currentroom.roomName = "AA";
currentroom.thisRoom = valves["AA"];
ParseRoomGraph(currentroom, "AA");
List<string> pathtaken = new List<string>();
List<string> valvesOpen = new List<string>();
sum = currentroom.GetPressureReleased(ref pathtaken, ref valvesOpen);
return sum;
}
private static void ParseRoomGraph(PathOption currentroom, string prevRoom)
{
if (currentroom.timeToHere >= 29) return;
foreach (ValveRoom rn in currentroom.thisRoom.connections)
{
PathOption newRoom = new PathOption();
newRoom.roomName = rn.name;
newRoom.thisRoom = rn;
newRoom.timeToHere = currentroom.timeToHere + 1;// + (currentroom.thisRoom.pressure > 0 ? 1 : 0);
currentroom.next.Add(newRoom);
}
foreach(var nr in currentroom.next)
{
if (nr.roomName == prevRoom) continue;
ParseRoomGraph(nr, currentroom.roomName);
}
}
internal static long Part2(string input) {
string[] lines = input.Split('\n');
int sum = 0;
return sum;
}
}
}