-
Notifications
You must be signed in to change notification settings - Fork 0
/
day16.c3
179 lines (170 loc) · 4.14 KB
/
day16.c3
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
module day16;
import std::io;
import std::math;
import std::collections::map;
import std::collections::list;
def ValveMap = HashMap(<String, int>);
def IntPairList = List(<int[<2>]>);
ValveMap v;
struct Valve
{
int id;
String name;
int rate;
int[10] tunnels;
int tunnel_count;
int[100] tunnel_cost;
IntPairList list;
}
fn void populate_tunnels(Valve[] valves, int i, Valve* v, int tunnel, int cost)
{
if (tunnel == i) return;
int current_cost = v.tunnel_cost[tunnel];
if (current_cost && current_cost <= cost) return;
v.tunnel_cost[tunnel] = cost;
Valve* other_valve = &valves[tunnel];
for (int j = 0; j < other_valve.tunnel_count; j++)
{
populate_tunnels(valves, i, v, other_valve.tunnels[j], cost + 1);
}
}
fn Valve[] load_valves(Valve[100]* available_slots)
{
File f = file::open("valves.txt", "rb")!!;
defer (void)f.close();
int count = 0;
while (!f.eof())
{
@pool()
{
String line = io::treadline(&f)!!;
String[] parts = line.tsplit(" ");
String name = parts[1].copy();
String rate_part = parts[4];
int val = v.@get_or_set(name, (int)v.count);
Valve *valve_entry = &(*available_slots)[val];
assert(valve_entry.id == 0);
count++;
int rate = rate_part[5..^2].to_int()!!;
int len = parts.len;
*valve_entry = { .rate = rate, .id = val, .name = name };
for (int i = 9; i < parts.len; i++)
{
String valve = parts[i];
if (i < parts.len - 1)
{
valve = valve[..^2];
}
val = v.@get_or_set(valve.copy(), (int)v.count);
valve_entry.tunnels[valve_entry.tunnel_count++] = val;
}
};
}
Valve[] valves = (*available_slots)[:count];
foreach (int i, &v : valves)
{
for (int j = 0; j < v.tunnel_count; j++)
{
populate_tunnels(valves, i, v, v.tunnels[j], 1);
}
}
// Make entering the other tunnels super expensive.
foreach (int i, &v : valves)
{
if (!v.rate) continue;
foreach (&v2 : valves)
{
v2.list.push({ i, v2.tunnel_cost[i] });
}
}
assert(count == v.count);
return valves;
}
fn int visit_valve(int id, Valve[] valves, int* locations, int minutes, int current_flow)
{
assert(minutes > 0);
Valve* current = &valves[id];
assert(locations[id] == 1);
int total = 0;
if (current.rate)
{
minutes--;
total += current_flow;
current_flow += current.rate;
}
int best_val = minutes * current_flow;
if (minutes > 1)
{
foreach (pair : current.list)
{
int i = pair[0];
if (locations[i]) continue;
int cost = pair[1];
assert(cost);
if (cost >= minutes) continue;
locations[i] = 1;
int val = current_flow * cost + visit_valve(i, valves, locations, minutes - cost, current_flow);
locations[i] = 0;
if (val > best_val) best_val = val;
}
}
return best_val + total;
}
fn int visit_valve2(int id, Valve[] valves, int* locations, int minutes, int current_flow, int start_moves, int start)
{
assert(minutes > 0);
Valve* current = &valves[id];
int total = 0;
if (current.rate)
{
minutes--;
total += current_flow;
current_flow += current.rate;
}
int best_val = minutes * current_flow + visit_valve(start, valves, locations, start_moves, 0);
if (minutes > 1)
{
foreach (pair : current.list)
{
int i = pair[0];
if (locations[i]) continue;
int cost = pair[1];
assert(cost);
if (cost >= minutes) continue;
locations[i] = 1;
int val = current_flow * cost + visit_valve2(i, valves, locations, minutes - cost, current_flow, start_moves, start);
locations[i] = 0;
if (val > best_val)
{
best_val = val;
}
}
}
return best_val + total;
}
fn void part1(Valve[] valves, int start)
{
int[100] locations;
locations[start] = 1;
int flow = visit_valve(start, valves, &locations, 30, 0);
io::printfn("Best flow: %d", flow);
}
fn void part2(Valve[] valves, int start)
{
int[100] locations;
locations[start] = 1;
int flow = visit_valve2(start, valves, &locations, 26, 0, 26, start);
io::printfn("Best flow: %d", flow);
}
fn void main()
{
v.new_init();
int start = -1;
Valve[] valves = load_valves(&&Valve[100] {});
foreach (int i, valve : valves)
{
if (valve.name == "AA") start = i;
}
part1(valves, start);
part2(valves, start);
}